What is the best way to give a C# auto-property an initial value?

前端 未结 22 2998
死守一世寂寞
死守一世寂寞 2020-11-22 02:48

How do you give a C# auto-property an initial value?

I either use the constructor, or revert to the old syntax.

Using the Constructor:

相关标签:
22条回答
  • 2020-11-22 03:28

    In C# 6 and above you can simply use the syntax:

    public object Foo { get; set; } = bar;
    

    Note that to have a readonly property simply omit the set, as so:

    public object Foo { get; } = bar;
    

    You can also assign readonly auto-properties from the constructor.

    Prior to this I responded as below.

    I'd avoid adding a default to the constructor; leave that for dynamic assignments and avoid having two points at which the variable is assigned (i.e. the type default and in the constructor). Typically I'd simply write a normal property in such cases.

    One other option is to do what ASP.Net does and define defaults via an attribute:

    http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx

    0 讨论(0)
  • 2020-11-22 03:28

    Use the constructor because "When the constructor is finished, Construction should be finished". properties are like states your classes hold, if you had to initialize a default state, you would do that in your constructor.

    0 讨论(0)
  • 2020-11-22 03:29

    Edited on 1/2/15

    C# 6 :

    With C# 6 you can initialize auto-properties directly (finally!), there are now other answers in the thread that describe that.

    C# 5 and below:

    Though the intended use of the attribute is not to actually set the values of the properties, you can use reflection to always set them anyway...

    public class DefaultValuesTest
    {    
        public DefaultValuesTest()
        {               
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(this))
            {
                DefaultValueAttribute myAttribute = (DefaultValueAttribute)property.Attributes[typeof(DefaultValueAttribute)];
    
                if (myAttribute != null)
                {
                    property.SetValue(this, myAttribute.Value);
                }
            }
        }
    
        public void DoTest()
        {
            var db = DefaultValueBool;
            var ds = DefaultValueString;
            var di = DefaultValueInt;
        }
    
    
        [System.ComponentModel.DefaultValue(true)]
        public bool DefaultValueBool { get; set; }
    
        [System.ComponentModel.DefaultValue("Good")]
        public string DefaultValueString { get; set; }
    
        [System.ComponentModel.DefaultValue(27)]
        public int DefaultValueInt { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-22 03:32

    When you inline an initial value for a variable it will be done implicitly in the constructor anyway.

    I would argue that this syntax was best practice in C# up to 5:

    class Person 
    {
        public Person()
        {
            //do anything before variable assignment
    
            //assign initial values
            Name = "Default Name";
    
            //do anything after variable assignment
        }
        public string Name { get; set; }
    }
    

    As this gives you clear control of the order values are assigned.

    As of C#6 there is a new way:

    public string Name { get; set; } = "Default Name";
    
    0 讨论(0)
提交回复
热议问题