Domanda

Here is a very basic example of method overloading , two methods with the same name but with different signatures :

int MyMethod(int a)
int MyMethod(int a, string b)

Now let's say I define two generic interfaces, sharing the exact same name but with different number of type parameters, such as:

IMyInterface<T>
IMyInterface<T1,T2>

Can I say this represents "generic interface overloading" ? Or does the "overloading" term only applies to methods in such a context ? Still it looks kind of very similar to method overloading, in the sense that we are keeping an exact same name but varying the parameters.

If I can't say "generic interface overload/overloading" what can I say about these two different interfaces sharing the same name ?

Thanks and sorry if this is a dumb question, but googling arround "generic interface overload" or "generic interface overloading" doesn't give me much but results concerning interface methods overloading, which is not what I'm interested in.

È stato utile?

Soluzione

Types with the same name but a different number of generic type parameters (including zero) are simply different types. The term "overloading" does not apply here. Overloading really only applies to methods belonging to the same type and having the same name but different signatures.


It is very common to have a generic as well as a non-generic interface with the same name (example from the .NET Library):

public interface IList : ICollection, IEnumerable

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

They are just called generic and non-generic.


The .NET name of a generic type is the name of the type ending with a grave accent (`) and the number of type parameters. For example the type IMyType<T> in C# or IMyType(Of T) in VB is translated to

IMyType`1

internally. The <T> is really just a C# syntactic construction which is translated to an internal .NET name used by the CLR.

IMyType<T,U> would be translated to

IMyType`2

This shows clearly that types with the same name in C# differing only by their number of generic type parameters are in (CLR-) reality types with different names.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top