简体   繁体   中英

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

Developing an interface generic I wished to declare a constructor in an interface but it says constructors are forbidden there. I've tried to declare a static factory method then, but it says neither static methods are allowed and suggests using 'new' keyword. But I have hardly any idea of what could 'new' keyword exactly mean when used inside an interface in C#. Have you?

UPDATE:

I didn't post any sample code because I didn't want to mix 2 questions - how to specify a constructor/factory in an interface AND what does the 'new' keyword mean in interfaces. II even was only forced to specify the first part because StackOverflow didn't accept the second question in pure form, saying it doesn't meet quality standards.

But, as you demand, I'll sample what I was trying to acheive:

Interface IMyInterface <T, U, V>
{
    IMyInterface (T, U, U);
    // OR
    static IMyInterface GetNewIMyInterface (T, U, U);
}

I just want every derived class to implement such a constructor.

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.

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

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, eg

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 Ba() 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?

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