What does 'new' keyword mean when used inside an interface in C#?

霸气de小男生 提交于 2019-12-02 20:18:40

The new keyword tells the compiler that your definition hides the definition contained in interfaces your interface might be extending.

Eric Lippert

Bala's answer is correct, but it might be helpful to see why you'd want to do this. Consider the problem that the BCL designers were faced with when designing the libraries for CLR version 2. There was an existing interface:

interface IEnumerable
{
  IEnumerator GetEnumerator();
}

Now you want to add:

interface IEnumerable<T> : IEnumerable
{
  new IEnumerator<T> GetEnumerator();
}

The new interface differs from the old solely in the return type.

What are your choices?

1) Mark the new GetEnumerator as "new" so that the compiler knows that this is intended to be the new method that does not conflict with the old method of the same name but different return type.

2) Change the name to GetEnumerator2.

3) Don't inherit from the original IEnumerable.

Options 2 and 3 are awful. Option 1 is awesome: new enumerables work seamlessly with code that expects old enumerables, but code written to use the new enumerables get the "new" generic behaviour by default.

You can't specify either a constructor or a static method in an interface... what you can do is add a type constraint for a generic type parameter, e.g.

void Foo<T>() where T : new()
{
    T t = new T();
    // Do stuff with t
}

Is that what you're thinking of?

An interface is supposed to specify a contract. It will only contain method signatures and no implementation. An interface cannot be instanciated directly, hence a constructor is not allowed in an interface.

http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx

You're using an interface, but it sounds like you want a base class instead. An interface should never need a constructor, as it can't contain any fields that would need to be initialized in the constructor. I think you want to use a base class instead.

First, the only thing the 'new' keyword actually does is prompt the compiler to NOT produce a warning that you should use the 'new' keyword. Apart from getting rid of the warning, the keyword itself does nothing (in the context in question).

Now, the compiler WANTS you to use the 'new' keyword when you redefine a member (property or method) already defined in an interface you inherit from, and there are at least two possible reasons for doing this. First, as Eric Lippert mentioned above, you may want a method to return a different type (or define a property of the same name with a different type). Another possibility is if you want to define different implementations for the two interfaces:

interface A
{
    void a();
}

interface B : A
{
    new void a();
}

class C : B
{
    void A.a() { Console.WriteLine("Called by interface A!"); }
    void B.a() { Console.WriteLine("Called by interface B!"); }
}

static class Test
{
    public static void DoTest()
    {
        B b = new C();
        b.a();       // Produces "Called by interface B!"
        ((A)b).a();  // Produces "Called by interface A!"
    }
}

If you try to define B.a() in C without redefining a() in B, you get a warning that it is not a member of interface: explicit interface declaration requires that you use an interface for which the member is explicitly defined.

C# and .NET does not allow you to declare a constructor on an interface.

It's simply got to be a stated constraint of implementing that interface (see ISerializable in the .NET libraries).

If you think about it, having a constructor on an interface doesn't make sense, as you have to know the concrete class you want to create when you call the constructor. How would you go about calling such an interface constructor, and what would the result be?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!