Below is the small source code which demonstrate how to override in built ToString() method with employee class in C# which is mostly used in exception handling.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace override_tostring_employee_demo
{
public class Emp
{
int eno, sal;
string name;
Emp()
{
eno = sal = 0;
name = "";
}
public Emp(int rno, string name,int sal)
{
this.eno = rno;
this.sal = sal;
this.name = name;
}
public override string ToString()
{
//return base.ToString(){
return "Empno" + eno.ToString() + "\nname" + name.ToString() + "Salary" + sal.ToString();
//}
}
}
public class M
{
static void Main(string[] args)
{
Emp e1 = new Emp(1,"Amol",12345);
Console.WriteLine(e1);
Console.ReadLine();
}
}
}
Comments
Post a Comment