Quick Properties

Encapsulation

LIMITED TO LIBERATE!

photo credit

Coding had been enormous fun for me and sharing the experience seems to be a surplus. This blog is to focus on C# related tips/routine/quickies but i am sure you would not mind little detours once in a while :-). Intended audience is beginner to intermediate C# devs but i have doubt there is any gauge to really measure the level. So, you are all welcome. Here you go…

Properties are a fun way to encapsulate.

Regular Property

string _myRegularProperty;
public string MyRegularProperty
{
  get{return _myRegularProperty;}
  set{_myRegularProperty = value;}
}

Save yourself from defining private fields for each property, c# will do it for you since 3.0.
Auto Implemented Properties

public string MyAutoProperty{get; set;}

If you want to control the visibility of get/set you can do so by assigning access modifier to get/set, by default they are public. For example, if you want only the containing class to update this property and make the property available for read only, do following

public string MyAutoProperty{get; private set;}

See more here, here, and here

Happy coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s