When a class is created, its constructor is called. Constructors have the same name as the class, and they usually initialize the data members of the new object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace constructorDemo
{
class Emp
{
int eno, sal;
string name;
public Emp()
{
eno = 0;
sal = 1200;
name = "-";
}
public Emp(int eno, String name, int sal)
{
set(eno, name, sal);
}
public void set(int eno,string name, int sal)
{
this.eno = eno;
this.name = name;
this.sal = sal;
}
public void show()
{
Console.WriteLine("Empno={0}, Empname={1}, Empsal={2}", eno, name, sal);
}
}
class Program
{
static void Main(string[] args)
{
Emp e1 = new Emp();
e1.show();
e1.set(1, "Vinayak", 23456);
e1.show();
Console.ReadLine();
}
}
}
Comments
Post a Comment