Customizing .NET: Overriding the ToString() Method in C# (System.Object)

In this blog post, we’re going to talk about a special trick in C# programming. This trick is about changing the ToString() method behaviour. In technical terms, we use the word How to Override the ToString() in C# programming.

What is the use of ToString()?

Imagine you have a number, like 10, and you want to assign it into a String, but the variable B only lets you put words in, not numbers.

int a = 10;
string b = a;

The above syntax will give you an error. So, to assign the value of a to b, you need to use the ToString() method.

string b = a.ToString(); // This turns the number into words.

The tostring () function belongs to the C# library named System.Object.

How to Overriding the ToString() in C#?

Let’s understand overriding in a very, very simple way. For complete fresher, I can say overriding means you have a rule for doing something already set up, but you decide, “Nah, I wanna do it my way.” I want to override/change the existing behavior of the function and apply my logic, we perform the operation called overriding.

To understand the Overriding in detail, first watch my video,

1. Index Based collection in C# in English

2. Index Based collection in C# in Hindi

In this video, I’ve kept it short and sweet, showing you how index-based collections work with method overriding for ToString().

Also Read: – What is Microservice: A Revolutionary Approach to Web Application Architecture in 2024

Leave a Comment