//default constructor example
//When a method name is same as class name we call it as constructor
//a constructor which has no argument is known as default constructor
using System;
namespace ConstructorCreation
{
public class Employee
{
public Employee()
{
Console.WriteLine("Default Constructor Invoked Hoyeche");
}
}
class Program
{
static void Main(string[] args)
{
Employee e1=new Employee();
Employee e2=new Employee(); //we are creating object here
Console.ReadKey();
}
}
}
//default constructor example
//When a method name is same as class name we call it as constructor
//a constructor which has no argument is known as default constructor
//main in same class
using System;
namespace ConstructorCreation
{
public class Employee
{
public Employee()
{
Console.WriteLine("Default Constructor Invoked Hoyeche");
}
static void Main(string[] args)
{
Employee e1=new Employee();
Employee e2=new Employee(); //we are creating object here
Console.ReadKey();
}
}
}
//parameterized constructor
//constructors is one kind of method where method name and class name is same
using System;
namespace ParameterizedConstructor
{
public class Employee
{
public int id;
public string name;
public float salary;
public Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
public void Display()
{
Console.WriteLine(id+" "+name+" "+salary);
}
}
class TestEmployee
{
static void Main(string[] args)
{
Employee e1 = new Employee(101, "Zaki", 555.56f);
Employee e2 = new Employee(624, "Live", 6666.5f);
e1.Display();
e2.Display();
Console.ReadKey();
}
}
}
Destructor:
//destructor is known as finalizer in c#
//destructor can't be public
//destructor does not contain any modifiers
using System;
namespace Destructor
{
class Employee
{
public Employee()
{
Console.WriteLine("Constructor Invoked");
}
~Employee()
{
Console.WriteLine("Destructor Invoked");
}
}
class TestEmployee
{
static void Main(string[] args)
{
Employee e1=new Employee();
Employee e2=new Employee();
Console.ReadKey();
}
}
}