C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Automatic Properties in C Sharp


Posted Date: 02 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: Muhammad JavedMember Level: Gold    
Rating: 1 out of 5Points: 7



Automatic Properties in C#


In the Contact class defined in the previous section, apart from the ID property, the properties are
actually not doing much except assigning their values to private members:

public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName = value;
}
}
public string Email
{
get
{
return _Email;
}
set
{
_Email = value;
}
}

In other words, you are not actually doing any checking before the values are assigned. In C# 3.0, you
can shorten those properties that have no filtering (checking) rules by using a feature known as automatic
properties . The Contact class can be rewritten as:

public class Contact
{
int _ID;
public int ID
{
get
{
return _ID;
}
set
{
if (value > 0 & & value < = 9999)
{
_ID = value;
}
else
{
_ID = 0;
};
}
}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {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.
You might be tempted to directly convert these properties ( FirstName , LastName , and Email )
into public data members. But if you did that and then later decided to convert these public members into
properties, you would need to recompile all of the assemblies that were compiled against the old class.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Automatic Properties in C#  .  Automatic Properties in C Sharp  .  Automatic Properties  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Keyword "this" in C#
Previous Resource: Exception Handling in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use