Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

class Person  {     public Person()     {         Name = "Initial Name";     }     public string Name { get; set; } } 

Using normal property syntax (with an initial value)

private string name = "Initial Name"; public string Name  {     get      {         return name;     }     set     {         name = value;     } } 

Is there a better way?

like image 952
bentford Avatar asked Sep 02 '08 21:09

bentford


People also ask

What is the most efficient way to use AC?

Run your air conditioner on the lowest setting required and turn the ceiling fan on. This way, cool air will spread across a wide area, maintaining a comfortable environment. Instead of choosing between an air conditioner and a fan, it's best to combine the two for maximum comfort and efficiency.

Is it better to point the AC up or down?

As a general rule, AC vents should point upward (but not enough to narrow the openings too much). This allows cold air to displace hot air before sinking. An exception is when the vents are located on the walls right below the ceiling. Here, vents must point parallel to the ceiling or slightly down.

What temperature should my AC be set at in summer?

To stay comfortable and save money this summer, the U.S. Department of Energy recommends setting your thermostat to 78F (26C) when you are home. Setting your air conditioner to this level will allow you to stay cool and avoid an unusually high electricity bill.

What should I keep my AC at in the summer to save money?

Many people agree that a comfortable AC temperature is 78 degrees Fahrenheit. If this works for you, setting your AC to this level will certainly help you save money.


2 Answers

In C# 5 and earlier, to give auto implemented properties an initial value, you have to do it in a constructor.

Since C# 6.0, you can specify initial value in-line. The syntax is:

public int X { get; set; } = x; // C# 6 or higher 

DefaultValueAttribute is intended to be used by the VS designer (or any other consumer) to specify a default value, not an initial value. (Even if in designed object, initial value is the default value).

At compile time DefaultValueAttribute will not impact the generated IL and it will not be read to initialize the property to that value (see DefaultValue attribute is not working with my Auto Property).

Example of attributes that impact the IL are ThreadStaticAttribute, CallerMemberNameAttribute, ...

like image 194
Darren Kopp Avatar answered Sep 29 '22 20:09

Darren Kopp


Edited on 1/2/15

C# 6 :

With C# 6 you can initialize auto-properties directly (finally!), there are now other answers 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; } } 
like image 39
Chuck Rostance Avatar answered Sep 29 '22 20:09

Chuck Rostance