diff --git a/docs/csharp/fundamentals/types/snippets/generics/Program.cs b/docs/csharp/fundamentals/types/snippets/generics/Program.cs index 62c20cc46de58..48ab58e2aa157 100644 --- a/docs/csharp/fundamentals/types/snippets/generics/Program.cs +++ b/docs/csharp/fundamentals/types/snippets/generics/Program.cs @@ -1,27 +1,29 @@ -namespace generics +namespace generics { //--------------------------------------------------------------------------- // // Declare the generic class. public class GenericList { - public void Add(T input) { } + public void Add(T item) { } } + + public class ExampleClass { } + class TestGenericList { - private class ExampleClass { } static void Main() { - // Declare a list of type int. - GenericList list1 = new GenericList(); + // Create a list of type int. + GenericList list1 = new(); list1.Add(1); - // Declare a list of type string. - GenericList list2 = new GenericList(); + // Create a list of type string. + GenericList list2 = new(); list2.Add(""); - // Declare a list of type ExampleClass. - GenericList list3 = new GenericList(); + // Create a list of type ExampleClass. + GenericList list3 = new(); list3.Add(new ExampleClass()); } } @@ -29,58 +31,36 @@ static void Main() namespace SecondExample { // - // type parameter T in angle brackets + // Type parameter T in angle brackets. public class GenericList { - // The nested class is also generic on T. - private class Node + // The nested class is also generic, and + // holds a data item of type T. + private class Node(T t) { - // T used in non-generic constructor. - public Node(T t) - { - next = null; - data = t; - } - - private Node? next; - public Node? Next - { - get { return next; } - set { next = value; } - } - - // T as private member data type. - private T data; + // T as property type. + public T Data { get; set; } = t; - // T as return type of property. - public T Data - { - get { return data; } - set { data = value; } - } + public Node? Next { get; set; } } + // First item in the linked list private Node? head; - // constructor - public GenericList() - { - head = null; - } - - // T as method parameter type: + // T as parameter type. public void AddHead(T t) { - Node n = new Node(t); + Node n = new(t); n.Next = head; head = n; } + // T in method return type. public IEnumerator GetEnumerator() { Node? current = head; - while (current != null) + while (current is not null) { yield return current.Data; current = current.Next; @@ -92,27 +72,29 @@ public IEnumerator GetEnumerator() namespace ThirdExample { using SecondExample; - // class TestGenericList { static void Main() { - // int is the type argument - GenericList list = new GenericList(); + // + // A generic list of int. + GenericList list = new(); + // Add ten int values. for (int x = 0; x < 10; x++) { list.AddHead(x); } + // Write them to the console. foreach (int i in list) { - System.Console.Write(i + " "); + Console.WriteLine(i); } - System.Console.WriteLine("\nDone"); + + Console.WriteLine("Done"); + // } } - // } - }