type property_name {
get {
// get accessor code
}
set {
// set accessor code
}
}
using System;
using System.Collections;
namespace ConsoleApplication1
{
public class Employee
{
string empName;
int empAge;
public Employee()
{
this.empName ="N/A";
this.empAge = 0;
}
public string Name
{
get { return empName; }
set { empName = value; }
}
public int Age
{
get { return empAge; }
set
{
if(value>0)
empAge = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
Console.WriteLine("Enter Employee Name");
obj.Name = Console.ReadLine();
Console.WriteLine("Enter Employee Age");
obj.Age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Employee Name ={0} , Employee Age ={1}",
obj.Name,obj.Age);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Employee
{
public Employee()
{
this.Name ="N/A";
this.ID = 0;
}
public Employee(int id,string name)
{
this.ID = id;
this.Name = name;
}
public int ID{ get; private set;}
public string Name { get; private set; }
public static List<Employee> GetEmployees()
{
List<Employee> list = new List<Employee>();
list.Add(new Employee(1,"Raj"));
list.Add(new Employee(2,"Digvijay"));
list.Add(new Employee(3,"Jenifer"));
list.Add(new Employee(4,"Heena"));
return list;
}
}
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
foreach(Employee emp in Employee.GetEmployees())
{
Console.WriteLine("Employee ID ={0} , Employee Name ={1}", emp.ID, emp.Name);
}
Console.ReadLine();
}
}
}