How to override the ToString() method
using System;
public class Employee
{
public string firstName;
public string lastName;
public Employee(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public override string ToString()
{
return firstName + " " + lastName;
}
}
class MainClass
{
public static void Main()
{
Employee myEmployee = new Employee("A", "M");
Employee myOtherEmployee = new Employee("B", "N");
Console.WriteLine("myEmployee.ToString() = " + myEmployee.ToString());
Console.WriteLine("myOtherEmployee.ToString() = " + myOtherEmployee.ToString());
}
}
Output myEmployee.ToString() = A M
myOtherEmployee.ToString() = B N
|
HTML code for linking to this page:
Related in same category :
-
-
-
|