Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中将自动属性初始化为不为null?_C#_.net_Initialization_Automatic Properties - Fatal编程技术网

如何在C#中将自动属性初始化为不为null?

如何在C#中将自动属性初始化为不为null?,c#,.net,initialization,automatic-properties,C#,.net,Initialization,Automatic Properties,我有一个财产: public Dictionary<string, string> MyProp { get; set; } 公共字典MyProp{get;set;} 当我调用该属性来添加项时,我得到一个NullReferenceException 如何在属性本身中执行null检查,以便在属性为null时为我提供一个新的检查?同时保持自动属性模式 谢谢 您必须使用显式支持字段,不能更改自动属性的getter或setter。在构造函数中初始化它 public MyClass(){

我有一个财产:

public Dictionary<string, string> MyProp { get; set; }
公共字典MyProp{get;set;}
当我调用该属性来添加项时,我得到一个NullReferenceException

如何在属性本身中执行null检查,以便在属性为null时为我提供一个新的检查?同时保持自动属性模式


谢谢

您必须使用显式支持字段,不能更改自动属性的getter或setter。

在构造函数中初始化它

public MyClass(){  dictionary = new
 Dictionary<string,string>() 
}
public MyClass(){dictionary=new
字典()
}

如果没有显式的私有变量,唯一的其他方法是向类的构造函数添加一些代码:

MyProp = new Dictionary<string,string>();
MyProp=newdictionary();

您可以在构造函数中初始化它:

public MyClass()
{
  MyProp = new Dictionary<string, string>();
}
publicmyclass()
{
MyProp=新字典();
}
有一个名为
的DefaultValueAttribute
,允许您指定成员所需的默认值,但它不会自动将成员设置为指定的值;不过,希望并没有破灭,因为您可以在运行时使用反射检索并应用此值,如中所述:


我还没有测试过这个,某些类型可能会有问题,但我会让你考虑和决定。如果您决定实现这一点,那么肯定可以进行改进。

如果您要运行这段代码,您将得到一个NullReferenceException,因为该字段从未初始化过

class Program
{
    static void Main(string[] args)
    {
        Person sergio = new Person();
        sergio.Items.Add("test", "test");

        Console.ReadKey();
    }

    public class Person
    {
        public Dictionary<string, string> Items { get; set; }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
Person sergio=新人();
sergio.Items.添加(“测试”、“测试”);
Console.ReadKey();
}
公共阶层人士
{
公共字典项{get;set;}
}
}
所以解决这个问题的一种方法是在类的构造函数中初始化它

class Program
{
    static void Main(string[] args)
    {
        Person sergio = new Person();
        sergio.Items.Add("test", "test");

        Console.ReadKey();
    }

    public class Person
    {
        public Dictionary<string, string> Items { get; set; }

        public Person()
        {
            Items = new Dictionary<string, string>();
        }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
Person sergio=新人();
sergio.Items.添加(“测试”、“测试”);
Console.ReadKey();
}
公共阶层人士
{
公共字典项{get;set;}
公众人士()
{
Items=新字典();
}
}
}

我认为您不会想要一个setter,因为它总是会生成一个新字典,正如其他人通过在构造函数中调用setter所指出的那样。更好的方法是:

public Dictionary<string, string> MyProp { get; internal set; }
public MyClass() { MyProp = new Dictionary<string, string>(); }

使用getter获取dictionary对象,然后执行Add以添加新项。您不能从代码中调用setter并意外删除字典,因为它对于MyClass之外的任何内容都是只读的。

对于其他陷入这个老问题的人,C#6.0中有一个新特性

在C#6.0中,您还可以在同一语句中将该属性初始化为某个常量值,如下所示:

public Dictionary<string, string> MyProp { get; set; } = new Dictionary<string, string>();
公共字典MyProp{get;set;}=newdictionary();

这似乎非常老套和复杂。好吧,我不能适当地提倡它;但这是可能的。好吧,我选择了这个,它是最干净的。为什么是内部的,而不是私人的,出于兴趣?
InstanceOfMyClass.MyProp.Add(blah, blah);
public Dictionary<string, string> MyProp { get; set; } = new Dictionary<string, string>();