Results 1 to 6 of 6

Thread: How to override ToString() method in C#?

  1. #1
    Join Date
    Nov 2009
    Posts
    57

    How to override ToString() method in C#?

    Hello friends,
    I am last year B.Sc.I.T. student. I recently started learning C# language. In our last lecture our sir teach us how to override Tostring() method, but I didn't understand it clearly. Can anyone tell me how to override ToString() method in C#? I want example of it. Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to override ToString() method in C#?

    As per my information to change some characters or some words in the ToString implementation, you have to call all ready existing ToString method by using the base keyword. It is very simple to do this. I have written following code for you. Just try to understand it.
    Code:
    public override string ToString()
    {
        return bases.ToString(s).Replace("car", "I love bike");
    }
    Note: If you forget the base keyword then ToString( ) method will call itself repeatedly.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to override ToString() method in C#?

    Every object which is defined in C# language inherits the ToString method, which is used to returns a string representation of that object. Like in following example all variables of type int must use ToString method which return string:
    Code:
    int a = 42;
    string stra= a.ToString();
    System.Console.WriteLine(stra);

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to override ToString() method in C#?

    You can declare ToString method with the following modifiers:
    Code:
    public override string ToString(){}
    The following code return the data specific to a particular instance of the class. Just try to understand this code.
    Code:
    class PersonEg
    { 
      string names;
      int ages;
      SampleObject(string names, int ages)
      {
      this.names = names;
      this.ages = ages;
      }
      public override string ToString() 
      {
         string ss = ages.ToString();
         return "Persons: " + names + " " + ss;
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to override ToString() method in C#?

    Hey it is very simple. You have to just use " public override" to do this. I have written following code for you. Just try to understand how I used ToString() method. I have just write public override before ToString() method for accessing this method by any class.
    Code:
     public override string ToString() 
     { 
          // Write your own code
     }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to override ToString() method in C#?

    I have written following code to override ToString() method in C#. It is very simple to do this. The following code overrides the ToString method inherited from System.Object for displaying the private variables. Just try to understand it. This is very simple code.
    Code:
    public class MyClassEg
    {
       private string customers ="";
       private int customersIDs = 0;
     
       public string Customers
       {
          get { return customers; }
          set { customerss = values; }
       }
     
       public int CustomersIDs
       {
          get { return customersIDs; }
          set { customersIDs = values; }
       }
     
       public override string ToString()
       {
          return string.Format s("Customers = {0} ID = {1}", Customers, CustomersIDs);
       }
     
    }

Similar Threads

  1. Urgent Help with return method toString() Java!!
    By vizu in forum Software Development
    Replies: 3
    Last Post: 01-05-2011, 11:48 PM
  2. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:25 AM
  3. What is toString() method in java?
    By Juany in forum Software Development
    Replies: 4
    Last Post: 10-02-2010, 05:12 PM
  4. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  5. Permission denied to call method Location.toString
    By HiSpeed in forum Software Development
    Replies: 3
    Last Post: 15-05-2009, 02:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,061,650.23750 seconds with 16 queries