Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

C# Constructor

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConstructorDemo  
  8. {  
  9.     class Student  
  10.     {  
  11.         int id;  
  12.         string name;  
  13.         string dept;  
  14.         //Constructor  
  15.         public Student(int id, string name, string dept) {  
  16.             this.id = id;  
  17.             this.name = name;  
  18.             this.dept = dept;  
  19.         }  
  20.   
  21.         public void showData()  
  22.         {  
  23.             Console.WriteLine("Id: " + id);  
  24.             Console.WriteLine("Name: "+name);  
  25.             Console.WriteLine("Department: "+dept);  
  26.               
  27.         }  
  28.     }  
  29. }  
  30.   
  31.   
  32. //Main class  
  33.   
  34. using System;  
  35. using System.Collections.Generic;  
  36. using System.Linq;  
  37. using System.Text;  
  38. using System.Threading.Tasks;  
  39.   
  40. namespace ConstructorDemo  
  41. {  
  42.     class Program  
  43.     {  
  44.         static void Main(string[] args)  
  45.         {  
  46.             Student st = new Student(101,"Sujan Hasan","CSE");  
  47.             st.showData();  
  48.   
  49.             Student st1 = new Student(102,"Shakil Sorker","CSE");  
  50.             st1.showData();  
  51.   
  52.             Student st2 = new Student(103, "Shimul Hossain", "CSE");  
  53.             st2.showData();  
  54.   
  55.             Console.ReadKey();  
  56.         }  
  57.     }  
  58. }  
Share:

C# Generic Collections Part 6: Comparison Delegate

Comparison delegate is used to sort or order the data inside a collection. It takes two parameters as generic input type and return type should always be int. This is how we can declare Comparison delegate.

Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace genericUserDefined  
  8. {  
  9.     public class Employee : IComparable<Employee>  
  10.     {  
  11.         public int eid { get; set; }  
  12.         public string first_name { get; set; }  
  13.         public string last_name { get; set; }  
  14.         public string department { get; set; }  
  15.         public double salary { get; set; }  
  16.   
  17.         public int CompareTo(Employee other)  
  18.         {  
  19.             if (this.eid > other.eid)  
  20.                 return 1;  
  21.             else if (this.eid < other.eid)  
  22.                 return -1;  
  23.             else  
  24.                 return 0;  
  25.         }  
  26.     }  
  27.     public class compareEmployee : IComparer<Employee>  
  28.     {  
  29.   
  30.         public int Compare(Employee x, Employee y)  
  31.         {  
  32.             if (x.salary > y.salary)  
  33.                 return 1;  
  34.             else if (x.salary < y.salary)  
  35.                 return -1;  
  36.             else  
  37.                 return 0;  
  38.         }  
  39.     }  
  40. }  
  41.   
  42.   
  43. //Main class  
  44.   
  45. using System;  
  46. using System.Collections.Generic;  
  47. using System.Linq;  
  48. using System.Text;  
  49. using System.Threading.Tasks;  
  50.   
  51. namespace genericUserDefined  
  52. {  
  53.     class Program  
  54.     {  
  55.   
  56.         public static int compareNames(Employee n1, Employee n2)  
  57.         {  
  58.             return n1.first_name.CompareTo(n2.first_name);  
  59.         }  
  60.   
  61.         static void Main(string[] args)  
  62.         {  
  63.             List<Employee> elist = new List<Employee>();  
  64.   
  65.             Employee e1 = new Employee();  
  66.             e1.eid = 101;  
  67.             e1.first_name = "Sujan";  
  68.             e1.last_name = "Hasan";  
  69.             e1.department = "IT";  
  70.             e1.salary = 55000.00;  
  71.   
  72.             elist.Add(e1);  
  73.   
  74.             Employee e2 = new Employee();  
  75.             e2.eid = 102;  
  76.             e2.first_name = "Shakil";  
  77.             e2.last_name = "Sorker";  
  78.             e2.department = "Networking";  
  79.             e2.salary = 175000.00;  
  80.   
  81.             elist.Add(e2);  
  82.   
  83.             Employee e3 = new Employee();  
  84.             e3.eid = 100;  
  85.             e3.first_name = "Shimul";  
  86.             e3.last_name = "Hossain";  
  87.             e3.department = "Marketing";  
  88.             e3.salary = 75000.50;  
  89.             elist.Add(e3);  
  90.   
  91.               
  92.   
  93.             print(elist);  
  94.             Console.WriteLine("\nAfter sorting the value with respect of employee id:\n");  
  95.             elist.Sort();  
  96.             print(elist);  
  97.   
  98.             Console.WriteLine("\nSorting with respect to salary:\n");  
  99.             compareEmployee obj = new compareEmployee();  
  100.             elist.Sort(obj);  
  101.             print(elist);  
  102.   
  103.             Comparison<Employee>obj1=new Comparison<Employee>(compareNames);//Using comparison delegate  
  104.             Console.WriteLine("\nSorting with respect to first_name:\n");  
  105.             elist.Sort(obj1);  
  106.             print(elist);  
  107.   
  108.             Console.ReadKey();  
  109.         }  
  110.         public static void print(List<Employee> emp)  
  111.         {  
  112.             foreach (Employee obj in emp)  
  113.             {  
  114.                 Console.WriteLine(obj.eid+" "+obj.first_name+" "+obj.last_name+" "+obj.department+" "+obj.salary);  
  115.             }  
  116.         }  
  117.     }  
  118. }  
Share: