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:

0 Comments:

Post a Comment