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

前端 未结 22 2994
死守一世寂寞
死守一世寂寞 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:05

    Starting with C# 6.0, We can assign default value to auto-implemented properties.

    public string Name { get; set; } = "Some Name";
    

    We can also create read-only auto implemented property like:

    public string Name { get; } = "Some Name";
    

    See: C# 6: First reactions , Initializers for automatically implemented properties - By Jon Skeet

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

    In addition to the answer already accepted, for the scenario when you want to define a default property as a function of other properties you can use expression body notation on C#6.0 (and higher) for even more elegant and concise constructs like:

    public class Person{
    
        public string FullName  => $"{First} {Last}"; // expression body notation
    
        public string First { get; set; } = "First";
        public string Last { get; set; } = "Last";
    }
    

    You can use the above in the following fashion

        var p = new Person();
    
        p.FullName; // First Last
    
        p.First = "Jon";
        p.Last = "Snow";
    
        p.FullName; // Jon Snow
    

    In order to be able to use the above "=>" notation, the property must be read only, and you do not use the get accessor keyword.

    Details on MSDN

    0 讨论(0)
  • 2020-11-22 03:10
    private string name;
    public string Name 
    {
        get 
        {
            if(name == null)
            {
                name = "Default Name";
            }
            return name;
        }
        set
        {
            name = value;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:11

    In Version of C# (6.0) & greater, you can do :

    For Readonly properties

    public int ReadOnlyProp => 2;
    

    For both Writable & Readable properties

    public string PropTest { get; set; } = "test";
    

    In current Version of C# (7.0), you can do : (The snippet rather displays how you can use expression bodied get/set accessors to make is more compact when using with backing fields)

    private string label = "Default Value";
    
    // Expression-bodied get / set accessors.
    public string Label
    {
       get => label;
       set => this.label = value; 
     }
    
    0 讨论(0)
  • 2020-11-22 03:12

    In C# 6.0 this is a breeze!

    You can do it in the Class declaration itself, in the property declaration statements.

    public class Coordinate
    { 
        public int X { get; set; } = 34; // get or set auto-property with initializer
    
        public int Y { get; } = 89;      // read-only auto-property with initializer
    
        public int Z { get; }            // read-only auto-property with no initializer
                                         // so it has to be initialized from constructor    
    
        public Coordinate()              // .ctor()
        {
            Z = 42;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:12

    I think this would do it for ya givng SomeFlag a default of false.

    private bool _SomeFlagSet = false;
    public bool SomeFlag
    {
        get
        {
            if (!_SomeFlagSet)
                SomeFlag = false;        
    
            return SomeFlag;
        }
        set
        {
            if (!_SomeFlagSet)
                _SomeFlagSet = true;
    
            SomeFlag = value;        
        }
    }
    
    0 讨论(0)
提交回复
热议问题