Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested contracts for generic interfaces

I can have a nested contracts type for a non-generic interface:

[ContractClass(typeof(Foo.FooContracts))]
public interface IFoo
{
    string Bar(object obj);
}

But it complains when I try to do the same thing with a generic interface:

[ContractClass(typeof(Foo.FooContracts<>))]
public interface IFoo<T>
{
    string Bar(T obj);
}

The warning is:

The contract class Foo+FooContracts`1 and the type IFoo`1 must have the same declaring type if any.

It compiles without a warning if I get FooContracts out of the Foo class.

  • Why does that limitation exist for generic interfaces?
  • Why doesn't that limitation exist for non-generic ones?
like image 490
Şafak Gür Avatar asked Sep 20 '13 09:09

Şafak Gür


People also ask

What is a generic interface?

Generic Interfaces in Java are the interfaces that deal with abstract data types. Interface help in the independent manipulation of java collections from representation details. They are used to achieving multiple inheritance in java forming hierarchies. They differ from the java class.

Can a generic implement an interface?

Only generic classes can implement generic interfaces. Normal classes can't implement generic interfaces.

Which interface defines methods to control the different generic collections?

NET class library defines several generic interfaces for use with the collection classes in the System. Collections. Generic namespace.

How can use generics with interface in C#?

You can declare variant generic interfaces by using the in and out keywords for generic type parameters. ref , in , and out parameters in C# cannot be variant. Value types also do not support variance. You can declare a generic type parameter covariant by using the out keyword.


1 Answers

The reason the limitation exists is that we need to copy contracts from the declaration point to the insertion points and that gets much more complicated if there are generic surrounding classes. There really is no need to have contract classes nested inside other types that I see.

like image 141
Manuel Fahndrich Avatar answered Nov 15 '22 19:11

Manuel Fahndrich