Content deleted Content added
→Advantages and disadvantages: Class specialization |
m Add reference for to show that .NET generics do not apply type erasure |
||
Line 381:
====Genericity in .NET [C#, VB.NET]====
Generics were added as part of [[.NET Framework#.NET Framework 2.0|.NET Framework 2.0]] in November 2005, based on a research prototype from Microsoft Research started in 1999.<ref>[https://docs.microsoft.com/en-us/archive/blogs/dsyme/netc-generics-history-some-photos-from-feb-1999 .NET/C# Generics History: Some Photos From Feb 1999]</ref> Although similar to generics in Java, .NET generics do not apply [[type erasure]]<ref>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |page= 208-209 |isbn= 978-1-098-12195-2}}</ref>, but implement generics as a first class mechanism in the runtime using [[Reification (computer science)|reification]]. This design choice provides additional functionality, such as allowing [[Reflection (computer science)|reflection]] with preservation of generic types, as well as alleviating some of the limitations of erasure (such as being unable to create generic arrays).<ref>[https://www.ondotnet.com/pub/a/dotnet/2005/10/17/interview-with-anders-hejlsberg.html C#: Yesterday, Today, and Tomorrow: An Interview with Anders Hejlsberg]</ref><ref>[https://www.artima.com/intv/generics2.html Generics in C#, Java, and C++]</ref> This also means that there is no performance hit from runtime [[Type conversion|casts]] and normally expensive [[Boxing (computer science)|boxing conversions]]. When primitive and value types are used as generic arguments, they get specialized implementations, allowing for efficient generic [[Collection class|collections]] and methods. As in C++ and Java, nested generic types such as Dictionary<string, List<int>> are valid types, however are advised against for member signatures in code analysis design rules.<ref>[https://msdn.microsoft.com/en-us/library/ms182144.aspx Code Analysis CA1006: Do not nest generic types in member signatures]</ref>
.NET allows six varieties of generic type constraints using the <code>where</code> keyword including restricting generic types to be value types, to be classes, to have constructors, and to implement interfaces.<ref>[https://msdn2.microsoft.com/en-us/library/d5x73970.aspx Constraints on Type Parameters (C# Programming Guide)]</ref> Below is an example with an interface constraint:
Line 692:
== Sources ==
{{refbegin}}
* {{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |page= 208-209 |isbn= 978-1-098-12195-2}}
* {{Cite book | last1 = Musser | first1 = D. R. | author-link1 = David Musser | last2 = Stepanov | first2 = A. A. | author-link2 = Alexander Stepanov| chapter = Generic programming | doi = 10.1007/3-540-51084-2_2 | title = Symbolic and Algebraic Computation: International symposium ISSAC 1988 | editor = P. Gianni | editor-link = Patrizia Gianni | series = Lecture Notes in Computer Science | volume = 358 | pages = 13–25 | year = 1989 | isbn = 978-3-540-51084-0 }}
* {{cite conference | url =https://www.research.att.com/~bs/hopl-almost-final.pdf |title=Evolving a language in and for the real world: C++ 1991-2006 |author-link=Bjarne Stroustrup |last=Stroustrup |first=Bjarne |conference=ACM HOPL 2007 |year=2007}}
|