Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic interface overloading. Valid terminology?

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.

like image 898
darkey Avatar asked Dec 19 '12 01:12

darkey


People also ask

Can generic methods be overloaded?

A generic method can also be overloaded by nongeneric methods. When the compiler encounters a method call, it searches for the method declaration that best matches the method name and the argument types specified in the call—an error occurs if two or more overloaded methods both could be considered best ...

What does generic interface mean?

A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. It can be returned from a method. It can be passed as argument.

Can we use generics in interface?

Generics make a class, interface and, method, consider all (reference) types that are given dynamically as parameters. This ensures type safety. Generic class parameters are specified in angle brackets “<>” after the class name as of the instance variable. Generic constructors are the same as generic methods.

Which of the following syntax is used to declare a generic interface?

The general syntax to declare a generic interface is as follows: interface interface-name<T> { void method-name(T t); // public abstract method. } In the above syntax, <T> is called a generic type parameter that specifies any data type used in the interface.


1 Answers

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 (&grave;) 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.

like image 116
Olivier Jacot-Descombes Avatar answered Oct 17 '22 20:10

Olivier Jacot-Descombes