Below is the small source code which demonstrate how to override in built ToString() method with Student 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_student_tostring_demo
{
public class Student
{
int rno, std;
float per;
string name;
Student()
{
rno = std = 0;
name = "";
per = 0.0f;
}
public Student(int rno, string name, int std, float per)
{
this.rno = rno;
this.std = std;
this.name = name;
this.per = per;
}
public override string ToString()
{
//return base.ToString(){
return "Rollno" + rno.ToString() + "\nname" + name.ToString() + "Div" + std.ToString()+"Percentage"+per.ToString();
//}
}
}
class M
{
static void Main(string[] args)
{
Student s1 = new Student(1, "Amol", 12,98.1f);
Console.WriteLine(s1);
Console.ReadLine();
}
}
}
Comments
Post a Comment