Properties

 “Properties” are one of the new features of C#.
 It’s taken from “Visual Basic”, not from C/C++.
 Generally, “Properties” concept not used in the classes, in some rare cases and complex / large scale classes, those are used.
 In simple, properties are extensions to “data members”.
 As you know that a data member contains a value, a property implements additional logic on that data member, especially when its value is changed.
 For example, you imaging a windows form.
Properties
 Currently, the form window height is 300 pixels.
 Suppose you change its height like this:
Form1.Height = 400;
 In this case, let us imagine that “Height” is a normal data member. With the above statement, the value of the data member will be changed. But it’s not enough. The window height should be increased visually. So, whenever you assign the value of the data member, some necessary code should be executed automatically, that changes the window height visually. In this kind of situations, “Properties” are used.
 Now, let us imagine that “Height” is a property. Then whenever you assign a value to it, it’s “set” method will be executed automatically.
 In the same way, in the properties, “get” method is also implemented, which gets automatically called when the property value is requested in the client code.
 We understand better with the following implementation diagram of “Properties”. It contains two accessors called “set” and “get”.
proferties
 The “Set” Accessor:

1. This gets called automatically, whenever a value is assigned to the property in the client code. Ex: obj.property = value;
2. The value that is assigned in the client code is available as “implicit parameter”, named “value” in this “set” accessor.
3. This accessor can’t return any value.

 The “Get” Accessor:
1. This gets called automatically, whenever the property value is requested in the client code. Ex: obj.property
2. No implicit parameters are available.
3. This accessor should return the value of the property.


IMP Note: No memory gets allocated for the property. So that, it can’t store any value. To store the actual value, we use a “private data member”.

The complete Property Implementation Syntax:

private datatype variablename;
accessmodifier datatype propertyname
{
set
{
variablename = value;
}
get
{
return (variablename);
}
}

Note: There should be some difference in the variable name and property name.
Ex:
private string name;
public string Name
{
set
{
name = value;
}
get
{
return (name);
}
}

Properties

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PropertyDemo
{
class Student
{
private int studentid;
private string studentname;
public int StudentID
{
set
{
Console.WriteLine("This is set accessor of student id.");
studentid = value;
}
get
{
Console.WriteLine("This is get accessor of student id.");
return studentid;
}
}
public string StudentName
{
set
{
Console.WriteLine("This is set accessor of student name.");
studentname = value;
}
get
{
Console.WriteLine("This is get accessor of student name.");
return studentname;
}
}
}
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.StudentID = 101;
stu.StudentName = "Ram";
Console.WriteLine(stu.StudentID);
Console.WriteLine(stu.StudentName);
System.Console.Read();
}
}
}
Properties
Read only Properties:
 Simply implement “get” accessor only, don’t implement “set” accessor.
 Syn:
private datatype variablename;
accessmodifier datatype propertyname
{
get
{
return (variablename);
}
}

 Ex:
private string name;
public string Name
{
get
{
return (name);
}
}

Write only Properties:

 Simply implement “set” accessor only, don’t implement “get” accessor.

 Syn:
private datatype variablename;
accessmodifier datatype propertyname
{
set
{
variablename = value;
}
}

 Ex:
private string name;
public string Name
{
get
{
name = value;
}
}

Automatic Properties:
 This is introduced in .NET Framework 3.0.
 To simplify the syntax of property declaration, that contains no extra logic in “set accessor” and “get accessor”, this concept is introduced.
 According to this, no code for “set” and “get” accessors needed to write.
 No extra variable is needed to be declared.

 Syn:
Accessmodifier datatype propertyname { get; set; }

 Ex: public string Name { get; set; }