Scenario:
Per MSDN: Generics introduce the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operation.Solution:
T is available to the nested class. GenericList<string> instantiated with a concrete type, T becomes string
|
Calling GenericList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class GenericsClass { static void Main(string[] args) { GenricList<int> list = new GenricList<int>(); for (int i = 0; i < 5; i++) { list.AddHead(i); } foreach (var l in list) { Console.WriteLine($"{l} "); } Console.ReadLine(); } } |
No comments:
Post a Comment