Properties Get Set Short Form Short Hand Auto-Implemented properties, via C# 3.0.
//linqpad, run as C# program
class Student
{
//Short Hand Auto-Implemented properties, via C# 3.0.
public string Code { get; set; }
public string Name { get; set; }
public int Age { get; set; }
/* Long Form: Eliminates having to declare the private fields
private field declarations not needed either!
private string code;
private string name;
private int age;
*/
/* Long form : Eliminates having to write these long blocks of
property setting code.
Traditional way of declaring properties prior C#3.0
Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type string:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a Age property of type int:
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
*/
}
class ExampleDemo
{
public static void Main()
{
// Create a new Student object:
Student s = new Student();
// Setting code, name and the age of the new student object
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: {0}, {1}, {2}", s.Code, s.Name, s.Age);
}
}
//linqpad, run as C# program
class Student
{
public string Code { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
class ExampleDemo
{
public static void Main()
{
Student s = new Student();
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: {0}, {1}, {2}", s.Code, s.Name, s.Age);
}
}