Properties are unique features of C#. They look like data fields, but they have logic behind them. Over the surface, they look like any other member variable. But they act like a member function.
Properties are defined like a field (variable), with the addition of get and set code. Like any other fields or methods, they can use access modifiers.
Let us consider a particular problem. Consider the code below.
using System; namespace Spells { class Program { static void Main(string[] args) { MyClass my = new MyClass(); my.SetVal(7); Console.WriteLine("The value that was set = {0}",my.GetVal()); Console.Read(); //keeps the console alive } } class MyClass { private int myint; public void SetVal(int val) { myint = val; } public int GetVal() { return myint; } } }
We have a class named MyClass. We have a private field and lets assume that its integer type. The methods on line 19 and 23 provides a way to set and get a value from the private field myint. This is neat and pretty basic. If you are familiar with Java, you will see that your IDE provides facilities to implement default code that are usually called getters and setters.
In C#, you can do the same with properties. Properties make your code more readable and manageable. Instead of the methods on line 19 and 23, add the following code inside MyClass.
public int MyIntVal { get { return myint; } set { myint = value; } }
This is what we call a property in C#. MyIntVal is a property. It looks somewhat a mixture of a variable and method declaration. The get code allows you to return the current value of this property. The set code helps you to save the incoming value to the desired variable. Notice the value variable inside the set code. This value is implicit and C# automatically provides the data inside this field. Here is the complete code.
using System; namespace Spells { class Program { static void Main(string[] args) { MyClass my = new MyClass(); my.MyIntVal = 7; //triggers the set logic Console.WriteLine("The value that was set = {0}",my.MyIntVal); //triggers the get logic Console.Read(); //keeps the console alive } } class MyClass { private int myint; public int MyIntVal { get { return myint; } set { myint = value; } } } }
Read the comments on the code, it will help you to get along. Copy and paste or simply write down all the code given. Now remove line 10 and try writing it down again. Once you type my[dot] you will notice a drop down suggestion that Visual Studio is offering.
Properties - MyIntVal |
You will see that Visual Studio marks properties differently for you so that they are easily recognizable. In fact Visual Studio provides different visual information for different types of things.
Why use Properties? The obvious reason is the flexibility that it gives. Firstly properties can be calculated on the fly. Secondly properties promote the idea of Encapsulation. It means that you keep your data logic and private things private, encapsulated inside a class. Encapsulation is one of the core features of Object Oriented Programming. Thirdly, properties provide complete control over data field access. The logic is hidden from the consumer class. In our case, the consumer class is the Program class.
Let's play with them a bit more. You can make properties automatic. It means that the property itself will serve the purpose of another private field. For example, look at the following code snippet.
class MyClass { public int MyIntVal { get; set; } }
I have modified MyClass. Go ahead and use it like you see above. It works right? You can also make a property read only or write only. Just remove the get logic, it makes your property write only. Again, just remove set logic, it makes your property read only.
Finally I would like to show you a real life problem and solution using properties.
using System; namespace Spells { class Program { static void Main(string[] args) { MyProduct soap1 = new MyProduct("Irish Spring", 50); soap1.Price = 95; Console.WriteLine("Price of {0} is {1} BDT", soap1.Name, soap1.Price); MyProduct cake1 = new MyProduct("Chocolate Cheese Cake", 10); cake1.Price = 710; Console.WriteLine("Price of {0} is {1} BDT", cake1.Name, cake1.Price); Console.Read(); //keeps the console alive } } class MyProduct { public string Name { get; set; } private decimal profit = 0.07m; // 7% profit on each product private decimal wholesale; private int units; public decimal Price { get { return wholesale + (wholesale * profit); } set { wholesale = value; } } public MyProduct(string prod, int units) { Name = prod; this.units = units; } } }
Observe the following points:
- Look at line 9 and 14 inside Main. We create two objects. We already know that once we declare an object, the constructor is called. The constructor at line 35 is called each time. The prod argument is assigned to Name property. This triggers the set logic on line 25.
- Consider line 10 and 14. In each case, the objects individual Price property is used to set a value. So the set logic on line 33 is executed.
- Finally consider line 11 and 15. C# compiler understands that you want to access the values set inside those properties. So the get logic is executed, respectively at line 24 and 32 for Name and Price properties. You already understand that among both the properties, Name is an automatic property. You may also notice that the get logic on Price is working on the fly.
No comments:
Post a Comment