We used to declare properties with get & set blocks
public string FirstName {
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
if we have to make them read only we used to remove the set block & if the property has to be made write only the get block was removed. In most cases there was not other filtering or validations done so Microsoft introduced the Automatic properties with .Net 2008
in C# you will declare a automatic property like the following
public string FirstName {get; set;}
Now there ’ s no need for you to define private members to store the values of the properties. Instead, you just need to use the get and set keywords, and the compiler will automatically create the private members in which to store the properties values. If you decide to add filtering rules to the properties later, you can simply implement the set and get accessor of each property.
To restrict the visibility of the get and set accessor when using the automatic properties feature, you simply prefix the get or set accessor with the private keyword, like this:
public string FirstName {get; private set;}
This statement sets the FirstName property as read - only.
Like the normal property implementation if you try to remove the set or get you will get a nasty compile time error.
Error 1 'ConsoleApplication1.FileHelper.FirstName.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
public string FirstName {
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
if we have to make them read only we used to remove the set block & if the property has to be made write only the get block was removed. In most cases there was not other filtering or validations done so Microsoft introduced the Automatic properties with .Net 2008
in C# you will declare a automatic property like the following
public string FirstName {get; set;}
Now there ’ s no need for you to define private members to store the values of the properties. Instead, you just need to use the get and set keywords, and the compiler will automatically create the private members in which to store the properties values. If you decide to add filtering rules to the properties later, you can simply implement the set and get accessor of each property.
To restrict the visibility of the get and set accessor when using the automatic properties feature, you simply prefix the get or set accessor with the private keyword, like this:
public string FirstName {get; private set;}
This statement sets the FirstName property as read - only.
Like the normal property implementation if you try to remove the set or get you will get a nasty compile time error.
Error 1 'ConsoleApplication1.FileHelper.FirstName.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
Comments