Generic syntactic sugar or true improvement Generic syntactic sugar or true improvement asp.net asp.net

Generic syntactic sugar or true improvement


Generics are built into C#, so it is a "true improvement". Hence why run-time co-variance and contra-variance is possible, as well as reflection on generic types and run-time reflection-based creation of generic types (such as List<T> where T is determined at run-time).

This differs from C++, where templates are, in a lot of ways, syntactic sugar. The compiler actually generates code for each generic type you use - so Add<T> would create Add<int>, Add<long>, Add<short>, Add<MyClass>, and so on if you were using those functions, and likewise for classes. The benefit of this is primarily operators and a few other smaller things - if each of those types has a + operator, and Add<T>(T a, T b) returns a + b, all types will work just fine. C#'s compiler would complain because it can't/doesn't resolve the operator declaration for any-and-all types at compile time. Moreover, C# (not 100% sure, but maybe 90%) creates 1 generic type implementation for reference types (if you're using that implementation) and then 1 for each value type (so int, long, Decimal, MyStruct, etc all get their own implementations, as needed).


In java, generics are syntactic sugar but in c#, they are built in. look at this: www.25hoursaday.com/CsharpVsJava.html