Generics
suggest changeSyntax
public void SomeMethod <T> () { }public void SomeMethod<T, V>() { }public T SomeMethod<T>(IEnumerable<T> sequence) { ... }public void SomeMethod<T>() where T : new() { }public void SomeMethod<T, V>() where T : new() where V : struct { }public void SomeMethod<T>() where T: IDisposable { }public void SomeMethod<T>() where T: Foo { }public class MyClass<T> { public T Data {get; set; } }
Remarks
Generics in C# are supported down to the runtime: generic types built with C# keep their generic semantics after compilation to CIL.
This effectively means that, in C#, it is possible to reflect on generic types and see them as they were declared or check if an object is an instance of a generic type, for example. This is in contrast with type erasure, where generic type information is removed during compilation. It is also in contrast with the template approach to generics, where multiple concrete generic types become multiple non-generic types at runtime, and any metadata required to further instantiate the original generic type definitions is lost.
When you reflect on a generic type, the runtime name replaces the angle brackets and the type-parameter names with a backtick and the number of type parameters. Dictionary<TKey, TValue> is therefore named Dictionary`2.