https://msdn.microsoft.com/en-us/library/435f1dw2.aspx

When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class.

When you hide an inherited member, the derived version of the member replaces the base class version.

Although you can hide members without using the new modifier, you get a compiler warning.

If you use new to explicitly hide a member, it suppresses this warning.

To hide an inherited member, declare it in the derived class by using the same member name, and modify it with the new keyword.

For example:

public class BaseC
{
public int x;
public void Invoke() { }
}
public class DerivedC : BaseC
{
new public void Invoke() { }
}

In this example, BaseC.Invoke is hidden by DerivedC.Invoke.

The field x is not affected because it is not hidden by a similar name.

Name hiding through inheritance takes one of the following forms:

Generally, a constant, field, property, or type that is introduced in a class or struct hides all base class members that share its name.

There are special cases.

For example, if you declare a new field with name N to have a type that is not invocable, and a base type declares N to be a method, the new field does not hide the base declaration in invocation syntax.

See the C# language specification for details (see section "Member Lookup" in section "Expressions").

It is an error to use both new and override on the same member, because the two modifiers have mutually exclusive meanings. The new modifier creates a new member with the same name and causes the original member to become hidden. The override modifier extends the implementation for an inherited member.

Using the new modifier in a declaration that does not hide an inherited member generates a warning.

http://stackoverflow.com/questions/5709034/does-c-sharp-support-return-type-covariance/5709191#5709191

问题:

I'm working with the .NET framework and I really want to be able to make a custom type of page that all of my website uses.

The problem comes when I am trying to access the page from a control.

I want to be able to return my specific type of page instead of the default page.

Is there any way to do this?

public class MyPage : Page
{
// My own logic
} public class MyControl : Control
{
public MyPage Page { get; set; }
}

答案:

It sounds like what you want is return type covariance. C# does not support return type covariance.

Return type covariance is where you override a base class method that returns a less-specific type with one that returns a more specific type:

  class Animal
{
} class Fish : Animal
{
} abstract class Enclosure
{
public abstract Animal Contents();
}
class Aquarium : Enclosure
{
public new Fish Contents() { return new Fish(); }
}

This is safe because consumers of Contents via Enclosure expect an Animal, and Aquarium promises to not only fulfill that requirement, but moreover, to make a more strict promise: that the animal is always a fish.

//上面的代码会报错,因为返回类型不一致

This kind of covariance is not supported in C#, and is unlikely to ever be supported.

It is not supported by the CLR. (It is supported by C++, and by the C++/CLI implementation on the CLR; it does so by generating magical helper methods of the sort I suggest below.)

(Some languages support formal parameter type contravariance as well -- that you can override a method that takes a Fish with a method that takes an Animal. Again, the contract is fulfilled; the base class requires that any Fish be handled, and the derived class promises to not only handle fish, but any animal. Similarly, C# and the CLR do not support formal parameter type contravariance.)

The way you can work around this limitation is to do something like:

   class Animal
{
} class Fish : Animal
{
} abstract class Enclosure
{
protected abstract Animal GetContents(); //子类通过重写这个方法,来返回Fish实例 public Animal Contents()
{
return this.GetContents();
}
}
class Aquarium : Enclosure
{
protected override Animal GetContents()
{
return this.Contents();
} public new Fish Contents()
{
return new Fish();
}
}

Now you get both the benefits of overriding a virtual method, and getting stronger typing when using something of compile-time type Aquarium.

  Enclosure enclosure = new Aquarium();
Animal animal = enclosure.Contents();
Console.WriteLine(animal.GetType());

===2017年06月30日===更新

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords

https://dotnetfiddle.net/tXdyiG

using System;

namespace PolymorphismApplication
{
public abstract class Shape
{
protected int width, height;
public Shape(int a = , int b = )
{
width = a;
height = b;
} public virtual int area()
{
Console.WriteLine("Parent class area :");
return ;
} public virtual int func()
{
return ;
}
} public class Rectangle : Shape
{
public Rectangle(int a = , int b = ): base (a, b)
{
} public override int area()
{
Console.WriteLine("Rectangle class area :");
return (width * height);
} public new int func()
// if this class is implemented by an object declared on the base class
// e.g Shape newShape = new Rectancle(5, 10), then the object (newShape)
// will NOT inherit this method. It will stick to the one in Shape.
// Look at the implementation of Caller and the Program/Main to see example.
{
return ;
}
} public class Triangle : Shape
{
public Triangle(int a = , int b = ): base (a, b)
{
} public override int area()
{
Console.WriteLine("Triangle class area :");
return (width * height / );
} public override int func()
{
return ;
}
} public class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("Area: {0}", a);
int c = sh.func();
Console.WriteLine("func: {0}", c);
}
public void CallArea2 (Rectangle rect)
{
int a;
a = rect.area();
Console.WriteLine("Area: {0}", a);
int c = rect.func();
Console.WriteLine("func: {0}", c);
}
} public class Program
{
public void Main(string[] args)
{
Caller c = new Caller();
c.CallArea(new Rectangle(, )); // a rectangle is being passed to CallArea's SHAPE object-variable sh
c.CallArea(new Triangle(, ));
c.CallArea2(new Rectangle(, )); // a rectangle is being passed to CallArea2's RECTANGLE object-variable rect
// Takeaway: When you declare a rectangle as a reactangle, it will act differently from when you declare it as a shape.
// And this is because of the use of 'new' for the func method, which is only given to rects declared as Rectangles
// }
}
}

https://github.com/kerryjiang/IrvineCSharpCourses/blob/master/Example11/Program.cs

new Modifier (C# Reference)的更多相关文章

  1. Knowing When to Use Override and New Keywords (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173153.aspx In C#, a method in a derived class can have t ...

  2. 【转载】#273 - Parameter Modifier Summary

    Here's a summary of the different parameter modifiers and how the behavior changes for each, when us ...

  3. 解决Deprecated: preg_replace(): The /e modifier is deprecated, use

    使用php5.5运行ecshop的时候出现如下错误Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace ...

  4. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  5. Sphinx 2.2.11-release reference manual

    1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...

  6. C# to IL 6 Reference and Value Types(引用类型和值类型)

    An interface is a reference type, in spite of the fact that it has no code at all. Thus, wecannot in ...

  7. What the difference between __weak and __block reference?

    近日遇到一个非常细的知识点,关于block的循环引用问题.相比非常多人都遇到了.也能顺利攻克了,至于block方面的技术文章.那就很多其它了.这里不再赘述,可是有这样一个问题: What the di ...

  8. abstract (C# Reference)

    https://msdn.microsoft.com/en-us/library/sf985hc5.aspx The abstract modifier indicates that the thin ...

  9. protected (C# Reference)

    https://msdn.microsoft.com/en-us/library/bcd5672a.aspx The protected keyword is a member access modi ...

随机推荐

  1. 华登区块狗系统APP开发

    华登区块狗系统开发,陈翎:{.l8O..285l..l22O.}华登区块狗软件开发,华登区块狗APP开发,华登区块狗模式开发,华登区块狗现成源码,狗狗集市理财模式开发 华登区块狗是什么?华登区块狗ap ...

  2. CodeForces 【20C】Dijkstra?

    解题思路 heap+Dijkstra就能过.注意边是双向边,要用long long. 附上代码 #include <iostream> #include <queue> #in ...

  3. UVA - 1611 Crane (思路题)

    题目: 输入一个1~n(1≤n≤300)的排列,用不超过96次操作把它变成升序.每次操作都可以选一个长度为偶数的连续区间,交换前一半和后一半.输出每次操作选择的区间的第一个和最后一个元素. 思路: 注 ...

  4. 自定义属性Attribute的运用

    有时候需要一个枚举类,能够承载更多的信息,于是可以利用attribute这个特性. 首先编写自己业务需求类 [AttributeUsage(AttributeTargets.Field)] publi ...

  5. Python使用Flask框架,结合Highchart处理xml数据

    1.html代码 <!DOCTYPE html><html lang="en"><head>    <meta charset=" ...

  6. CentOS 6磁盘配额

    可以指定用户能超过其配额限制.如果不想拒绝用户对卷的访问但想跟踪每个用户的磁盘空间使用情况,启用配额而且不限制磁盘空间的使用是非常有用的.也可指定不管用户超过配额警告级别还是超过配额限制时是否要记录事 ...

  7. 【模板】51nod 1051 最大子矩阵和

    [题解] 二重循环枚举起始列和终止列,竖着往下加,转化为一个最大子段和问题,逐行累加即可. #include<cstdio> #include<cstring> #includ ...

  8. Codeforces Round #254 (Div. 1) C DZY Loves Colors

    http://codeforces.com/contest/444/problem/C 题意:给出一个数组,初始时每个值从1--n分别是1--n.  然后两种操作. 1:操作 a.b内的数字是a,b内 ...

  9. [51Nod1089] 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   回文串是指aba.abba.cccbccc.aaaa这种左右对称 ...

  10. [bzoj1572][Usaco2009 Open]工作安排Job_贪心_堆

    工作安排 Job bzoj-1572 Usaco-2009 Open 题目大意:题目链接. 注释:略. 想法: 我们将任务按照截止时间排序,枚举任务的同时顺便记录出已经做了多少任务(当前时间). 对于 ...