Basic Example of using Constructor

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace TestingOops  
  7. {  
  8.    class Employee  
  9.    {  
  10.       public int Empage  
  11.       {  
  12.          getset;  
  13.       }  
  14.       public string Empname  
  15.       {  
  16.          get;set;  
  17.       }  
  18.       public string Empphonenumber  
  19.       {  
  20.          get;  
  21.          set;  
  22.       }  
  23.       public double Empsalary  
  24.       {  
  25.          get;  
  26.          set;  
  27.       }  
  28.       public void Bonus(double yearsbouns)  
  29.       {  
  30.          Empsalary = Empsalary + Empsalary * yearsbouns;  
  31.       }  
  32.       public Employee(int empage,string empname,string empphonenumber,double empsalary) // Defining a constructor and passing parameters  
  33.       {  
  34.          Empsalary = empsalary;  
  35.          Empage = empage;  
  36.          Empname = empname;  
  37.          Empphonenumber = empphonenumber;  
  38.       }  
  39.       public Employee() //Defining this constructor as default.  
  40.       {  
  41.       }  
  42.    }  
  43.    class Program  
  44.    {  
  45.       static void Main(string[] args)  
  46.       {  
  47.          Employee emp = new Employee  
  48.          {  
  49.             Empage=32,  
  50.             Empname="Ujjwal",  
  51.             Empphonenumber="9031-214-542",  
  52.             Empsalary=50000  
  53.          };  
  54.          Console.WriteLine("Employee Name is : \'{0}\' and Age is {1}. {0} gets salary of $ {2} and his phone number is {3}",emp.Empname,emp.Empage,emp.Empsalary,emp.Empphonenumber);  
  55. emp.Bonus(0.05);  
  56.          Console.WriteLine("{0}'s salary after bonus is $ {1}", emp.Empname, emp.Empsalary);  
  57.          Employee Steve = new Employee(22,"Steven Mathews","242-34-53",30000);  
  58.          Console.WriteLine("Employee Name is : \'{0}\' and Age is {1}. {0} gets salary of $ {2} and his phone number is {3}", Steve.Empname, Steve.Empage, Steve.Empsalary, Steve.Empphonenumber);  
  59.          Console.ReadKey();  
  60.       }  
  61.    }  
  62. }