zoukankan      html  css  js  c++  java
  • C# Keyword usage virtual + override VS new

    A simple case:

    public class Foo
    {
         public /*virtual*/ bool DoSomething() { return false; }
    }
    
    public class Bar : Foo
    {
         public /*override or new*/ bool DoSomething() { return true; }
    }

    Call the code like this:

    Foo a = new Bar();
    a.DoSomething();

    NOTE: The important thing is that our object is actually a Bar, but we are storing it in a variable of type Foo (this is similar to casting it)

    Explain:

    New keyword is for Hiding. - means you are hiding your method at runtime. Output will be based base class method.

    Override for overriding. - means you are invoking your derived class method with the reference of base class. Output will be based on derived class method.

    Check out the  following link for more discussion:

    http://stackoverflow.com/questions/159978/c-sharp-keyword-usage-virtualoverride-vs-new

  • 相关阅读:
    html
    头部标签
    ajax
    分辨率
    js 运动基础
    js DOM
    js定时器
    js数组
    js基础
    例子:js简易日历
  • 原文地址:https://www.cnblogs.com/freecodeX/p/4240975.html
Copyright © 2011-2022 走看看