简体   繁体   中英

override ToString() with override and new keyword

I am new in c#.confuse with this question.

I Have override ToString() with override and new keyword.both are give me same output.then what is the difference between both.

here is my Example

class A
{
    public new string ToString()
    {
        return "With New Keyword";
    }
}
class B
{
    public override string ToString()
    {
        return "With Override Keyword";
    }
}


class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();

        Console.WriteLine(a.ToString());
        Console.WriteLine(b.ToString());

        Console.Read();
    }

}

Output

With New Keyword

With Override Keyword

I know its a silly question. please anyone help me to give me difference between both methods.

I am not asking about difference between new and override key word.I want to know that difference between both method.In concept of Override Object methods.

It makes a difference when you do this:

object a = new A(); // notice the type of the variable here!
object b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());

a.ToString() will not call your implementation of ToString and will instead call object.ToString , which returns the fully qualified type name of the object. b.ToString() will call your implementation.

What you did in B is called overriding. What you did in A is called hiding . Hiding loses its effect when the compile time type of a variable is not that type anymore. Your implementation of ToString will only be called when the compile time type is A .

Learn more here .

You can easily google this.

From MSDN

In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class method, and the new modifier hides it. The difference is illustrated in the examples in this topic.

In a console application, declare the following two classes, BaseClass and DerivedClass. DerivedClass inherits from BaseClass.

In class B you are overriding ToString() method that is Object.ToString() because you have not inherited A . Now consider below example,

class A
{
    public string ReturnString()
    {
       return "ClassA::Method";
    }
}
class B : A
{
    public newstring ReturnString()
    {
        return "ClassB::Method";
    }
}


class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();

        Console.WriteLine(a.ToString());
        Console.WriteLine(b.ToString());

        Console.Read();
    }
}

Here you are hiding the method in class B as you have inherited Class A . So you will get the output as ClassA::Method for both method calls.

Now consider below example

class A
{
    public virtual string ReturnString()
    {
       return "ClassA::Method";
    }
}
class B : A
{
    public override string ReturnString()
    {
        return "ClassB::Method";
    }
}


class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();

        Console.WriteLine(a.ToString());
        Console.WriteLine(b.ToString());

        Console.Read();
    }
}

Here we have overridden the Class A's method in Class B . This means ClassB method is like extension or you can say it has different meaning than ClassA . You will get output like below

ClassA::Method
ClassB::Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM