![]() |
|
Properties in C#, Free online C# Programming Tutorial... |
| Lessons | C# Properties |
|
public int getSize() { return size; } public void setSize (int value) { size = value; } foo.setSize (getSize () + 1); In C# we can define a property and use the same as if we were using a public variable. This is shown below: public int Size { get {return size; } set {size = value; } } foo.size = foo.size + 1; Note: You can write as many C# statements within get { } and set { } as you need. The value is a keyword and when you assign a value to a property, the compiler provides the assigned value in the 'value' identifier. You can also implement a property as read only or write only. Examples are presented below: public int Size {// write only property set {size = value; } } public int Size { // read only property get {return size; } } The rule is very simple i.e. if you do not implement the get part of a property, it is a write only property. On the other hand, if you do not implement the set part of a property it is a readonly property.
Next >>> Lesson No. 10: C# Pointers
|