.net 如何在C#中使局部字符串变量成为只读?

a11xaf1n  于 2023-04-22  发布在  .NET
关注(0)|答案(5)|浏览(142)

我有一个本地字符串(文件路径),我只需要从函数中检索一次,并且我想确保它永远不会再次修改。我不能使用const关键字,因为我的字符串的值是在运行时而不是编译时确定的。所以我尝试使用readonly关键字,但是Visual Studio告诉我它对我的项目无效。我如何才能达到我想要的保护级别,最好不需要制作另一个类?
为了简单和公司的政策,我已经(彻底地)缩小并重命名了我的类和函数,但概念是相同的。

public class myClass
{
    private void myFunction()
    {
      readonly string filePath = HelperClass.getFilePath("123");

     //do stuff
    }
}

public static class HelperClass
{ 
    public static string getFilePath(string ID)
    {
        switch(ID)
        {
             case "123":
                 return "C:/123.txt";

             case "234":
                 return "C:/234.txt";

             default:
                 throw new Exception(ID + " is not supported");
        }
    }
}

===编辑PS2Goat ====

public class myClass
{
    protected SomeObject o;
    private virtual readonly string path;        

    public myClass(someObject o)
    {
        this.o = o;
        path = HelperClass.getFilePath(o.getID());
    }

    private virtual void myFunction()
    { 

     //do stuff
    }
}

public class myDerivedClass
{
    private override virtual readonly string path;        

    public myDerivedClass(someObject o) : base(o)
    {
        path = HelperClass.getFilePath(o.getID()); //ID will be different
    }

    private override void myFunction()
    { 

     //do different stuff
    }
}



public static class HelperClass
{ 
    public static string getFilePath(string ID)
    {
        switch(ID)
        {
             case "123":
                 return "C:/123.txt";

             case "234":
                 return "C:/234.txt";

             default:
                 throw new Exception(ID + " is not supported");
        }
    }
}

如果我想抛出一个异常,我必须在父类的构造函数中捕获它(直到该类被支持),因为父类构造函数将在派生构造函数之前被调用。所以在调用子构造函数(具有正确的ID)之前,错误的ID将被设置一次。

eqzww0vc

eqzww0vc1#

你不能在一个方法中设置只读变量的作用域。因此,它应该被提升为一个readonly static字段:

public class myClass
{
    private readonly static string filePath = HelperClass.getFilePath("123");
    private void myFunction()
    {    
      //do stuff
    }
}

这将导致您的filePath变量在第一次访问myClass时被初始化。如果这不是您想要的,getFilePath是一个长时间运行/昂贵的操作,并且您想等到myFunction被调用,您可以用System.Lazy<T>替换实现:

public class myClass
{
    private readonly static Lazy<string> filePath 
            = new Lazy<string>(() => HelperClass.getFilePath("123")));
    private void myFunction()
    {   
      string path = filePath.Value;
      //do stuff
    }
}
gzszwxb4

gzszwxb42#

readonly意味着它可以ONLY在类构造函数或instatiation中设置。因此,您可以将逻辑更改为如下所示:

public class myClass
{
    private readonly string _filePath;

    public myClass()
    {
        _filePath = HelperClass.getFilePath("123");
    }

    private void myFunction()
    {
      // Use your _filePath here...

     //do stuff
    }
}

public static class HelperClass
{ 
    public static string getFilePath(string ID)
    {
        switch(ID)
        {
             case "123":
                 return "C:/123.txt";

             case "234":
                 return "C:/234.txt";

             default:
                 throw new Exception(ID + " is not supported");
        }
    }
}
kqhtkvqz

kqhtkvqz3#

您可以将readonly变量移到函数定义之外声明。

public class myClass
    {
        readonly string filePath = HelperClass.getFilePath("123");

        private void myFunction()
        {

         //do stuff with filePath
        }
    }
yjghlzjz

yjghlzjz4#

因为这是一个变量,而不是字段或属性,所以不能将其标记为只读。然而,您可以通过使用匿名类型来“欺骗”并实现您想要的,这确保了其属性是只读的。
例如:

var data = new
    {
        FileName = HelperClass.getFilePath("123");
    };
kx1ctssn

kx1ctssn5#

filePath变量提升到field,这应该可以修复错误。局部变量不能是只读的。

public class myClass
{
      readonly string filePath = HelperClass.getFilePath("123");
}

相关问题