30

I came to know that C# 3.0 comes with a new feature of Auto-Implemented Properties,I liked it as we don't have to declare extra private varible in this (compare to earlier property), earlier I was using a Property i.e.

private bool isPopup = true;
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}

Now I've converted it into Auto-Implemented property i.e.

public bool IsPopup
{
    get; set;
}

I want to set the default value of this property to true without using it not even in page_init method, I tried but not succeeded, Can anyone explain how to do this?

Sampath
  • 50,641
  • 40
  • 250
  • 357
Imran Rizvi
  • 6,971
  • 10
  • 52
  • 94

5 Answers5

49

You can initialize the property in the default constructor:

public MyClass()
{
   IsPopup = true;
}

With C# 6.0 it is possible to initialize the property at the declaration like normal member fields:

public bool IsPopup { get; set; } = true;  // property initializer

It is now even possible to create a real read-only automatic property which you can either initialize directly or in the constructor, but not set in other methods of the class.

public bool IsPopup { get; } = true;  // read-only property with initializer
slfan
  • 8,209
  • 115
  • 61
  • 73
  • Thanks , I think thats the only way. – Imran Rizvi Nov 28 '11 at 09:48
  • Looking for more specific answers , for this I have to write additional code in constructor , is there no way to write it in the Auto-implemented property itself, without using old conventions. – Imran Rizvi Nov 29 '11 at 09:45
  • 2
    Somehow you have to initialize your property. As there is no backing field (it's generated by the compiler), there is no other option. Under these circumstances I write the full properties, fortunately you get intellisense to do this. – slfan Nov 29 '11 at 09:51
  • @slfan Can you elaborate? I am starting to use auto's and am trying to understand fundamentals. Your answer made sense, but then your comment (as I read it) seems to contradict it- I'm sure I am misunderstanding what you are saying in that comment though as your answer wouldn't be correct and have upvotes. – Nyra Jul 18 '14 at 14:10
  • 1
    At the moment this is the only way. In the coming C# 6.0 there will be the possibility to directly initialize the property. I use full properties only when I have to write code in the setter or getter. – slfan Jul 18 '14 at 17:29
8

Attributes specified for an auto property do not apply to the backing field, so an attribute for a default value won't work for this type of property.

You can, however, initialize an auto property:

VB.NET

Property FirstName As String = "James"
Property PartNo As Integer = 44302
Property Orders As New List(Of Order)(500)

C# 6.0 and above

public string FirstName { get; set; } = "James";
public int PartNo { get; set; } = 44302;
public List<Order> Orders { get; set; } = new List<Order>(500);

C# 5.0 and below

Unfortunately, C# versions below 6.0 do not support this, so you have to initialize the default values for auto properties in the constructor.

Eagle-Eye
  • 1,377
  • 2
  • 17
  • 26
Ed DeGagne
  • 3,092
  • 1
  • 27
  • 46
0

Have you tried DefaultValueAttribute

Akshat Jiwan Sharma
  • 13,482
  • 13
  • 43
  • 58
  • 9
    Ok so I found out that this was not the correct way.It seems that DefaultValueAttribute just instructs the meta data that a property can have a default value and does not actually set it.So I hope this helps someone. – Akshat Jiwan Sharma May 23 '12 at 18:15
0
using System.ComponentModel;

[DefaultValue(true)]
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}
Flexo
  • 82,006
  • 22
  • 174
  • 256
0

You can use default property value like below

One advantage of this method is you don't need to check null values for Boolean types

using System.ComponentModel; 

public class ClassName
 {
   [DefaultValue(true)]
   public bool IsPopup{ get; set; }
 }
Sampath
  • 50,641
  • 40
  • 250
  • 357
  • 2
    That doesn't actually set the default value, it just indicates what the default value should be – Joe Flateau Dec 21 '12 at 18:03
  • @JoeFlateau could you explain what is the difference of both methods?B'cos when I access that property from controller its value is true.So then ? – Sampath Dec 21 '12 at 18:10
  • 4
    DefaultValue is purely informational, Visual Studio will show the value as not bold. – Joe Flateau Dec 21 '12 at 18:42