From the course: Learning C#

String representation - C# Tutorial

From the course: Learning C#

String representation

- [Instructor] In C#, every class implicitly inherits from the base object class, which you can find in the documentation at this link. Because of this inheritance, every class, both built-in and the ones that you create inherit the ToString function from the object class. And if I scroll down to the ToString method, you can find that here in the documentation at this link. The purpose of the ToString function is, as you would imagine, to return a string representation of the object that is suitable for display. So when you create your own classes, it's a good idea to override this method and generate your own string representation of your class. The built-in types use this method to display their data as strings. So let's jump over to Visual Studio Code, and let's see how this works. So here in the StringRep folder, I'm going to open up the Program code, as well as the Book code. So let's go back to our Program code for a second and let's try a simple example. So let's try out a built-in class. And let me collapse that down. I'll make an integer x equal to 1000. And then I'll just simply Console.WriteLine and I'm going to call x dot and then I'm going to call the ToString function. Now, if you don't override the method, then the default version just represents the fully qualified name of the class, along with the namespace that it's in. So for example, if I create an object and then I write this out, if I call a.ToString, now obviously, a is just a plain object, I haven't defined a class or anything like that, then the ToString function will print out the name of the class and its namespace. So let's go ahead and run this code. And you can see there in the output that I have the 1000 value from the integer variable, along with System.Object, which is the string representation of the plain object. So let's actually implement this method in our own Book class. So I'm going to go ahead and hide this for a moment. And let's go back to our Book class. And you can see that we have already got some properties in our Book class here. So we have the Name, Author and PageCount. So I'm going to use the string function and it's a virtual method, so I can override it. So I'm going to write public and then override string ToString. And I'm going to return an interpolated string, which is going to say Book and then Name by Author. I'm using the keyword override here to override the default behavior in the base object class. So in our program code, we now can create this new Book object and then convert it to a string. So I'm going to Console.WriteLine, b1.ToString. So this function also gets called whenever the object is implicitly converted to a string. So the same thing would happen, let me copy and paste this, even if I didn't have the ToString method. Console.WriteLine is going to try to convert this to a string by calling the ToString method on it. So it's this implicit conversion. So let's go ahead and see what happens. And I'm going to go ahead and comment out the previous examples. So let's bring our terminal back up and let's run this again. And you can see that both the direct and implicit calls to the ToString method are resulting in the string being created. So we can also overload the ToString function to provide some additional formatting capabilities to our class. Let's close this and let's go back to our Book class. And what I'm going to do here is write public string ToString and this one's going to take a character parameter named format. Now, notice here, I'm not using the override keyword. The reason I'm not using the override keyword is because there is no base class version that takes a character parameter. So I'm not overriding it, I'm overloading it. So we'll see what that means in just a moment. So if the user passes in a b character, then what I'm going to do is return an interpolated string of the form Book and it's just going to be condensed into Name: Author. And if they pass an F instead, so if format is equal to F, then what I'm going to do is return a longer, more richly formatted string. So in this case, it'll say Name by Author, oops, put a little space in there. Name by Author is Pagecount pages. And then what I'm going to do is if they don't specify a character at all, I'll just simply return the base ToString function. So now let's go back and add some more code to our program. And what I'm going to do here is copy and paste this. So in this case, I'll call ToString with a B. And ToString with an F. So let's bring our terminal back up one more time. Oops, looks like I misspelled that. Yeah, actually it's PageCount. So let's run that again. And now we can see the more advanced formatting output down here. So again, you can learn more about the ToString function at that link I showed earlier in the docs and pay particular attention to the notes to inheritor section for some good practices to follow when using the ToString function.

Contents