संग्रह प्रारंभकर्ता सिंटैक्स का उपयोग करके वर्ग वस्तुओं की तरह संग्रह प्रारंभ करें।
सबसे पहले, कर्मचारी ऑब्जेक्ट के लिए मान सेट करें -
var emp1 = new Employee() { EID = 001, EmpName = "Tim", EmpDept = "Finance"}; var emp2 = new Employee() { EID = 002, EmpName = "Tom", EmpDept = "HR"};
अब इसे एक संग्रह में जोड़ें।
IList<Employee> empDetails = new List<Employee> {emp1, emp2 };
आइए देखें पूरा कोड -
उदाहरण
using System; using System.Collections.Generic; public class Demo { public static void Main() { var emp1 = new Employee() { EID = 001, EmpName = "Tim", EmpDept = "Finance"}; var emp2 = new Employee() { EID = 002, EmpName = "Tom", EmpDept = "HR"}; IList<Employee> empDetails = new List<Employee> {emp1, emp2 }; // Employee 1 Console.WriteLine("Employee One..."); Console.WriteLine(emp1.EID); Console.WriteLine(emp1.EmpName); Console.WriteLine(emp1.EmpDept); // Employee 2 Console.WriteLine("Employee Two..."); Console.WriteLine(emp2.EID); Console.WriteLine(emp2.EmpName); Console.WriteLine(emp2.EmpDept); } } public class Employee { public int EID { get; set; } public string EmpName { get; set; } public string EmpDept { get; set; } }
आउटपुट
Employee One... 1 Tim Finance Employee Two... 2 Tom HR