From ce358efd1d3a938dd6d207ff00f5f5f16c239fdf Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Thu, 21 Nov 2024 09:38:39 +1100 Subject: [PATCH] Modernise snippets showing use of generics in C# Use some newer language features to increase the signal/noise ratio in code samples used to describe generics in C#. - Make code shorter through auto properties. - Narrow scope of areas to remove boilerplate. - Use target-typed new. - Remove redundant constructor. Note we could go further with the language features here (such as collection initializers) but as this seems like a very fundamental level article I thought it best to use more explicit forms. These snippets are used in https://review.learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics?branch=main --- .../types/snippets/generics/Program.cs | 82 ++++++++----------- 1 file changed, 32 insertions(+), 50 deletions(-) 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"); + // } } - // } - }