In case of generic collection the types of value we want to store under the collections need not be pre-defined types like int,float,double,string etc... But it can also be some user defined types.
Code:
Code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace genericUserDefined
- {
- class Employee
- {
- public int eid { get; set; }
- public string first_name { get; set; }
- public string last_name { get; set; }
- public string department { get; set; }
- public double salary { get; set; }
- }
- }
- //Main class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace genericUserDefined
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Employee> elist = new List<Employee>();//Declare user defined type employee in the list
- Employee e1 = new Employee();
- e1.eid = 101;
- e1.first_name = "Sujan";
- e1.last_name = "Hasan";
- e1.department = "IT";
- e1.salary = 55000.00;
- elist.Add(e1);
- Employee e2 = new Employee();
- e2.eid = 102;
- e2.first_name = "Shakil";
- e2.last_name = "Sorker";
- e2.department = "Networking";
- e2.salary = 75000.00;
- elist.Add(e2);
- Employee e3 = new Employee();
- e3.eid = 100;
- e3.first_name = "Shimul";
- e3.last_name = "Hossain";
- e3.department = "CEO";
- e3.salary = 175000.50;
- elist.Add(e3);
- print(elist);
- Console.ReadKey();
- }
- public static void print(List<Employee> emp)
- {
- foreach (Employee obj in emp)
- {
- Console.WriteLine(obj.eid+" "+obj.first_name+" "+obj.last_name+" "+obj.department+" "+obj.salary);
- }
- }
- }
- }
0 Comments:
Post a Comment