C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# ToString Method: Returns The String Of The Object Instance

In C#, the ToString method is a member of the System.Object class, which means that it is inherited by all classes in the .NET Framework. The ToString method is used to return a string representation of an object, which can be useful for debugging, logging, or displaying information to users. In this tutorial, we'll cover the basics of working with the ToString method in C#.

  • Default implementation of ToString

By default, the ToString method returns the fully qualified name of the object's type. For example:

object obj = new object();
Console.WriteLine(obj.ToString()); // System.Object
  • Override ToString in custom classes

You can override the ToString method in your custom classes to provide a more meaningful string representation of the object. To do this, use the override keyword, and implement your custom logic in the method.

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return $"{FirstName} {LastName}";
    }
}

Person person = new Person { FirstName = "John", LastName = "Doe" };
Console.WriteLine(person.ToString()); // John Doe
  • ToString in built-in classes

Many built-in .NET classes provide their own implementation of the ToString method, which returns a string representation of the object's value or state. For example:

int number = 42;
Console.WriteLine(number.ToString()); // 42

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString()); // 04/29/2023 15:30:00 (example output)
  • Formatting with ToString

Some classes, like numeric and date/time types, provide overloaded versions of the ToString method that accept a format string or a format provider. This allows you to customize the string representation of the object.

double pi = 3.14159265359;
Console.WriteLine(pi.ToString("F2")); // 3.14

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd")); // 2023-04-29

In this tutorial, we covered the basics of working with the ToString method in C#. The ToString method is a member of the System.Object class and is used to return a string representation of an object. By default, it returns the fully qualified name of the object's type, but you can override the method in your custom classes to provide a more meaningful representation. Many built-in .NET classes provide their own implementation of the ToString method, and some classes even allow you to customize the string representation with format strings or format providers.

  1. How to use ToString in C#

    The ToString method is a fundamental method in C# that is defined in the System.Object class. It is used to obtain a string representation of an object.

    using System;
    
    class Program
    {
        static void Main()
        {
            int number = 42;
            string numberString = number.ToString();
    
            Console.WriteLine("Number as String: " + numberString);
        }
    }
    
  2. Object.ToString in C#

    Every class in C# is derived from the System.Object class, which includes the ToString method. By default, the ToString method returns the fully qualified name of the object's type.

    using System;
    
    class Program
    {
        static void Main()
        {
            object myObject = new object();
            string objectString = myObject.ToString();
    
            Console.WriteLine("Object as String: " + objectString);
        }
    }
    
  3. Customizing ToString method in C#

    You can override the ToString method in your custom classes to provide a more meaningful string representation.

    using System;
    
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        public override string ToString()
        {
            return $"{FirstName} {LastName}";
        }
    }
    
    class Program
    {
        static void Main()
        {
            Person person = new Person { FirstName = "John", LastName = "Doe" };
            string personString = person.ToString();
    
            Console.WriteLine("Person as String: " + personString);
        }
    }
    
  4. Overriding ToString in C#

    Overriding the ToString method allows you to provide a custom string representation for your objects.

    using System;
    
    class MyObject
    {
        public override string ToString()
        {
            return "Custom representation of MyObject";
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyObject myObject = new MyObject();
            string myObjectString = myObject.ToString();
    
            Console.WriteLine("MyObject as String: " + myObjectString);
        }
    }
    
  5. Default behavior of ToString in C#

    If you don't override the ToString method in your class, it will inherit the default behavior from the System.Object class, which returns the fully qualified name of the object's type.

    using System;
    
    class MyClass { }
    
    class Program
    {
        static void Main()
        {
            MyClass myObject = new MyClass();
            string myObjectString = myObject.ToString();
    
            Console.WriteLine("MyObject as String: " + myObjectString);
        }
    }
    
  6. ToString vs. other string conversion methods in C#

    using System;
    
    class Program
    {
        static void Main()
        {
            int number = 42;
    
            // Using ToString
            string numberString1 = number.ToString();
    
            // Using string interpolation
            string numberString2 = $"{number}";
    
            // Using Convert.ToString
            string numberString3 = Convert.ToString(number);
    
            Console.WriteLine("ToString: " + numberString1);
            Console.WriteLine("String Interpolation: " + numberString2);
            Console.WriteLine("Convert.ToString: " + numberString3);
        }
    }
    
  7. Formatting output with ToString in C#

    using System;
    
    class Temperature
    {
        public double Celsius { get; set; }
    
        public override string ToString()
        {
            return $"{Celsius}��C";
        }
    }
    
    class Program
    {
        static void Main()
        {
            Temperature temperature = new Temperature { Celsius = 25.5 };
            Console.WriteLine("Temperature: " + temperature);
        }
    }
    
  8. C# ToString method and string interpolation

    using System;
    
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        public override string ToString()
        {
            return $"{FirstName} {LastName}";
        }
    }
    
    class Program
    {
        static void Main()
        {
            Person person = new Person { FirstName = "John", LastName = "Doe" };
            Console.WriteLine($"Person: {person}");
        }
    }
    
  9. Using ToString with custom classes in C#

    using System;
    
    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    
        public override string ToString()
        {
            return $"{Name} - ${Price:F2}";
        }
    }
    
    class Program
    {
        static void Main()
        {
            Product product = new Product { Name = "Laptop", Price = 999.99 };
            Console.WriteLine("Product Details: " + product);
        }
    }
    
  10. ToString and culture-specific formatting in C#

    using System;
    using System.Globalization;
    
    class Temperature
    {
        public double Celsius { get; set; }
    
        public override string ToString()
        {
            // Use culture-specific formatting
            return $"{Celsius.ToString("N2", CultureInfo.CurrentCulture)}��C";
        }
    }
    
    class Program
    {
        static void Main()
        {
            Temperature temperature = new Temperature { Celsius = 25.5 };
            Console.WriteLine("Temperature: " + temperature);
        }
    }
    
  11. Handling null objects with ToString in C#

    using System;
    
    class MyClass
    {
        public override string ToString()
        {
            return "Custom representation of MyClass";
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass myObject = null;
            string myObjectString = myObject?.ToString() ?? "Object is null";
    
            Console.WriteLine("MyObject as String: " + myObjectString);
        }
    }
    
  12. ToString and debugging in C#

    using System;
    using System.Diagnostics;
    
    class MyClass
    {
        public string Name { get; set; }
        public int Age { get; set; }
    
        public override string ToString()
        {
            return $"Name: {Name}, Age: {Age}";
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass myObject = new MyClass { Name = "John", Age = 30 };
    
            // Debugging using ToString
            Debug.WriteLine(myObject.ToString());
        }
    }