Override 'ToString()' Function in a class (c# / .NET)
Let's suppose 'MyClass' is the name of the class for which you want to override 'ToString()' function.
Then, you can override the function as below.
public class MyClass
{
// This function will override the ToString() function when called for MyClass Object.
public override string ToString()
{
string yourReturnString = "String which you want to return when ToString() for this class object is called.";
return yourReturnString;
}
}
Now let's create an object for the class and call 'ToString()' function.
MyClass obj = new MyClass();
var overriddenString = obj.ToString();
Try it yourself and see the result.
No comments:
Post a Comment