Property - A Change from C# 2.0 to C# 3.0


Property is used to access the private Data members of the Class. Property is public and contains get and set Methods. get Method returns the value and set method assigns the value to the private data members. This article provides a change in the Property Syntax from C# 2.0 to C# 3.0.

Property - Introduction


Property is used to access the private Data members of the Class. Property is public and contains get and set Methods. get Method returns the value and set method assigns the value to the private data members. This article provides a change in the Property Syntax from C# 2.0 to C# 3.0.

C# 2.0 Property


1. Declare the Private variable in the class.
2. Define a public Property to Access the value.
3. Property Definition contains get and set Methods.

using System;
class FullName
{
// Private Variable Declaration
string fName;
string lName;
// Public Property Declaration
public string FName {
get{
return fName;
}
set {
fName=value;
}
}
public string LName {
get{
return lName;
}
set{
lName=value;
}
}
public void showFullName() {
Console.WriteLine("Full Name : " + FName + LName);
}
}
class PropertiesDemo
{
public static void Main() {
FullName objFN = new FullName();
// Accessing the Public properties
objFN.FName = "Vijaya";
objFN.LName = "Lakshmi";
objFN.showFullName();
}
}

C# 3.0 Property


1. Property Definition has been simplified.
2. We need to write only get and set Keywords inside the Property Definition.
3. No need of declaring the private data members in the class
4. Private Data members and syntax for get/set methods will be generated by the System.
5. Advantage: It reduces number of lines of code and gives better readability to the program.

// Properties C# 3.0
using System;
class FullName
{
// Public Property Declaration C# 3.0
public string FName {
get;
set;
}
public string LName {
get;
set;
}
public void showFullName() {
Console.WriteLine("Full Name : " + FName + LName);
}
}
class PropertiesDemo
{
public static void Main() {
FullName objFN = new FullName();
// Accessing the Public properties
objFN.FName = "Vijaya";
objFN.LName = "Lakshmi";
objFN.showFullName();

}
}


Comments

Author: Gaurav Aroraa19 Apr 2012 Member Level: Gold   Points : 0

You missed Proeprties with ViewState



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: