top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to override ToString() method in c#?

+4 votes
885 views

Is it possible to override this method? if yes then how can we achieve it?

posted Apr 21, 2014 by Merry

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

Yes it is possible to overwrite ToString method:

Step 1: Declare a ToString method with the following modifiers and return type:

public override string ToString(){}

Step 2: Implement the method so that it returns a string. The following example returns the name of the class in addition to the data specific to a particular instance of the class.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return "Person: " + Name + " " + Age;
    }
}
answer Apr 22, 2014 by Salil Agrawal
Similar Questions
+3 votes

By-default all the methods & variables in class are private.

In C#.net Main() method is private then how compiler access this Main() Method out side the class because compiler is not part of our program then how compiler access this Main() method?

Here we loose our data abstraction property ? Can anyone put some light on it?

+2 votes

What are some of the best ways to build XML in C# code?

+3 votes

I want get Number of indexed pages of any website so, it will give me exact number of pages in google.
What I have tried:

Uri queryUri = new Uri(string.Format("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:{0}", url));
using (WebClient client = new WebClient())
{
    string getString = client.DownloadString(queryUri);
    dynamic s = JsonConvert.DeserializeObject(getString);
    var IndexedPage = s["responseData"]["cursor"]["estimatedResultCount"].Value;
    lblIndexedPageRecv.Text = IndexedPage;
}

its working but not giving me right answer

my code is giving different values then other online webCheckers like one of this: http://www.pingler.com/seo-tools/tools/google-indexed-pages-checker

...