//Property Introduction
using System;
class Student
{
// Declare Private Data
int sage,sno;
string sname;
//Property Declaration for sno
public int Id
{
get
{
return sno;
}
}
//Property Declaration for sname
public string Name
{
get
{
return sname;
}
set
{
sname=value;
}
}
//Property Declaration for sage
public int Age
{
get
{
return sage;
}
set
{
sage=value;
}
}
//Parameter Constructor with one argument
public Student(int id)
{
sno=id;
}
}
class Program
{
static void Main()
{
Student obj=new Student(10001);
//Setting Values to Proreties.
obj.Name="Ankur Bhatnagar";
obj.Age=26;
//obj.Id=100; we cannot use it because it will give a compile time error that Id property is Read-Only.
//Getting Values Through Properties
Console.WriteLine("Id: {0}, Name: {1}, Age: {2}\n",obj.Id,obj.Name,obj.Age);
}
}
No comments:
Post a Comment