What & Why : Properties :: 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:

////////// 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
{
// 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.cs

Now type ExA and press enter (return) key...

Output is: Your favorite color is Yellow

In Example 'A', I have declared two classes in Rahul namspace:

One is UrChoice (as the names are self explanatory) is talking about your choice, not all of your choices but here we are discussing about your favorite color and you have fixed your favorite color as "White" (Just for an instance). Now everybody is free to know about your favorite 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 a normal variable rather we defined a property named Color.

But in this case we had to define a new variable MyColor to store the initial and right value of your favorite color (also making it private to 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 Data Type 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 the return statement of the get block, so that somebody can only get the value 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 of this eries and also about the "set" block of a property, so that we can 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


Recommended Free Ebook
Similar Articles