i am getting the null reference exception when running the following code.i have created a class where return a collection of objects.Printing the values in the main class.
//main class where i am calling employee class.
class Program
{
static void Main(string[] args)
{
//Creating an object for employee class.
Employees obje = new Employees();
var obj2 = obje.EmployeeDetails();
//looping through each object and printing the values.
foreach (var emp in obj2)
{
Console.WriteLine(emp.Employeeid);
Console.WriteLine(emp.Employeename);
Console.WriteLine(emp.Employeeage);
}
Console.ReadKey();
}
}
//Employee class with some properties and function.
class Employees
{
public int Employeeid { get; set; }
public string Employeename { get; set; }
public string Employeeage { get; set; }
public Employees[] EmployeeDetails()
{
Employees[] objEmployees = new Employees[2];
objEmployees[0].Employeeage = "20";
objEmployees[0].Employeeid = 101;
objEmployees[0].Employeename = "Arjunan";
objEmployees[1].Employeeage = "24";
objEmployees[1].Employeeid = 102;
objEmployees[1].Employeename = "Shiva";
return objEmployees;
}
}
Please tell me where i am doing mistake.am newbie to the dotnet world.