Wriju's BLOG

.NET, Cloud and everything

C# 3.0 Features: Automatic Property (Part 3)

I was about to write this entry in my blog which I found. Before that I got comment in my previous Blog on C# 3.0 Features: Automatic Property (Part 2). The same question came in my mind. What will happen if you try to create an automatic property just with get. The C# 3.0 compiler throws an error. If you try to write


 


//Automatic Property


public int CustID2


{


    get;


}


 


The error looks like


 


‘ConsoleApplication1.Customer.CustID2.get’ must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.


 


To many it is very unfortunate because most often we may need to declare read-only property (commonly known as “getter” property). But we do have way to do this


 


The same set of property can be declared as


 


public int CustID2


{


    get;


    private set;


}


 


This allows us to create the read-only property.


 


Namoskar!!!