What & Why : Properties in C#:: Part 1

 

If you were programming in C or C++ before coming to C#, you'll be a little bit confused about properties.But Visual Basic programmers and Visual C++ programmers are familiar to this concept of Microsoft as a powerful tool.

Well! I'll explain the concept of properties with some examples to make a clear understanding.So First of all:
What are properties?
Just normal variables, like other primitive variables!

Example A:
/////////// ExA.cs //////////////
namespace Rahul
{
using System ;

class Colors
{
public static void Main()
{
UrChoice UrColor = new UrChoice() ;

//Overwriting your favourite color to yellow
UrColor.Color = \"Yellow\" ;
Console.WriteLine(\"Your favourite color is \" + UrColor.Color) ;
}
}

public class UrChoice
{
// Want to provide the access to everybody.
public string Color ;

// Default Constructor setting up your favourite color
public UrChoice()
{
Color = \"White\" ;
}

}

}

Compile the program as: csc ExA.csNow type ExA and press enter (return) key…Output is: Your favorite color is Yellow

In Example 'A', I have declared two classes inRahul namspace:One is UrChoice (as the names are self explanatory) is talking aboutyour choice, not all of your choices but here we are discussing aboutyour favorite color and you have fixed your favorite color as "White"(Just for an instance). Now everybody is free to know about yourfavorite color and he can tell others about your choice.

But there is a problem!
Somebody has changed the value of your favorite color and now your favorite color is Yellow.
Hey! That?s cheating!!!!
Yes! Of course but is there any way to protect your variable or your choice?
Here comes the turn of properties.

Properties are like normal variables but with more power and flexibility.Now we convert the color variable in a property with the same name.

Example B:
////////// ExB.cs //////////////
namespace Rahul
{

using System ;

class Colors
{
public static void Main()
{
UrChoice UrColor = new UrChoice() ;

//Overwriting your favourite color to yellow
UrColor.Color = \"Yellow\" ;
Console.WriteLine(\"Your favourite color is \" + UrColor.Color) ;
}
}

public class UrChoice
{
// your private variable to store your favourite color
private string MyColor ;

// Defining property, Want to provide the access to everybody.
public string Color
{
get{
return MyColor ;
}
}

// Default Constructor setting up your favourite color
public UrChoice()
{
MyColor = \"White\" ;
}
}

}

This time we didn't define color variable as anormal variable rather we defined a property named Color.But in this case we had to define a new variable MyColor to store theinitial and right value of your favorite color (also making it privateto hide it from outside world).
This time when we try to compile our program as: csc ExB.cs
We get an error:
Property or indexer ' Rahul.UrChoice.Color ' can not be assigned to — it is read only.

So, we got actually what we wanted. Now if somebody tries to modify your favorite color, he can not do this.If we discuss the syntax of the property, this is really very easy.
Decide a name to the property that can reflect its use and the DataType of the property and start with the braces, Like in our example:

// Declaring its scope as public so that everybody can use it.
// Data Type is string
public string Color
{

}

Now put the value of the variable in thereturn statement of the get block, so that somebody can only get thevalue but can not write the value to the property. In our example:

public string Color
{
get
{
return MyColor ;
}
}

That's it!!!
Can we do the same security of our data by some other means….
Yes!I'll be discussing another method to do the same thing in the PART 2 ofthis series and also about the \"set\" block of a property, so that wecan make a variable in which everybody can write but can not read.Hey! This is not all about properties, there is a lot…
Something more than this!

Will continue in PART 2….

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

One Response to “What & Why : Properties in C#:: Part 1”

  1. Its very nice example. This example explains very nicely why we use properties