using System;
public class Student
{
private int _id;
private string _Name;
private int _PassMark = 35;
//private string _city;
//private string _email;
//public string Email
//{
// get
// {
// return this._email;
// }
// set
// {
// this._email = value;
// }
//}
//public string City
//{
// get
// {
// return this._city;
// }
// set
// {
// this._city = value;
// }
//}
// doesn't need this way
//.NET 3.0 - auto implemented properties
public string Email { get; set; }
public string City { get; set; }
//possible to access prop erties as it they were public fields
//property - read only
public int PassMark
{
get
{
return this._PassMark;
}
}
//using property - read , write
public string Name
{
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("Name cannot be null or empty");
}
this._Name = value;
}
get
{
return string.IsNullOrEmpty(this._Name) ? "No Name" : this._Name;
}
}
//property
public int Id
{
set
{
if (value <= 0) // value keyword - receive the value
{
throw new Exception("Student Id cannot be negative");
}
this._id = value;
}
get
{
return this._id;
}
}
}
public class Program
{
public static void Main()
{
Student C1 = new Student();
C1.Id = 101;
C1.Name ="Rody";
//C1.PassMark = 32; can't set value as of read only field
Console.WriteLine("Student Id = {0}", C1.Id);
Console.WriteLine("Student Name = {0}", C1.Name);
Console.WriteLine("Student Mark = {0}", C1.PassMark);
}
}