Skip to content

Commit 1704b3b

Browse files
authored
Modernise snippets showing use of generics in C# (#43678)
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 <Snippet> 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
1 parent b0c857b commit 1704b3b

File tree

1 file changed

+32
-50
lines changed
  • docs/csharp/fundamentals/types/snippets/generics

1 file changed

+32
-50
lines changed
Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,66 @@
1-
namespace generics
1+
namespace generics
22
{
33
//---------------------------------------------------------------------------
44
//<Snippet1>
55
// Declare the generic class.
66
public class GenericList<T>
77
{
8-
public void Add(T input) { }
8+
public void Add(T item) { }
99
}
10+
11+
public class ExampleClass { }
12+
1013
class TestGenericList
1114
{
12-
private class ExampleClass { }
1315
static void Main()
1416
{
15-
// Declare a list of type int.
16-
GenericList<int> list1 = new GenericList<int>();
17+
// Create a list of type int.
18+
GenericList<int> list1 = new();
1719
list1.Add(1);
1820

19-
// Declare a list of type string.
20-
GenericList<string> list2 = new GenericList<string>();
21+
// Create a list of type string.
22+
GenericList<string> list2 = new();
2123
list2.Add("");
2224

23-
// Declare a list of type ExampleClass.
24-
GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
25+
// Create a list of type ExampleClass.
26+
GenericList<ExampleClass> list3 = new();
2527
list3.Add(new ExampleClass());
2628
}
2729
}
2830
//</Snippet1>
2931
namespace SecondExample
3032
{
3133
//<Snippet2>
32-
// type parameter T in angle brackets
34+
// Type parameter T in angle brackets.
3335
public class GenericList<T>
3436
{
35-
// The nested class is also generic on T.
36-
private class Node
37+
// The nested class is also generic, and
38+
// holds a data item of type T.
39+
private class Node(T t)
3740
{
38-
// T used in non-generic constructor.
39-
public Node(T t)
40-
{
41-
next = null;
42-
data = t;
43-
}
44-
45-
private Node? next;
46-
public Node? Next
47-
{
48-
get { return next; }
49-
set { next = value; }
50-
}
51-
52-
// T as private member data type.
53-
private T data;
41+
// T as property type.
42+
public T Data { get; set; } = t;
5443

55-
// T as return type of property.
56-
public T Data
57-
{
58-
get { return data; }
59-
set { data = value; }
60-
}
44+
public Node? Next { get; set; }
6145
}
6246

47+
// First item in the linked list
6348
private Node? head;
6449

65-
// constructor
66-
public GenericList()
67-
{
68-
head = null;
69-
}
70-
71-
// T as method parameter type:
50+
// T as parameter type.
7251
public void AddHead(T t)
7352
{
74-
Node n = new Node(t);
53+
Node n = new(t);
7554
n.Next = head;
7655
head = n;
7756
}
7857

58+
// T in method return type.
7959
public IEnumerator<T> GetEnumerator()
8060
{
8161
Node? current = head;
8262

83-
while (current != null)
63+
while (current is not null)
8464
{
8565
yield return current.Data;
8666
current = current.Next;
@@ -92,27 +72,29 @@ public IEnumerator<T> GetEnumerator()
9272
namespace ThirdExample
9373
{
9474
using SecondExample;
95-
//<Snippet3>
9675
class TestGenericList
9776
{
9877
static void Main()
9978
{
100-
// int is the type argument
101-
GenericList<int> list = new GenericList<int>();
79+
//<Snippet3>
80+
// A generic list of int.
81+
GenericList<int> list = new();
10282

83+
// Add ten int values.
10384
for (int x = 0; x < 10; x++)
10485
{
10586
list.AddHead(x);
10687
}
10788

89+
// Write them to the console.
10890
foreach (int i in list)
10991
{
110-
System.Console.Write(i + " ");
92+
Console.WriteLine(i);
11193
}
112-
System.Console.WriteLine("\nDone");
94+
95+
Console.WriteLine("Done");
96+
//</Snippet3>
11397
}
11498
}
115-
//</Snippet3>
11699
}
117-
118100
}

0 commit comments

Comments
 (0)