From a710bcbc6a6160b67b5c22ef7590e49c63ac0f15 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 18 Aug 2026 17:19:35 -0700 Subject: [PATCH 1/3] Modernize C# snippets --- .../ComparerT/Overview/program.cs | 52 ++--- .../DictionaryTKey,TValue/.ctor/Program.cs | 5 + .../.ctor/Project.csproj | 6 + .../DictionaryTKey,TValue/.ctor/source.cs | 21 +- .../DictionaryTKey,TValue/.ctor/source1.cs | 21 +- .../DictionaryTKey,TValue/.ctor/source2.cs | 21 +- .../DictionaryTKey,TValue/.ctor/source3.cs | 21 +- .../DictionaryTKey,TValue/.ctor/source4.cs | 21 +- .../DictionaryTKey,TValue/Overview/Program.cs | 2 + .../Overview/Project.csproj | 6 + .../DictionaryTKey,TValue/Overview/source.cs | 27 +-- .../DictionaryTKey,TValue/Overview/source2.cs | 23 +- .../source.cs | 31 +-- .../source.cs | 23 +- .../source.cs | 25 +-- .../source.cs | 23 +- .../source.cs | 27 +-- .../source.cs | 23 +- .../source.cs | 27 +-- .../EqualityComparerT/Default/cs.csproj | 4 - .../EqualityComparerT/Default/program.cs | 18 +- .../EqualityComparerT/Overview/program.cs | 24 +-- .../HashSetT/.ctor/Program.cs | 4 +- .../HashSetT/.ctor/Project.csproj | 6 + .../HashSetT/.ctor/RunExamples.cs | 2 + .../HashSetT/.ctor/source2.cs | 10 +- .../HashSetT/Clear/Program.cs | 2 +- .../HashSetT/Contains/Program.cs | 8 +- .../HashSetT/IsProperSubsetOf/Program.cs | 6 +- .../HashSetT/Overview/Program.cs | 6 +- .../HashSetT/SymmetricExceptWith/Program.cs | 4 +- .../ICollectionT/Overview/program.cs | 31 +-- .../Overview/Program.cs | 2 + .../Overview/Project.csproj | 6 + .../IDictionaryTKey,TValue/Overview/source.cs | 27 +-- .../Overview/source2.cs | 21 +- .../IEnumerableT/Overview/program.cs | 98 +++++---- .../LinkedListNodeT/Overview/llnctor.cs | 91 ++++---- .../LinkedListT/.ctor/llctor.cs | 4 +- .../LinkedListT/Overview/source.cs | 36 ++-- .../ListT/.ctor/RunExamples.cs | 2 + .../ListT/.ctor/source.cs | 4 +- .../ListT/.ctor/source1.cs | 198 +++++++++--------- .../ListT/BinarySearch/Program.cs | 3 + .../ListT/BinarySearch/Project.csproj | 6 + .../ListT/BinarySearch/source.cs | 27 +-- .../ListT/BinarySearch/source1.cs | 24 ++- .../ListT/BinarySearch/source2.cs | 33 +-- .../ListT/Capacity/program.cs | 2 +- .../ListT/Contains/program.cs | 20 +- .../ListT/Contains/program1.cs | 20 +- .../ListT/CopyTo/source.cs | 19 +- .../ListT/Exists/source.cs | 29 +-- .../ListT/Find/program.cs | 18 +- .../ListT/FindIndex/FindIndex1.cs | 78 +++---- .../ListT/FindIndex/FindIndex2.cs | 78 +++---- .../ListT/FindIndex/FindIndex3.cs | 82 ++++---- .../ListT/FindIndex/Program.cs | 3 + .../ListT/FindIndex/Project.csproj | 6 + .../ListT/IndexOf/source.cs | 21 +- .../ListT/LastIndexOf/source.cs | 21 +- .../ListT/Overview/Project.csproj | 6 + .../ListT/Overview/RunExamples.cs | 2 + .../ListT/Overview/program.cs | 133 ++++++------ .../ListT/Overview/source.cs | 18 +- .../ListT/Reverse/source.cs | 23 +- .../ListT/Sort/Project.csproj | 6 + .../ListT/Sort/RunExamples.cs | 2 + .../ListT/Sort/Sort1.cs | 50 ++--- .../ListT/Sort/program.cs | 26 ++- .../QueueT/Overview/source.cs | 12 +- .../.ctor/Program.cs | 3 + .../.ctor/Project.csproj | 6 + .../.ctor/source.cs | 21 +- .../.ctor/source1.cs | 21 +- .../.ctor/source2.cs | 24 +-- .../Overview/source.cs | 23 +- .../source.cs | 31 +-- .../SortedListTKey,TValue/.ctor/Program.cs | 5 + .../.ctor/Project.csproj | 6 + .../SortedListTKey,TValue/.ctor/source.cs | 21 +- .../SortedListTKey,TValue/.ctor/source1.cs | 21 +- .../SortedListTKey,TValue/.ctor/source2.cs | 22 +- .../SortedListTKey,TValue/.ctor/source3.cs | 21 +- .../SortedListTKey,TValue/.ctor/source4.cs | 21 +- .../SortedListTKey,TValue/Overview/Program.cs | 2 + .../Overview/Project.csproj | 6 + .../SortedListTKey,TValue/Overview/remarks.cs | 25 +-- .../SortedListTKey,TValue/Overview/source.cs | 27 +-- .../source.cs | 31 +-- .../SortedSetT/Overview/program.cs | 16 +- .../StackT/Overview/source.cs | 12 +- 92 files changed, 1164 insertions(+), 988 deletions(-) create mode 100644 snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/HashSetT/.ctor/RunExamples.cs create mode 100644 snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/ListT/.ctor/RunExamples.cs create mode 100644 snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/ListT/FindIndex/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/ListT/FindIndex/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/ListT/Overview/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/ListT/Overview/RunExamples.cs create mode 100644 snippets/csharp/System.Collections.Generic/ListT/Sort/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs create mode 100644 snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Project.csproj create mode 100644 snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Program.cs create mode 100644 snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Project.csproj diff --git a/snippets/csharp/System.Collections.Generic/ComparerT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/ComparerT/Overview/program.cs index 6582bc30266..6c0e1395532 100644 --- a/snippets/csharp/System.Collections.Generic/ComparerT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/ComparerT/Overview/program.cs @@ -8,27 +8,29 @@ class Program static void Main(string[] args) { // - List Boxes = new List(); - Boxes.Add(new Box(4, 20, 14)); - Boxes.Add(new Box(12, 12, 12)); - Boxes.Add(new Box(8, 20, 10)); - Boxes.Add(new Box(6, 10, 2)); - Boxes.Add(new Box(2, 8, 4)); - Boxes.Add(new Box(2, 6, 8)); - Boxes.Add(new Box(4, 12, 20)); - Boxes.Add(new Box(18, 10, 4)); - Boxes.Add(new Box(24, 4, 18)); - Boxes.Add(new Box(10, 4, 16)); - Boxes.Add(new Box(10, 2, 10)); - Boxes.Add(new Box(6, 18, 2)); - Boxes.Add(new Box(8, 12, 4)); - Boxes.Add(new Box(12, 10, 8)); - Boxes.Add(new Box(14, 6, 6)); - Boxes.Add(new Box(16, 6, 16)); - Boxes.Add(new Box(2, 8, 12)); - Boxes.Add(new Box(4, 24, 8)); - Boxes.Add(new Box(8, 6, 20)); - Boxes.Add(new Box(18, 18, 12)); + List Boxes = + [ + new Box(4, 20, 14), + new Box(12, 12, 12), + new Box(8, 20, 10), + new Box(6, 10, 2), + new Box(2, 8, 4), + new Box(2, 6, 8), + new Box(4, 12, 20), + new Box(18, 10, 4), + new Box(24, 4, 18), + new Box(10, 4, 16), + new Box(10, 2, 10), + new Box(6, 18, 2), + new Box(8, 12, 4), + new Box(12, 10, 8), + new Box(14, 6, 6), + new Box(16, 6, 16), + new Box(2, 8, 12), + new Box(4, 24, 8), + new Box(8, 6, 20), + new Box(18, 18, 12), + ]; // Sort by an Comparer implementation that sorts // first by the length. @@ -72,12 +74,12 @@ static void Main(string[] args) // compares first by the length. // Returns -1 because the length of BoxA // is less than the length of BoxB. - BoxLengthFirst LengthFirst = new BoxLengthFirst(); + BoxLengthFirst LengthFirst = new(); - Comparer bc = (Comparer) LengthFirst; + Comparer bc = (Comparer)LengthFirst; - Box BoxA = new Box(2, 6, 8); - Box BoxB = new Box(10, 12, 14); + Box BoxA = new(2, 6, 8); + Box BoxB = new(10, 12, 14); int x = LengthFirst.Compare(BoxA, BoxB); Console.WriteLine(); Console.WriteLine(x.ToString()); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Program.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Program.cs new file mode 100644 index 00000000000..535394f65eb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Program.cs @@ -0,0 +1,5 @@ +DictionaryConstructorExample1.Run(); +DictionaryConstructorExample2.Run(); +DictionaryConstructorExample3.Run(); +DictionaryConstructorExample4.Run(); +DictionaryConstructorExample5.Run(); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Project.csproj b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source.cs index 82dfc935115..d0ef96e5941 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source.cs @@ -2,20 +2,21 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryConstructorExample1 { - public static void Main() + public static void Run() { // Create a new sorted dictionary of strings, with string // keys. SortedDictionary openWith = - new SortedDictionary(); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new SortedDictionary() + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Create a Dictionary of strings with string keys, and // initialize it with the contents of the sorted dictionary. @@ -24,7 +25,7 @@ public static void Main() // List the contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source1.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source1.cs index fb4604008d6..2e18f6c35b7 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source1.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source1.cs @@ -2,21 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryConstructorExample2 { - public static void Main() + public static void Run() { // Create a new sorted dictionary of strings, with string // keys and a case-insensitive comparer. SortedDictionary openWith = new SortedDictionary( - StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("Bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "Bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Create a Dictionary of strings with string keys and a // case-insensitive equality comparer, and initialize it @@ -27,7 +28,7 @@ public static void Main() // List the contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source2.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source2.cs index 103530a2b0e..b69908554b1 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source2.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source2.cs @@ -2,21 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryConstructorExample3 { - public static void Main() + public static void Run() { // Create a new Dictionary of strings, with string keys // and a case-insensitive comparer for the current culture. Dictionary openWith = new Dictionary( - StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Try to add a fifth element with a key that is the same // except for case; this would be allowed with the default @@ -32,7 +33,7 @@ public static void Main() // List the contents of the sorted dictionary. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source3.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source3.cs index 9db7785c3f3..6beba992c05 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source3.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source3.cs @@ -2,24 +2,25 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryConstructorExample4 { - public static void Main() + public static void Run() { // Create a new dictionary of strings, with string keys and // an initial capacity of 4. Dictionary openWith = - new Dictionary(4); - - // Add 4 elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary(4) + { + // Add 4 elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // List the contents of the dictionary. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source4.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source4.cs index 079e83ffc5f..4bf6dd39ae2 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source4.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source4.cs @@ -2,22 +2,23 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryConstructorExample5 { - public static void Main() + public static void Run() { // Create a new dictionary of strings, with string keys, an // initial capacity of 5, and a case-insensitive equality // comparer. Dictionary openWith = new Dictionary(5, - StringComparer.CurrentCultureIgnoreCase); - - // Add 4 elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add 4 elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Try to add a fifth element with a key that is the same // except for case; this would be allowed with the default @@ -33,7 +34,7 @@ public static void Main() // List the contents of the dictionary. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Program.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Program.cs new file mode 100644 index 00000000000..2567e51fffa --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Program.cs @@ -0,0 +1,2 @@ +DictionaryOverviewExample1.Run(); +DictionaryOverviewExample2.Run(); diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source.cs index f99ad7dbf00..52858180b5f 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source.cs @@ -1,23 +1,24 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryOverviewExample1 { - public static void Main() + public static void Run() { // // // Create a new dictionary of strings, with string keys. // Dictionary openWith = - new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new() + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // The Add method throws an exception if the new key is // already in the dictionary. @@ -92,7 +93,7 @@ public static void Main() // When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); @@ -107,7 +108,7 @@ public static void Main() // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); - foreach( string s in valueColl ) + foreach (string s in valueColl) { Console.WriteLine("Value = {0}", s); } @@ -121,7 +122,7 @@ public static void Main() // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary keys. Console.WriteLine(); - foreach( string s in keyColl ) + foreach (string s in keyColl) { Console.WriteLine("Key = {0}", s); } diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source2.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source2.cs index cdd0529456e..0c7ce002259 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/source2.cs @@ -1,24 +1,25 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryOverviewExample2 { - public static void Main() + public static void Run() { // Create a new dictionary of strings, with string keys. // Dictionary myDictionary = - new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - myDictionary.Add("txt", "notepad.exe"); - myDictionary.Add("bmp", "paint.exe"); - myDictionary.Add("dib", "paint.exe"); - myDictionary.Add("rtf", "wordpad.exe"); + new() + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // - foreach( KeyValuePair kvp in myDictionary ) + foreach (KeyValuePair kvp in myDictionary) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs index 4f539ba0d71..bdd57e5ed58 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs @@ -31,18 +31,19 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // -// + // // try { @@ -132,7 +133,7 @@ public static void Main() // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. Console.WriteLine(); - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); @@ -148,7 +149,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Value = {0}", s); } @@ -163,7 +164,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Key = {0}", s); } @@ -180,7 +181,7 @@ public static void Main() Console.WriteLine("Key \"dib\" is not found."); } // -// + // } } // diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Contains/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Contains/source.cs index 63bb1892951..5265f7066a9 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Contains/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Contains/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,16 +9,17 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Contains can be used to test keys before inserting // them. diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.GetEnumerator/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.GetEnumerator/source.cs index 35597994841..44fe9223de8 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.GetEnumerator/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.GetEnumerator/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,21 +9,22 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // When you use foreach to enumerate dictionary elements // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine($"Key = {de.Key}, Value = {de.Value}"); } diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Item/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Item/source.cs index 0fafd516c19..aba6dddd5e4 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Item/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Item/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,16 +9,17 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // The Item property is another name for the indexer, so you // can omit its name when accessing elements. diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Keys/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Keys/source.cs index 7c63bcd7654..31542d7ffe3 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Keys/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Keys/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,16 +9,17 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // To get the keys alone, use the Keys property. ICollection icoll = openWith.Keys; @@ -28,7 +29,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine($"Key = {s}"); } @@ -37,7 +38,7 @@ public static void Main() // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. Console.WriteLine(); - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine($"Key = {de.Key}, Value = {de.Value}"); } diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Remove/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Remove/source.cs index c3af8991cc4..e31605ce68e 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Remove/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Remove/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,16 +9,17 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Use the Remove method to remove a key/value pair. No // exception is thrown if the wrong data type is supplied. diff --git a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Values/source.cs b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Values/source.cs index 72bd9b597c7..529f36487c4 100644 --- a/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Values/source.cs +++ b/snippets/csharp/System.Collections.Generic/DictionaryTKey,TValue/System.Collections.IDictionary.Values/source.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; @@ -9,16 +9,17 @@ public static void Main() // Create a new dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // To get the values alone, use the Values property. ICollection icoll = openWith.Values; @@ -28,7 +29,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine($"Value = {s}"); } @@ -37,7 +38,7 @@ public static void Main() // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. Console.WriteLine(); - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine($"Key = {de.Key}, Value = {de.Value}"); } diff --git a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/cs.csproj b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/cs.csproj index 1f74533ceb8..2c17b281ccc 100644 --- a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/cs.csproj +++ b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/cs.csproj @@ -4,8 +4,4 @@ Exe net10.0 - - - - diff --git a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/program.cs b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/program.cs index 0265a784606..a816b763f8f 100644 --- a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/program.cs +++ b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Default/program.cs @@ -5,19 +5,19 @@ static class Program { static void Main() { - var redBox = new Box(8, 8, 4); - var blueBox = new Box(6, 8, 4); - var greenBox = new Box(4, 8, 8); + Box redBox = new(8, 8, 4); + Box blueBox = new(6, 8, 4); + Box greenBox = new(4, 8, 8); - var boxes = new[] { redBox, blueBox, greenBox }; + Box[] boxes = new[] { redBox, blueBox, greenBox }; - var boxToFind = new Box(4, 8, 8); + Box boxToFind = new(4, 8, 8); - var foundByDimension = boxes.FindFirst(boxToFind); + Box foundByDimension = boxes.FindFirst(boxToFind); Console.WriteLine($"Found box {foundByDimension} by dimension."); - var foundByVolume = boxes.FindFirst(boxToFind, new BoxEqVolume()); + Box foundByVolume = boxes.FindFirst(boxToFind, new BoxEqVolume()); Console.WriteLine($"Found box {foundByVolume} by volume."); } @@ -30,7 +30,7 @@ public static T FindFirst( { comparer = comparer ?? EqualityComparer.Default; - foreach (var item in collection) + foreach (T item in collection) { if (comparer.Equals(item, itemToFind)) { @@ -92,4 +92,4 @@ public bool Equals(Box other) * Found box 4 x 8 x 8 by dimension. Found box 8 x 8 x 4 by volume. - */ \ No newline at end of file + */ diff --git a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Overview/program.cs index 5493c957fb1..e6c2f67f144 100644 --- a/snippets/csharp/System.Collections.Generic/EqualityComparerT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/EqualityComparerT/Overview/program.cs @@ -4,18 +4,18 @@ class Program { - static Dictionary boxes; + static Dictionary boxes; static void Main() { - BoxSameDimensions boxDim = new BoxSameDimensions(); + BoxSameDimensions boxDim = new(); boxes = new Dictionary(boxDim); Console.WriteLine("Boxes equality by dimensions:"); - Box redBox = new Box(8, 4, 8); - Box greenBox = new Box(8, 6, 8); - Box blueBox = new Box(8, 4, 8); - Box yellowBox = new Box(8, 8, 8); + Box redBox = new(8, 4, 8); + Box greenBox = new(8, 6, 8); + Box blueBox = new(8, 4, 8); + Box yellowBox = new(8, 8, 8); AddBox(redBox, "red"); AddBox(greenBox, "green"); AddBox(blueBox, "blue"); @@ -24,12 +24,12 @@ static void Main() Console.WriteLine(); Console.WriteLine("Boxes equality by volume:"); - BoxSameVolume boxVolume = new BoxSameVolume(); + BoxSameVolume boxVolume = new(); boxes = new Dictionary(boxVolume); - Box pinkBox = new Box(8, 4, 8); - Box orangeBox = new Box(8, 6, 8); - Box purpleBox = new Box(4, 8, 8); - Box brownBox = new Box(8, 8, 4); + Box pinkBox = new(8, 4, 8); + Box orangeBox = new(8, 6, 8); + Box purpleBox = new(4, 8, 8); + Box brownBox = new(8, 8, 4); AddBox(pinkBox, "pink"); AddBox(orangeBox, "orange"); AddBox(purpleBox, "purple"); @@ -119,4 +119,4 @@ A box equal to purple is already in the collection. A box equal to brown is already in the collection. * */ -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Program.cs index 638e1dd20be..3d6705b9a7e 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Program.cs @@ -2,9 +2,9 @@ using System; using System.Collections.Generic; -class Program +class HashSetConstructorExample1 { - static void Main() + public static void Run() { // HashSet lowNumbers = new HashSet(); diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Project.csproj b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/RunExamples.cs b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/RunExamples.cs new file mode 100644 index 00000000000..fca9c221dfb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/RunExamples.cs @@ -0,0 +1,2 @@ +HashSetConstructorExample1.Run(); +HashSetConstructorExample2.Run(); diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/source2.cs b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/source2.cs index 7415275b002..a29de9666ac 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/source2.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/.ctor/source2.cs @@ -1,17 +1,13 @@ using System; using System.Collections.Generic; -class Program +class HashSetConstructorExample2 { - public static void Main() + public static void Run() { // HashSet allVehicles = new HashSet(StringComparer.OrdinalIgnoreCase); - List someVehicles = new List(); - - someVehicles.Add("Planes"); - someVehicles.Add("Trains"); - someVehicles.Add("Automobiles"); + List someVehicles = ["Planes", "Trains", "Automobiles"]; // Add in the vehicles contained in the someVehicles list. allVehicles.UnionWith(someVehicles); diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/Clear/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/Clear/Program.cs index 320721119a2..9ab94995261 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/Clear/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/Clear/Program.cs @@ -7,7 +7,7 @@ static void Main() { // // - HashSet Numbers = new HashSet(); + HashSet Numbers = []; for (int i = 0; i < 10; i++) { diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/Contains/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/Contains/Program.cs index eb4c41abcc0..183f945d664 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/Contains/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/Contains/Program.cs @@ -7,9 +7,10 @@ class Example static void Main() { // - HashSet numbers = new HashSet(); + HashSet numbers = []; - for (int i = 0; i < 20; i++) { + for (int i = 0; i < 20; i++) + { numbers.Add(i); } @@ -23,7 +24,8 @@ static void Main() DisplaySet(numbers); // Check if the hash table contains 0 and, if so, remove it. - if (numbers.Contains(0)) { + if (numbers.Contains(0)) + { numbers.Remove(0); } Console.Write("numbers contains {0} elements: ", numbers.Count); diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/IsProperSubsetOf/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/IsProperSubsetOf/Program.cs index 8a4f5a07cef..9ea366c4c98 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/IsProperSubsetOf/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/IsProperSubsetOf/Program.cs @@ -7,8 +7,8 @@ class Program static void Main() { // - HashSet lowNumbers = new HashSet(); - HashSet allNumbers = new HashSet(); + HashSet lowNumbers = []; + HashSet allNumbers = []; for (int i = 1; i < 5; i++) { @@ -32,7 +32,7 @@ static void Main() Console.WriteLine("allNumbers and lowNumbers are equal sets: {0}", allNumbers.SetEquals(lowNumbers)); - // Show the results of sub/superset testing + // Show the results of sub/superset testing. Console.WriteLine("lowNumbers is a subset of allNumbers: {0}", lowNumbers.IsSubsetOf(allNumbers)); Console.WriteLine("allNumbers is a superset of lowNumbers: {0}", diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/Overview/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/Overview/Program.cs index 81527af7e64..2230316b60b 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/Overview/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/Overview/Program.cs @@ -8,8 +8,8 @@ static void Main() // // // - HashSet evenNumbers = new HashSet(); - HashSet oddNumbers = new HashSet(); + HashSet evenNumbers = []; + HashSet oddNumbers = []; for (int i = 0; i < 5; i++) { @@ -28,7 +28,7 @@ static void Main() DisplaySet(oddNumbers); // Create a new HashSet populated with even numbers. - HashSet numbers = new HashSet(evenNumbers); + HashSet numbers = [.. evenNumbers]; Console.WriteLine("numbers UnionWith oddNumbers..."); numbers.UnionWith(oddNumbers); diff --git a/snippets/csharp/System.Collections.Generic/HashSetT/SymmetricExceptWith/Program.cs b/snippets/csharp/System.Collections.Generic/HashSetT/SymmetricExceptWith/Program.cs index d7639bbeb1c..f52e5098f71 100644 --- a/snippets/csharp/System.Collections.Generic/HashSetT/SymmetricExceptWith/Program.cs +++ b/snippets/csharp/System.Collections.Generic/HashSetT/SymmetricExceptWith/Program.cs @@ -7,8 +7,8 @@ class Program static void Main() { // - HashSet lowNumbers = new HashSet(); - HashSet highNumbers = new HashSet(); + HashSet lowNumbers = []; + HashSet highNumbers = []; for (int i = 0; i < 6; i++) { diff --git a/snippets/csharp/System.Collections.Generic/ICollectionT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/ICollectionT/Overview/program.cs index ea297593deb..d758d640622 100644 --- a/snippets/csharp/System.Collections.Generic/ICollectionT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/ICollectionT/Overview/program.cs @@ -9,15 +9,15 @@ class Program static void Main(string[] args) { - BoxCollection bxList = new BoxCollection(); - - bxList.Add(new Box(10, 4, 6)); - bxList.Add(new Box(4, 6, 10)); - bxList.Add(new Box(6, 10, 4)); - bxList.Add(new Box(12, 8, 10)); - - // Same dimensions. Cannot be added: - bxList.Add(new Box(10, 4, 6)); + BoxCollection bxList = + [ + new Box(10, 4, 6), + new Box(4, 6, 10), + new Box(6, 10, 4), + new Box(12, 8, 10), + // Same dimensions. Cannot be added: + new Box(10, 4, 6), + ]; // Test the Remove method. Display(bxList); @@ -26,7 +26,7 @@ static void Main(string[] args) Display(bxList); // Test the Contains method. - Box BoxCheck = new Box(8, 12, 10); + Box BoxCheck = new(8, 12, 10); Console.WriteLine("Contains {0}x{1}x{2} by dimensions: {3}", BoxCheck.Height.ToString(), BoxCheck.Length.ToString(), BoxCheck.Width.ToString(), bxList.Contains(BoxCheck).ToString()); @@ -125,7 +125,7 @@ IEnumerator IEnumerable.GetEnumerator() public BoxCollection() { - innerCol = new List(); + innerCol = []; } // Adds an index to the collection. @@ -195,13 +195,14 @@ public void Clear() public void CopyTo(Box[] array, int arrayIndex) { if (array == null) - throw new ArgumentNullException("The array cannot be null."); + throw new ArgumentNullException("The array cannot be null."); if (arrayIndex < 0) - throw new ArgumentOutOfRangeException("The starting array index cannot be negative."); + throw new ArgumentOutOfRangeException("The starting array index cannot be negative."); if (Count > array.Length - arrayIndex) - throw new ArgumentException("The destination array has fewer elements than the collection."); + throw new ArgumentException("The destination array has fewer elements than the collection."); - for (int i = 0; i < innerCol.Count; i++) { + for (int i = 0; i < innerCol.Count; i++) + { array[i + arrayIndex] = innerCol[i]; } } diff --git a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Program.cs b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Program.cs new file mode 100644 index 00000000000..d7864b60367 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Program.cs @@ -0,0 +1,2 @@ +DictionaryInterfaceOverviewExample1.Run(); +DictionaryInterfaceOverviewExample2.Run(); diff --git a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source.cs b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source.cs index da0a30ac81a..ec39d0a1234 100644 --- a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source.cs @@ -2,22 +2,23 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryInterfaceOverviewExample1 { - public static void Main() + public static void Run() { // // Create a new dictionary of strings, with string keys, // and access it through the IDictionary generic interface. IDictionary openWith = - new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // The Add method throws an exception if the new key is // already in the dictionary. @@ -92,7 +93,7 @@ public static void Main() // When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); @@ -106,7 +107,7 @@ public static void Main() // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Value = {0}", s); } @@ -119,7 +120,7 @@ public static void Main() // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Key = {0}", s); } diff --git a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source2.cs b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source2.cs index d3cd2898cc0..0fd3995bb5b 100644 --- a/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Generic/IDictionaryTKey,TValue/Overview/source2.cs @@ -1,20 +1,21 @@ using System; using System.Collections.Generic; -public class Example +public class DictionaryInterfaceOverviewExample2 { - public static void Main() + public static void Run() { // Create a new dictionary of strings, with string keys. // - Dictionary exDictionary = new Dictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - exDictionary.Add(0, "notepad.exe"); - exDictionary.Add(1, "paint.exe"); - exDictionary.Add(2, "paint.exe"); - exDictionary.Add(3, "wordpad.exe"); + Dictionary exDictionary = new() + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + { 0, "notepad.exe" }, + { 1, "paint.exe" }, + { 2, "paint.exe" }, + { 3, "wordpad.exe" } + }; IDictionary myDictionary = exDictionary; // foreach (KeyValuePair kvp in myDictionary) diff --git a/snippets/csharp/System.Collections.Generic/IEnumerableT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/IEnumerableT/Overview/program.cs index 988b6bad18c..a129393205c 100644 --- a/snippets/csharp/System.Collections.Generic/IEnumerableT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/IEnumerableT/Overview/program.cs @@ -1,8 +1,8 @@ // using System; -using System.IO; using System.Collections; using System.Collections.Generic; +using System.IO; using System.Linq; public class App @@ -17,49 +17,54 @@ public static void Main() } public static void TestStreamReaderEnumerable() - { - // Check the memory before the iterator is used. - long memoryBefore = GC.GetTotalMemory(true); - IEnumerable stringsFound; - // Open a file with the StreamReaderEnumerable and check for a string. - try { - stringsFound = - from line in new StreamReaderEnumerable(@"c:\temp\tempFile.txt") - where line.Contains("string to search for") - select line; - Console.WriteLine("Found: " + stringsFound.Count()); - } - catch (FileNotFoundException) { - Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt."); - return; - } - - // Check the memory after the iterator and output it to the console. - long memoryAfter = GC.GetTotalMemory(false); - Console.WriteLine("Memory Used With Iterator = \t" + { + // Check the memory before the iterator is used. + long memoryBefore = GC.GetTotalMemory(true); + IEnumerable stringsFound; + // Open a file with the StreamReaderEnumerable and check for a string. + try + { + stringsFound = + from line in new StreamReaderEnumerable(@"c:\temp\tempFile.txt") + where line.Contains("string to search for") + select line; + Console.WriteLine("Found: " + stringsFound.Count()); + } + catch (FileNotFoundException) + { + Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt."); + return; + } + + // Check the memory after the iterator and output it to the console. + long memoryAfter = GC.GetTotalMemory(false); + Console.WriteLine("Memory Used With Iterator = \t" + string.Format(((memoryAfter - memoryBefore) / 1000).ToString(), "n") + "kb"); - } + } public static void TestReadingFile() - { - long memoryBefore = GC.GetTotalMemory(true); - StreamReader sr; - try { - sr = File.OpenText("c:\\temp\\tempFile.txt"); - } - catch (FileNotFoundException) { - Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt."); - return; - } + { + long memoryBefore = GC.GetTotalMemory(true); + StreamReader sr; + try + { + sr = File.OpenText("c:\\temp\\tempFile.txt"); + } + catch (FileNotFoundException) + { + Console.WriteLine(@"This example requires a file named C:\temp\tempFile.txt."); + return; + } // Add the file contents to a generic list of strings. - List fileContents = new List(); - while (!sr.EndOfStream) { - fileContents.Add(sr.ReadLine()); - } + List fileContents = []; + while (!sr.EndOfStream) + { + fileContents.Add(sr.ReadLine()); + } - // Check for the string. - var stringsFound = + // Check for the string. + IEnumerable stringsFound = from line in fileContents where line.Contains("string to search for") select line; @@ -67,11 +72,11 @@ where line.Contains("string to search for") sr.Close(); Console.WriteLine("Found: " + stringsFound.Count()); - // Check the memory after when the iterator is not used, and output it to the console. - long memoryAfter = GC.GetTotalMemory(false); - Console.WriteLine("Memory Used Without Iterator = \t" + + // Check the memory after when the iterator is not used, and output it to the console. + long memoryAfter = GC.GetTotalMemory(false); + Console.WriteLine("Memory Used Without Iterator = \t" + string.Format(((memoryAfter - memoryBefore) / 1000).ToString(), "n") + "kb"); - } + } } // A custom class that implements IEnumerable(T). When you implement IEnumerable(T), @@ -173,16 +178,17 @@ protected virtual void Dispose(bool disposing) // Dispose of managed resources. } _current = null; - if (_sr != null) { - _sr.Close(); - _sr.Dispose(); + if (_sr != null) + { + _sr.Close(); + _sr.Dispose(); } } this.disposedValue = true; } - ~StreamReaderEnumerator() + ~StreamReaderEnumerator() { Dispose(disposing: false); } diff --git a/snippets/csharp/System.Collections.Generic/LinkedListNodeT/Overview/llnctor.cs b/snippets/csharp/System.Collections.Generic/LinkedListNodeT/Overview/llnctor.cs index d485756dbf8..9fb73a93ac6 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListNodeT/Overview/llnctor.cs +++ b/snippets/csharp/System.Collections.Generic/LinkedListNodeT/Overview/llnctor.cs @@ -4,50 +4,53 @@ using System; using System.Collections.Generic; -public class GenericCollection { - - public static void Main() { - - // Create a new LinkedListNode of type String and displays its properties. - LinkedListNode lln = new LinkedListNode( "orange" ); - Console.WriteLine( "After creating the node ...." ); - DisplayProperties( lln ); - - // Create a new LinkedList. - LinkedList ll = new LinkedList(); - - // Add the "orange" node and display its properties. - ll.AddLast( lln ); - Console.WriteLine( "After adding the node to the empty LinkedList ...." ); - DisplayProperties( lln ); - - // Add nodes before and after the "orange" node and display the "orange" node's properties. - ll.AddFirst( "red" ); - ll.AddLast( "yellow" ); - Console.WriteLine( "After adding red and yellow ...." ); - DisplayProperties( lln ); - } - - public static void DisplayProperties( LinkedListNode lln ) { - if ( lln.List == null ) - Console.WriteLine( " Node is not linked." ); - else - Console.WriteLine( " Node belongs to a linked list with {0} elements.", lln.List.Count ); - - if ( lln.Previous == null ) - Console.WriteLine( " Previous node is null." ); - else - Console.WriteLine( " Value of previous node: {0}", lln.Previous.Value ); - - Console.WriteLine( " Value of current node: {0}", lln.Value ); - - if ( lln.Next == null ) - Console.WriteLine( " Next node is null." ); - else - Console.WriteLine( " Value of next node: {0}", lln.Next.Value ); - - Console.WriteLine(); - } +public class GenericCollection +{ + + public static void Main() + { + + // Create a new LinkedListNode of type String and displays its properties. + LinkedListNode lln = new("orange"); + Console.WriteLine("After creating the node ...."); + DisplayProperties(lln); + + // Create a new LinkedList. + LinkedList ll = new(); + + // Add the "orange" node and display its properties. + ll.AddLast(lln); + Console.WriteLine("After adding the node to the empty LinkedList ...."); + DisplayProperties(lln); + + // Add nodes before and after the "orange" node and display the "orange" node's properties. + ll.AddFirst("red"); + ll.AddLast("yellow"); + Console.WriteLine("After adding red and yellow ...."); + DisplayProperties(lln); + } + + public static void DisplayProperties(LinkedListNode lln) + { + if (lln.List == null) + Console.WriteLine(" Node is not linked."); + else + Console.WriteLine(" Node belongs to a linked list with {0} elements.", lln.List.Count); + + if (lln.Previous == null) + Console.WriteLine(" Previous node is null."); + else + Console.WriteLine(" Value of previous node: {0}", lln.Previous.Value); + + Console.WriteLine(" Value of current node: {0}", lln.Value); + + if (lln.Next == null) + Console.WriteLine(" Next node is null."); + else + Console.WriteLine(" Value of next node: {0}", lln.Next.Value); + + Console.WriteLine(); + } } diff --git a/snippets/csharp/System.Collections.Generic/LinkedListT/.ctor/llctor.cs b/snippets/csharp/System.Collections.Generic/LinkedListT/.ctor/llctor.cs index 3c530b4c53d..a3789d87e2f 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListT/.ctor/llctor.cs +++ b/snippets/csharp/System.Collections.Generic/LinkedListT/.ctor/llctor.cs @@ -10,7 +10,7 @@ public class GenericCollection public static void Main() { // Create and initialize a new LinkedList. - LinkedList ll = new LinkedList(); + LinkedList ll = new(); ll.AddLast("red"); ll.AddLast("orange"); ll.AddLast("yellow"); @@ -23,7 +23,7 @@ public static void Main() Console.WriteLine("The last item in the list is {0}.", ll.Last.Value); Console.WriteLine("The LinkedList contains:"); - foreach (String s in ll) + foreach (string s in ll) Console.WriteLine(" {0}", s); } else diff --git a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs index 9ddb194b4b7..6c69fd3ab4f 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs @@ -1,7 +1,7 @@ // using System; -using System.Text; using System.Collections.Generic; +using System.Text; public class Example { @@ -10,8 +10,8 @@ public static void Main() // // Create the link list. string[] words = - { "the", "fox", "jumps", "over", "the", "dog" }; - LinkedList sentence = new LinkedList(words); + ["the", "fox", "jumps", "over", "the", "dog"]; + LinkedList sentence = new(words); Display(sentence, "The linked list values:"); // @@ -135,7 +135,7 @@ public static void Main() Console.WriteLine("Test 17: linked list Contains 'jumps' = {0}", sentence.Contains("jumps")); // - + // // Release all the nodes. sentence.Clear(); @@ -169,7 +169,7 @@ private static void IndicateNode(LinkedListNode node, string test) return; } - StringBuilder result = new StringBuilder("(" + node.Value + ")"); + StringBuilder result = new("(" + node.Value + ")"); LinkedListNode nodeP = node.Previous; while (nodeP != null) @@ -196,28 +196,28 @@ private static void IndicateNode(LinkedListNode node, string test) //the fox jumps over the dog //Test 1: Add 'today' to beginning of the list: -//today the fox jumps over the dog +//today the fox jumps over the dog. //Test 2: Move first node to be last node: -//the fox jumps over the dog today +//the fox jumps over the dog today. //Test 3: Change the last node to 'yesterday': -//the fox jumps over the dog yesterday +//the fox jumps over the dog yesterday. //Test 4: Move last node to be first node: -//yesterday the fox jumps over the dog +//yesterday the fox jumps over the dog. //Test 5: Indicate last occurence of 'the': -//the fox jumps over (the) dog +//the fox jumps over (the) dog. //Test 6: Add 'lazy' and 'old' after 'the': -//the fox jumps over (the) lazy old dog +//the fox jumps over (the) lazy old dog. //Test 7: Indicate the 'fox' node: -//the (fox) jumps over the lazy old dog +//the (fox) jumps over the lazy old dog. //Test 8: Add 'quick' and 'brown' before 'fox': -//the quick brown (fox) jumps over the lazy old dog +//the quick brown (fox) jumps over the lazy old dog. //Test 9: Indicate the 'dog' node: //the quick brown fox jumps over the lazy old (dog) @@ -232,13 +232,13 @@ private static void IndicateNode(LinkedListNode node, string test) //Node 'dog' is not in the list. //Test 13: Add node removed in test 12 after a referenced node (brown): -//the quick brown (dog) jumps over the lazy old fox +//the quick brown (dog) jumps over the lazy old fox. //Test 14: Remove node that has the value 'old': -//the quick brown dog jumps over the lazy fox +//the quick brown dog jumps over the lazy fox. //Test 15: Remove last node, cast to ICollection, and add 'rhinoceros': -//the quick brown dog jumps over the lazy rhinoceros +//the quick brown dog jumps over the lazy rhinoceros. //Test 16: Copy the list to an array: //the @@ -249,9 +249,9 @@ private static void IndicateNode(LinkedListNode node, string test) //over //the //lazy -//rhinoceros +//rhinoceros. -//Test 17: linked list Contains 'jumps'= True +//Test 17: linked list Contains 'jumps'= True. //Test 18: Cleared linked list Contains 'jumps' = False // diff --git a/snippets/csharp/System.Collections.Generic/ListT/.ctor/RunExamples.cs b/snippets/csharp/System.Collections.Generic/ListT/.ctor/RunExamples.cs new file mode 100644 index 00000000000..7e4238e45e1 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/.ctor/RunExamples.cs @@ -0,0 +1,2 @@ +ListConstructorExample1.Run(); +ListConstructorExample2.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source.cs b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source.cs index 3295604acf7..dac12bcc088 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source.cs @@ -2,9 +2,9 @@ using System; using System.Collections.Generic; -public partial class Program +public class ListConstructorExample1 { - public static void Main() + public static void Run() { List animals = new List(4); diff --git a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs index c468b8f2b30..2129c52d600 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs @@ -1,100 +1,106 @@ -// -using System; +using System; using System.Collections.Generic; -string[] input = { "Apple", - "Banana", - "Orange" }; - -List fruits = new List(input); - -Console.WriteLine("\nCapacity: {0}", fruits.Capacity); -Console.WriteLine(); - -foreach (string fruit in fruits) -{ - Console.WriteLine(fruit); -} - -Console.WriteLine("\nAddRange(fruits)"); -fruits.AddRange(fruits); - -Console.WriteLine(); -foreach (string fruit in fruits) -{ - Console.WriteLine(fruit); -} - -Console.WriteLine("\nRemoveRange(2, 2)"); -fruits.RemoveRange(2, 2); - -Console.WriteLine(); -foreach (string fruit in fruits) -{ - Console.WriteLine(fruit); -} - -input = new string[] { "Mango", - "Pineapple", - "Watermelon" }; - -Console.WriteLine("\nInsertRange(3, input)"); -fruits.InsertRange(3, input); - -Console.WriteLine(); -foreach (string fruit in fruits) +public class ListConstructorExample2 { - Console.WriteLine(fruit); + public static void Run() + { + // + string[] input = [ "Apple", + "Banana", + "Orange" ]; + + List fruits = new List(input); + + Console.WriteLine("\nCapacity: {0}", fruits.Capacity); + Console.WriteLine(); + + foreach (string fruit in fruits) + { + Console.WriteLine(fruit); + } + + Console.WriteLine("\nAddRange(fruits)"); + fruits.AddRange(fruits); + + Console.WriteLine(); + foreach (string fruit in fruits) + { + Console.WriteLine(fruit); + } + + Console.WriteLine("\nRemoveRange(2, 2)"); + fruits.RemoveRange(2, 2); + + Console.WriteLine(); + foreach (string fruit in fruits) + { + Console.WriteLine(fruit); + } + + input = [ "Mango", + "Pineapple", + "Watermelon" ]; + + Console.WriteLine("\nInsertRange(3, input)"); + fruits.InsertRange(3, input); + + Console.WriteLine(); + foreach (string fruit in fruits) + { + Console.WriteLine(fruit); + } + + Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()"); + string[] output = [.. fruits.GetRange(2, 3)]; + + Console.WriteLine(); + foreach (string fruit in output) + { + Console.WriteLine(fruit); + } + + /* + This code example produces the following output: + + Capacity: 3 + + Apple + Banana + Orange + + AddRange(fruits) + + Apple + Banana + Orange + Apple + Banana + Orange + + RemoveRange(2, 2) + + Apple + Banana + Banana + Orange + + InsertRange(3, input) + + Apple + Banana + Banana + Mango + Pineapple + Watermelon + Orange + + output = fruits.GetRange(2, 3).ToArray() + + Banana + Mango + Pineapple + */ + // + } } - -Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()"); -string[] output = fruits.GetRange(2, 3).ToArray(); - -Console.WriteLine(); -foreach (string fruit in output) -{ - Console.WriteLine(fruit); -} - -/* - This code example produces the following output: - - Capacity: 3 - - Apple - Banana - Orange - - AddRange(fruits) - - Apple - Banana - Orange - Apple - Banana - Orange - - RemoveRange(2, 2) - - Apple - Banana - Banana - Orange - - InsertRange(3, input) - - Apple - Banana - Banana - Mango - Pineapple - Watermelon - Orange - - output = fruits.GetRange(2, 3).ToArray() - - Banana - Mango - Pineapple -*/ -// diff --git a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Program.cs b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Program.cs new file mode 100644 index 00000000000..9e29903f25a --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Program.cs @@ -0,0 +1,3 @@ +BinarySearchSnippet1.BinarySearchExample1.Run(); +BinarySearchSnippet2.Example.Run(); +BinarySearchSnippet3.Example.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Project.csproj b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source.cs b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source.cs index 21618e8c646..864ed9d5583 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source.cs @@ -1,21 +1,24 @@ using System; using System.Collections.Generic; -public class Example +namespace BinarySearchSnippet1; + +public class BinarySearchExample1 { - public static void Main() + public static void Run() { // - List dinosaurs = new List(); - - dinosaurs.Add("Pachycephalosaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Deinonychus"); + List dinosaurs = + [ + "Pachycephalosaurus", + "Amargasaurus", + "Mamenchisaurus", + "Deinonychus", + ]; Console.WriteLine("Initial list:"); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -24,7 +27,7 @@ public static void Main() dinosaurs.Sort(); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -37,7 +40,7 @@ public static void Main() } Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -50,7 +53,7 @@ public static void Main() } Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source1.cs b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source1.cs index f72fca91f3b..fbe4a09cc54 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source1.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Generic; -public class DinoComparer: IComparer +namespace BinarySearchSnippet2; + +public class DinoComparer : IComparer { public int Compare(string x, string y) { @@ -26,7 +28,7 @@ public int Compare(string x, string y) // If x is not null... // if (y == null) - // ...and y is null, x is greater. + // ...and y is null, x is greater. { return 1; } @@ -58,16 +60,18 @@ public int Compare(string x, string y) public class Example { - public static void Main() + public static void Run() { - List dinosaurs = new List(); - dinosaurs.Add("Pachycephalosaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Deinonychus"); + List dinosaurs = + [ + "Pachycephalosaurus", + "Amargasaurus", + "Mamenchisaurus", + "Deinonychus", + ]; Display(dinosaurs); - DinoComparer dc = new DinoComparer(); + DinoComparer dc = new(); Console.WriteLine("\nSort with alternate comparer:"); dinosaurs.Sort(dc); @@ -102,7 +106,7 @@ private static void SearchAndInsert(List list, private static void Display(List list) { Console.WriteLine(); - foreach( string s in list ) + foreach (string s in list) { Console.WriteLine(s); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source2.cs b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source2.cs index 1f4327f1328..3ecc5fa8b3b 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source2.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/BinarySearch/source2.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Generic; -public class DinoComparer: IComparer +namespace BinarySearchSnippet3; + +public class DinoComparer : IComparer { public int Compare(string x, string y) { @@ -26,7 +28,7 @@ public int Compare(string x, string y) // If x is not null... // if (y == null) - // ...and y is null, x is greater. + // ...and y is null, x is greater. { return 1; } @@ -58,23 +60,24 @@ public int Compare(string x, string y) public class Example { - public static void Main() + public static void Run() { - List dinosaurs = new List(); - - dinosaurs.Add("Pachycephalosaurus"); - dinosaurs.Add("Parasauralophus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Galimimus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Deinonychus"); - dinosaurs.Add("Oviraptor"); - dinosaurs.Add("Tyrannosaurus"); + List dinosaurs = + [ + "Pachycephalosaurus", + "Parasauralophus", + "Amargasaurus", + "Galimimus", + "Mamenchisaurus", + "Deinonychus", + "Oviraptor", + "Tyrannosaurus", + ]; int herbivores = 5; Display(dinosaurs); - DinoComparer dc = new DinoComparer(); + DinoComparer dc = new(); Console.WriteLine("\nSort a range with the alternate comparer:"); dinosaurs.Sort(0, herbivores, dc); @@ -97,7 +100,7 @@ public static void Main() private static void Display(List list) { Console.WriteLine(); - foreach( string s in list ) + foreach (string s in list) { Console.WriteLine(s); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Capacity/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Capacity/program.cs index 1b6156a3271..65b87441984 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Capacity/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Capacity/program.cs @@ -34,7 +34,7 @@ public class Example public static void Main() { - List parts = new List(); + List parts = []; Console.WriteLine("\nCapacity: {0}", parts.Capacity); diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs index 8f2b6a71d91..6ab44a5b520 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs @@ -6,16 +6,14 @@ class Program { static void Main(string[] args) { - List cubes = new List(); + List cubes = [new Cube(8, 8, 4), new Cube(8, 4, 8), new Cube(8, 6, 4)]; - cubes.Add(new Cube(8, 8, 4)); - cubes.Add(new Cube(8, 4, 8)); - cubes.Add(new Cube(8, 6, 4)); - - if (cubes.Contains(new Cube(8, 6, 4))) { + if (cubes.Contains(new Cube(8, 6, 4))) + { Console.WriteLine("An equal cube is already in the collection."); } - else { + else + { Console.WriteLine("Cube can be added."); } @@ -38,12 +36,14 @@ public Cube(int h, int l, int w) public bool Equals(Cube other) { if (this.Height == other.Height && this.Length == other.Length - && this.Width == other.Width) { + && this.Width == other.Width) + { return true; } - else { + else + { return false; } } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs index 34f00e3265a..ea7de23687f 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs @@ -35,15 +35,17 @@ public class Example public static void Main() { // Create a list of parts. - List parts = new List(); - - // Add parts to the list. - parts.Add(new Part() { PartName = "crank arm", PartId = 1234 }); - parts.Add(new Part() { PartName = "chain ring", PartId = 1334 }); - parts.Add(new Part() { PartName = "regular seat", PartId = 1434 }); - parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); - parts.Add(new Part() { PartName = "cassette", PartId = 1534 }); - parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ; + List parts = + [ + // Add parts to the list. + new Part() { PartName = "crank arm", PartId = 1234 }, + new Part() { PartName = "chain ring", PartId = 1334 }, + new Part() { PartName = "regular seat", PartId = 1434 }, + new Part() { PartName = "banana seat", PartId = 1444 }, + new Part() { PartName = "cassette", PartId = 1534 }, + new Part() { PartName = "shift lever", PartId = 1634 }, + ]; + ; // Write out the parts in the list. This will call the overridden ToString method // in the Part class. diff --git a/snippets/csharp/System.Collections.Generic/ListT/CopyTo/source.cs b/snippets/csharp/System.Collections.Generic/ListT/CopyTo/source.cs index d1e1036bde6..0ebb7a81e30 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/CopyTo/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/CopyTo/source.cs @@ -6,16 +6,17 @@ public class Example { public static void Main() { - List dinosaurs = new List(); - - dinosaurs.Add("Tyrannosaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Brachiosaurus"); - dinosaurs.Add("Compsognathus"); + List dinosaurs = + [ + "Tyrannosaurus", + "Amargasaurus", + "Mamenchisaurus", + "Brachiosaurus", + "Compsognathus", + ]; Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -28,7 +29,7 @@ public static void Main() dinosaurs.CopyTo(2, array, 12, 3); Console.WriteLine("\nContents of the array:"); - foreach(string dinosaur in array) + foreach (string dinosaur in array) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Exists/source.cs b/snippets/csharp/System.Collections.Generic/ListT/Exists/source.cs index ff464c65d38..fe875e1cdbe 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Exists/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Exists/source.cs @@ -6,19 +6,20 @@ public class Example { public static void Main() { - List dinosaurs = new List(); - - dinosaurs.Add("Compsognathus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Oviraptor"); - dinosaurs.Add("Velociraptor"); - dinosaurs.Add("Deinonychus"); - dinosaurs.Add("Dilophosaurus"); - dinosaurs.Add("Gallimimus"); - dinosaurs.Add("Triceratops"); + List dinosaurs = + [ + "Compsognathus", + "Amargasaurus", + "Oviraptor", + "Velociraptor", + "Deinonychus", + "Dilophosaurus", + "Gallimimus", + "Triceratops", + ]; Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -35,7 +36,7 @@ public static void Main() Console.WriteLine("\nFindAll(EndsWithSaurus):"); List sublist = dinosaurs.FindAll(EndsWithSaurus); - foreach(string dinosaur in sublist) + foreach (string dinosaur in sublist) { Console.WriteLine(dinosaur); } @@ -45,7 +46,7 @@ public static void Main() dinosaurs.RemoveAll(EndsWithSaurus)); Console.WriteLine("\nList now contains:"); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -55,7 +56,7 @@ public static void Main() } // Search predicate returns true if a string ends in "saurus". - private static bool EndsWithSaurus(String s) + private static bool EndsWithSaurus(string s) { return s.ToLower().EndsWith("saurus"); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Find/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Find/program.cs index 4e5cacd36b6..f9d1bdbd1c5 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Find/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Find/program.cs @@ -10,14 +10,14 @@ class Program { private static string IDtoFind = "bk109"; - private static List Books = new List(); + private static List Books = []; public static void Main(string[] args) { FillList(); // Find a book by its ID. Book result = Books.Find( - delegate(Book bk) + delegate (Book bk) { return bk.ID == IDtoFind; } @@ -33,9 +33,9 @@ public static void Main(string[] args) // Find last book in collection published before 2001. result = Books.FindLast( - delegate(Book bk) + delegate (Book bk) { - DateTime year2001 = new DateTime(2001,01,01); + DateTime year2001 = new(2001, 01, 01); return bk.Publish_date < year2001; }); if (result != null) @@ -60,7 +60,7 @@ public static void Main(string[] args) // Find all books under $10.00. results = Books.FindAll( - delegate(Book bk) + delegate (Book bk) { return bk.Price < 10.00; } @@ -102,8 +102,10 @@ private static void FillList() // Evaluate each element and set set values in the book object. foreach (XElement el in elements) { - Book book = new Book(); - book.ID = el.Attribute("id").Value; + Book book = new() + { + ID = el.Attribute("id").Value + }; IEnumerable props = el.Elements(); foreach (XElement p in props) { @@ -148,7 +150,7 @@ private static bool FindComputer(Book bk) { return true; } - else + else { return false; } diff --git a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex1.cs b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex1.cs index 5aa8cf958ee..f32d67234b1 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex1.cs @@ -2,58 +2,60 @@ using System; using System.Collections.Generic; +namespace FindIndexSnippet1; + public class Employee : IComparable { - public String Name { get; set; } - public int Id { get; set; } + public string Name { get; set; } + public int Id { get; set; } - public int CompareTo(Object o ) - { - Employee e = o as Employee; - if (e == null) - throw new ArgumentException("o is not an Employee object."); + public int CompareTo(object o) + { + Employee e = o as Employee; + if (e == null) + throw new ArgumentException("o is not an Employee object."); - return Name.CompareTo(e.Name); - } + return Name.CompareTo(e.Name); + } } public class EmployeeSearch { - String _s; + string _s; - public EmployeeSearch(String s) - { - _s = s; - } + public EmployeeSearch(string s) + { + _s = s; + } - public bool StartsWith(Employee e) - { - return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); - } + public bool StartsWith(Employee e) + { + return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); + } } public class Example { - public static void Main() - { - var employees = new List(); - employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 }, - new Employee { Name = "Jill", Id = 3 }, - new Employee { Name = "Dave", Id = 5 }, - new Employee { Name = "Jack", Id = 8 }, - new Employee { Name = "Judith", Id = 12 }, - new Employee { Name = "Robert", Id = 14 }, - new Employee { Name = "Adam", Id = 1 } } ); - employees.Sort(); - - var es = new EmployeeSearch("J"); - Console.WriteLine("'J' starts at index {0}", - employees.FindIndex(0, employees.Count - 1, es.StartsWith)); - - es = new EmployeeSearch("Ju"); - Console.WriteLine("'Ju' starts at index {0}", - employees.FindIndex(0, employees.Count - 1,es.StartsWith)); - } + public static void Run() + { + List employees = []; + employees.AddRange([ new() { Name = "Frank", Id = 2 }, + new() { Name = "Jill", Id = 3 }, + new() { Name = "Dave", Id = 5 }, + new() { Name = "Jack", Id = 8 }, + new() { Name = "Judith", Id = 12 }, + new() { Name = "Robert", Id = 14 }, + new() { Name = "Adam", Id = 1 } ]); + employees.Sort(); + + EmployeeSearch es = new("J"); + Console.WriteLine("'J' starts at index {0}", + employees.FindIndex(0, employees.Count - 1, es.StartsWith)); + + es = new EmployeeSearch("Ju"); + Console.WriteLine("'Ju' starts at index {0}", + employees.FindIndex(0, employees.Count - 1, es.StartsWith)); + } } // The example displays the following output: // 'J' starts at index 3 diff --git a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex2.cs b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex2.cs index 172389f14f7..2844e9bb2a8 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex2.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex2.cs @@ -2,58 +2,60 @@ using System; using System.Collections.Generic; +namespace FindIndexSnippet2; + public class Employee : IComparable { - public String Name { get; set; } - public int Id { get; set; } + public string Name { get; set; } + public int Id { get; set; } - public int CompareTo(Object o ) - { - Employee e = o as Employee; - if (e == null) - throw new ArgumentException("o is not an Employee object."); + public int CompareTo(object o) + { + Employee e = o as Employee; + if (e == null) + throw new ArgumentException("o is not an Employee object."); - return Name.CompareTo(e.Name); - } + return Name.CompareTo(e.Name); + } } public class EmployeeSearch { - String _s; + string _s; - public EmployeeSearch(String s) - { - _s = s; - } + public EmployeeSearch(string s) + { + _s = s; + } - public bool StartsWith(Employee e) - { - return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); - } + public bool StartsWith(Employee e) + { + return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); + } } public class Example { - public static void Main() - { - var employees = new List(); - employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 }, - new Employee { Name = "Jill", Id = 3 }, - new Employee { Name = "Dave", Id = 5 }, - new Employee { Name = "Jack", Id = 8 }, - new Employee { Name = "Judith", Id = 12 }, - new Employee { Name = "Robert", Id = 14 }, - new Employee { Name = "Adam", Id = 1 } } ); - employees.Sort(); - - var es = new EmployeeSearch("J"); - Console.WriteLine("'J' starts at index {0}", - employees.FindIndex(es.StartsWith)); - - es = new EmployeeSearch("Ju"); - Console.WriteLine("'Ju' starts at index {0}", - employees.FindIndex(es.StartsWith)); - } + public static void Run() + { + List employees = []; + employees.AddRange([ new() { Name = "Frank", Id = 2 }, + new() { Name = "Jill", Id = 3 }, + new() { Name = "Dave", Id = 5 }, + new() { Name = "Jack", Id = 8 }, + new() { Name = "Judith", Id = 12 }, + new() { Name = "Robert", Id = 14 }, + new() { Name = "Adam", Id = 1 } ]); + employees.Sort(); + + EmployeeSearch es = new("J"); + Console.WriteLine("'J' starts at index {0}", + employees.FindIndex(es.StartsWith)); + + es = new EmployeeSearch("Ju"); + Console.WriteLine("'Ju' starts at index {0}", + employees.FindIndex(es.StartsWith)); + } } // The example displays the following output: // 'J' starts at index 3 diff --git a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex3.cs b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex3.cs index a25cfecb074..420aaa7b61f 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex3.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/FindIndex3.cs @@ -2,60 +2,62 @@ using System; using System.Collections.Generic; +namespace FindIndexSnippet3; + public class Employee : IComparable { - public String Name { get; set; } - public int Id { get; set; } + public string Name { get; set; } + public int Id { get; set; } - public int CompareTo(Object o ) - { - Employee e = o as Employee; - if (e == null) - throw new ArgumentException("o is not an Employee object."); + public int CompareTo(object o) + { + Employee e = o as Employee; + if (e == null) + throw new ArgumentException("o is not an Employee object."); - return Name.CompareTo(e.Name); - } + return Name.CompareTo(e.Name); + } } public class EmployeeSearch { - String _s; + string _s; - public EmployeeSearch(String s) - { - _s = s; - } + public EmployeeSearch(string s) + { + _s = s; + } - public bool StartsWith(Employee e) - { - return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); - } + public bool StartsWith(Employee e) + { + return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase); + } } public class Example { - public static void Main() - { - var employees = new List(); - employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 }, - new Employee { Name = "Jill", Id = 3 }, - new Employee { Name = "Dave", Id = 5 }, - new Employee { Name = "Jack", Id = 8 }, - new Employee { Name = "Judith", Id = 12 }, - new Employee { Name = "Robert", Id = 14 }, - new Employee { Name = "Adam", Id = 1 } } ); - employees.Sort(); - - var es = new EmployeeSearch("J"); - int index = employees.FindIndex(4, es.StartsWith); - Console.WriteLine("Starting index of'J': {0}", - index >= 0 ? index.ToString() : "Not found"); - - es = new EmployeeSearch("Ju"); - index = employees.FindIndex(4, es.StartsWith); - Console.WriteLine("Starting index of 'Ju': {0}", - index >= 0 ? index.ToString() : "Not found"); - } + public static void Run() + { + List employees = []; + employees.AddRange([ new() { Name = "Frank", Id = 2 }, + new() { Name = "Jill", Id = 3 }, + new() { Name = "Dave", Id = 5 }, + new() { Name = "Jack", Id = 8 }, + new() { Name = "Judith", Id = 12 }, + new() { Name = "Robert", Id = 14 }, + new() { Name = "Adam", Id = 1 } ]); + employees.Sort(); + + EmployeeSearch es = new("J"); + int index = employees.FindIndex(4, es.StartsWith); + Console.WriteLine("Starting index of'J': {0}", + index >= 0 ? index.ToString() : "Not found"); + + es = new EmployeeSearch("Ju"); + index = employees.FindIndex(4, es.StartsWith); + Console.WriteLine("Starting index of 'Ju': {0}", + index >= 0 ? index.ToString() : "Not found"); + } } // The example displays the following output: // 'J' starts at index 4 diff --git a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Program.cs b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Program.cs new file mode 100644 index 00000000000..baef33a6fb4 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Program.cs @@ -0,0 +1,3 @@ +FindIndexSnippet1.Example.Run(); +FindIndexSnippet2.Example.Run(); +FindIndexSnippet3.Example.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Project.csproj b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/FindIndex/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/ListT/IndexOf/source.cs b/snippets/csharp/System.Collections.Generic/ListT/IndexOf/source.cs index fbbe679eb26..4c76015e262 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/IndexOf/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/IndexOf/source.cs @@ -6,18 +6,19 @@ public class Example { public static void Main() { - List dinosaurs = new List(); - - dinosaurs.Add("Tyrannosaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Brachiosaurus"); - dinosaurs.Add("Deinonychus"); - dinosaurs.Add("Tyrannosaurus"); - dinosaurs.Add("Compsognathus"); + List dinosaurs = + [ + "Tyrannosaurus", + "Amargasaurus", + "Mamenchisaurus", + "Brachiosaurus", + "Deinonychus", + "Tyrannosaurus", + "Compsognathus", + ]; Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/LastIndexOf/source.cs b/snippets/csharp/System.Collections.Generic/ListT/LastIndexOf/source.cs index 791e291167c..441cdef26e4 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/LastIndexOf/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/LastIndexOf/source.cs @@ -6,18 +6,19 @@ public class Example { public static void Main() { - List dinosaurs = new List(); - - dinosaurs.Add("Tyrannosaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Brachiosaurus"); - dinosaurs.Add("Deinonychus"); - dinosaurs.Add("Tyrannosaurus"); - dinosaurs.Add("Compsognathus"); + List dinosaurs = + [ + "Tyrannosaurus", + "Amargasaurus", + "Mamenchisaurus", + "Brachiosaurus", + "Deinonychus", + "Tyrannosaurus", + "Compsognathus", + ]; Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/ListT/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/ListT/Overview/RunExamples.cs b/snippets/csharp/System.Collections.Generic/ListT/Overview/RunExamples.cs new file mode 100644 index 00000000000..9c9212e4631 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/Overview/RunExamples.cs @@ -0,0 +1,2 @@ +ListOverviewSnippet1.ListOverviewExample1.Run(); +ListOverviewSnippet2.Example.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Overview/program.cs index 0ef266716d0..4a1bdfd0ac4 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Overview/program.cs @@ -1,50 +1,53 @@ // using System; using System.Collections.Generic; + +namespace ListOverviewSnippet2; // Simple business object. A PartId is used to identify the type of part // but the part name can change. public class Part : IEquatable - { - public string PartName { get; set; } +{ + public string PartName { get; set; } - public int PartId { get; set; } + public int PartId { get; set; } - public override string ToString() - { - return "ID: " + PartId + " Name: " + PartName; - } - public override bool Equals(object obj) - { - if (obj == null) return false; - Part objAsPart = obj as Part; - if (objAsPart == null) return false; - else return Equals(objAsPart); - } - public override int GetHashCode() - { - return PartId; - } - public bool Equals(Part other) - { - if (other == null) return false; - return (this.PartId.Equals(other.PartId)); - } - // Should also override == and != operators. + public override string ToString() + { + return "ID: " + PartId + " Name: " + PartName; + } + public override bool Equals(object obj) + { + if (obj == null) return false; + Part objAsPart = obj as Part; + if (objAsPart == null) return false; + else return Equals(objAsPart); + } + public override int GetHashCode() + { + return PartId; } + public bool Equals(Part other) + { + if (other == null) return false; + return (this.PartId.Equals(other.PartId)); + } + // Should also override == and != operators. +} public class Example { - public static void Main() + public static void Run() { // Create a list of parts. - List parts = new List(); - - // Add parts to the list. - parts.Add(new Part() { PartName = "crank arm", PartId = 1234 }); - parts.Add(new Part() { PartName = "chain ring", PartId = 1334 }); - parts.Add(new Part() { PartName = "regular seat", PartId = 1434 }); - parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); - parts.Add(new Part() { PartName = "cassette", PartId = 1534 }); - parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); + List parts = + [ + // Add parts to the list. + new Part() { PartName = "crank arm", PartId = 1234 }, + new Part() { PartName = "chain ring", PartId = 1334 }, + new Part() { PartName = "regular seat", PartId = 1434 }, + new Part() { PartName = "banana seat", PartId = 1444 }, + new Part() { PartName = "cassette", PartId = 1534 }, + new Part() { PartName = "shift lever", PartId = 1634 }, + ]; // Write out the parts in the list. This will call the overridden ToString method // in the Part class. @@ -92,47 +95,47 @@ public static void Main() Console.WriteLine(aPart); } - /* + /* - ID: 1234 Name: crank arm - ID: 1334 Name: chain ring - ID: 1434 Name: regular seat - ID: 1444 Name: banana seat - ID: 1534 Name: cassette - ID: 1634 Name: shift lever + ID: 1234 Name: crank arm + ID: 1334 Name: chain ring + ID: 1434 Name: regular seat + ID: 1444 Name: banana seat + ID: 1534 Name: cassette + ID: 1634 Name: shift lever - Contains("1734"): False + Contains("1734"): False - Insert(2, "1834") - ID: 1234 Name: crank arm - ID: 1334 Name: chain ring - ID: 1834 Name: brake lever - ID: 1434 Name: regular seat - ID: 1444 Name: banana seat - ID: 1534 Name: cassette - ID: 1634 Name: shift lever + Insert(2, "1834") + ID: 1234 Name: crank arm + ID: 1334 Name: chain ring + ID: 1834 Name: brake lever + ID: 1434 Name: regular seat + ID: 1444 Name: banana seat + ID: 1534 Name: cassette + ID: 1634 Name: shift lever - Parts[3]: ID: 1434 Name: regular seat + Parts[3]: ID: 1434 Name: regular seat - Remove("1534") + Remove("1534") - ID: 1234 Name: crank arm - ID: 1334 Name: chain ring - ID: 1834 Name: brake lever - ID: 1434 Name: regular seat - ID: 1444 Name: banana seat - ID: 1634 Name: shift lever + ID: 1234 Name: crank arm + ID: 1334 Name: chain ring + ID: 1834 Name: brake lever + ID: 1434 Name: regular seat + ID: 1444 Name: banana seat + ID: 1634 Name: shift lever - RemoveAt(3) + RemoveAt(3) - ID: 1234 Name: crank arm - ID: 1334 Name: chain ring - ID: 1834 Name: brake lever - ID: 1444 Name: banana seat - ID: 1634 Name: shift lever + ID: 1234 Name: crank arm + ID: 1334 Name: chain ring + ID: 1834 Name: brake lever + ID: 1444 Name: banana seat + ID: 1634 Name: shift lever - */ + */ } } // diff --git a/snippets/csharp/System.Collections.Generic/ListT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/ListT/Overview/source.cs index fd328110e9f..7a19c6d6103 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Overview/source.cs @@ -1,13 +1,15 @@ using System; using System.Collections.Generic; -public class Example +namespace ListOverviewSnippet1; + +public class ListOverviewExample1 { - public static void Main() + public static void Run() { // // - List dinosaurs = new List(); + List dinosaurs = []; Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity); @@ -18,7 +20,7 @@ public static void Main() dinosaurs.Add("Compsognathus"); // Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -33,21 +35,21 @@ public static void Main() dinosaurs.Insert(2, "Compsognathus"); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } - // + // // Shows accessing the list using the Item property. Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]); - // + // Console.WriteLine("\nRemove(\"Compsognathus\")"); dinosaurs.Remove("Compsognathus"); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Reverse/source.cs b/snippets/csharp/System.Collections.Generic/ListT/Reverse/source.cs index d0e533bfe43..0404b539148 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Reverse/source.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Reverse/source.cs @@ -6,17 +6,18 @@ public class Example { public static void Main() { - List dinosaurs = new List(); - - dinosaurs.Add("Pachycephalosaurus"); - dinosaurs.Add("Parasauralophus"); - dinosaurs.Add("Mamenchisaurus"); - dinosaurs.Add("Amargasaurus"); - dinosaurs.Add("Coelophysis"); - dinosaurs.Add("Oviraptor"); + List dinosaurs = + [ + "Pachycephalosaurus", + "Parasauralophus", + "Mamenchisaurus", + "Amargasaurus", + "Coelophysis", + "Oviraptor", + ]; Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -24,7 +25,7 @@ public static void Main() dinosaurs.Reverse(); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } @@ -32,7 +33,7 @@ public static void Main() dinosaurs.Reverse(1, 4); Console.WriteLine(); - foreach(string dinosaur in dinosaurs) + foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/Project.csproj b/snippets/csharp/System.Collections.Generic/ListT/Sort/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs new file mode 100644 index 00000000000..40e8d7ea655 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs @@ -0,0 +1,2 @@ +ListSortSnippet1.Example.Run(); +ListSortSnippet2.Example.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs index 0ffc4177945..eb8635b2248 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs @@ -1,34 +1,36 @@ using System; using System.Collections.Generic; +namespace ListSortSnippet1; + public class Example { - public static void Main() - { - // - String[] names = { "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Jody", - "Yiska", "Yuma", "Jody", "Nikita" }; - var nameList = new List(); - nameList.AddRange(names); - Console.WriteLine("List in unsorted order: "); - foreach (var name in nameList) - Console.Write(" {0}", name); + public static void Run() + { + // + string[] names = [ "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Jody", + "Yiska", "Yuma", "Jody", "Nikita" ]; + List nameList = []; + nameList.AddRange(names); + Console.WriteLine("List in unsorted order: "); + foreach (string name in nameList) + Console.Write(" {0}", name); - Console.WriteLine(Environment.NewLine); + Console.WriteLine(Environment.NewLine); - nameList.Sort(); - Console.WriteLine("List in sorted order: "); - foreach (var name in nameList) - Console.Write(" {0}", name); + nameList.Sort(); + Console.WriteLine("List in sorted order: "); + foreach (string name in nameList) + Console.Write(" {0}", name); - Console.WriteLine(); + Console.WriteLine(); - // The example displays the following output: - // List in unsorted order: - // Samuel Dakota Koani Saya Vanya Jody Yiska Yuma Jody Nikita - // - // List in sorted order: - // Dakota Jody Jody Koani Nikita Samuel Saya Vanya Yiska Yuma - // - } + // The example displays the following output: + // List in unsorted order: + // Samuel Dakota Koani Saya Vanya Jody Yiska Yuma Jody Nikita + // + // List in sorted order: + // Dakota Jody Jody Koani Nikita Samuel Saya Vanya Yiska Yuma + // + } } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs index f7edb4d55ac..81fe83a6ba2 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs @@ -1,9 +1,11 @@ // using System; using System.Collections.Generic; + +namespace ListSortSnippet2; // Simple business object. A PartId is used to identify the type of part // but the part name can change. -public class Part : IEquatable , IComparable +public class Part : IEquatable, IComparable { public string PartName { get; set; } @@ -29,7 +31,7 @@ public int SortByNameAscending(string name1, string name2) // Default comparer for Part type. public int CompareTo(Part comparePart) { - // A null value means that this object is greater. + // A null value means that this object is greater. if (comparePart == null) return 1; @@ -49,17 +51,19 @@ public bool Equals(Part other) } public class Example { - public static void Main() + public static void Run() { // Create a list of parts. - List parts = new List(); - - // Add parts to the list. - parts.Add(new Part() { PartName = "regular seat", PartId = 1434 }); - parts.Add(new Part() { PartName= "crank arm", PartId = 1234 }); - parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ; + List parts = + [ + // Add parts to the list. + new Part() { PartName = "regular seat", PartId = 1434 }, + new Part() { PartName= "crank arm", PartId = 1234 }, + new Part() { PartName = "shift lever", PartId = 1634 }, + ]; + ; // Name intentionally left null. - parts.Add(new Part() { PartId = 1334 }); + parts.Add(new Part() { PartId = 1334 }); parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); parts.Add(new Part() { PartName = "cassette", PartId = 1534 }); @@ -85,7 +89,7 @@ public static void Main() // This shows calling the Sort(Comparison(T) overload using // an anonymous method for the Comparison delegate. // This method treats null as the lesser of two values. - parts.Sort(delegate(Part x, Part y) + parts.Sort(delegate (Part x, Part y) { if (x.PartName == null && y.PartName == null) return 0; else if (x.PartName == null) return -1; diff --git a/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs index 58f2b83bffb..ab295a659e6 100644 --- a/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs @@ -6,7 +6,7 @@ class Example { public static void Main() { - Queue numbers = new Queue(); + Queue numbers = new(); numbers.Enqueue("one"); numbers.Enqueue("two"); numbers.Enqueue("three"); @@ -14,7 +14,7 @@ public static void Main() numbers.Enqueue("five"); // A queue can be enumerated without disturbing its contents. - foreach( string number in numbers ) + foreach (string number in numbers) { Console.WriteLine(number); } @@ -26,10 +26,10 @@ public static void Main() // Create a copy of the queue, using the ToArray method and the // constructor that accepts an IEnumerable. - Queue queueCopy = new Queue(numbers.ToArray()); + Queue queueCopy = new([.. numbers]); Console.WriteLine("\nContents of the first copy:"); - foreach( string number in queueCopy ) + foreach (string number in queueCopy) { Console.WriteLine(number); } @@ -42,10 +42,10 @@ public static void Main() // Create a second queue, using the constructor that accepts an // IEnumerable(Of T). - Queue queueCopy2 = new Queue(array2); + Queue queueCopy2 = new(array2); Console.WriteLine("\nContents of the second copy, with duplicates and nulls:"); - foreach( string number in queueCopy2 ) + foreach (string number in queueCopy2) { Console.WriteLine(number); } diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Program.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Program.cs new file mode 100644 index 00000000000..ac7396a25f3 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Program.cs @@ -0,0 +1,3 @@ +SortedDictionaryConstructorExample1.Run(); +SortedDictionaryConstructorExample2.Run(); +SortedDictionaryConstructorExample3.Run(); diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Project.csproj b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source.cs index 7c19012aa91..823740a0c06 100644 --- a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source.cs @@ -2,21 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class SortedDictionaryConstructorExample1 { - public static void Main() + public static void Run() { // Create a new SortedDictionary of strings, with string keys // and a case-insensitive comparer for the current culture. SortedDictionary openWith = new SortedDictionary( - StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Try to add a fifth element with a key that is the same // except for case; this would be allowed with the default @@ -32,7 +33,7 @@ public static void Main() // List the contents of the sorted dictionary. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source1.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source1.cs index a0d24ab2472..5e634884e3f 100644 --- a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source1.cs +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source1.cs @@ -2,20 +2,21 @@ using System; using System.Collections.Generic; -public class Example +public class SortedDictionaryConstructorExample2 { - public static void Main() + public static void Run() { // Create a new Dictionary of strings, with string keys. // Dictionary openWith = - new Dictionary(); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary() + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Create a SortedDictionary of strings with string keys, // and initialize it with the contents of the Dictionary. @@ -24,7 +25,7 @@ public static void Main() // List the contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source2.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source2.cs index e6b670a6f47..b54246cf870 100644 --- a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source2.cs +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/.ctor/source2.cs @@ -2,26 +2,26 @@ using System; using System.Collections.Generic; -public class Example +public class SortedDictionaryConstructorExample3 { - public static void Main() + public static void Run() { // Create a new Dictionary of strings, with string keys and // a case-insensitive equality comparer for the current // culture. Dictionary openWith = - new Dictionary - (StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("Bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary(StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "Bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // List the contents of the Dictionary. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); @@ -36,7 +36,7 @@ public static void Main() // List the sorted contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/Overview/source.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/Overview/source.cs index 47822d75efa..849ec26d678 100644 --- a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/Overview/source.cs @@ -10,14 +10,15 @@ public static void Main() // Create a new sorted dictionary of strings, with string // keys. SortedDictionary openWith = - new SortedDictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new() + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // The Add method throws an exception if the new key is // already in the dictionary. @@ -92,7 +93,7 @@ public static void Main() // When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); @@ -107,7 +108,7 @@ public static void Main() // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); - foreach( string s in valueColl ) + foreach (string s in valueColl) { Console.WriteLine("Value = {0}", s); } @@ -121,7 +122,7 @@ public static void Main() // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary keys. Console.WriteLine(); - foreach( string s in keyColl ) + foreach (string s in keyColl) { Console.WriteLine("Key = {0}", s); } diff --git a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs index 6902d6ac205..1931e6bd7ad 100644 --- a/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedDictionaryTKey,TValue/System.Collections.IDictionary.Add/source.cs @@ -31,18 +31,19 @@ public static void Main() // Create a new sorted dictionary of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new SortedDictionary(); - - // Add some elements to the dictionary. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new SortedDictionary + { + // Add some elements to the dictionary. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // -// + // // try { @@ -132,7 +133,7 @@ public static void Main() // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. Console.WriteLine(); - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); @@ -148,7 +149,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Value = {0}", s); } @@ -163,7 +164,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Key = {0}", s); } @@ -180,7 +181,7 @@ public static void Main() Console.WriteLine("Key \"dib\" is not found."); } // -// + // } } // diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Program.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Program.cs new file mode 100644 index 00000000000..4ed305467c6 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Program.cs @@ -0,0 +1,5 @@ +SortedListConstructorExample1.Run(); +SortedListConstructorExample2.Run(); +SortedListConstructorExample3.Run(); +SortedListConstructorExample4.Run(); +SortedListConstructorExample5.Run(); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Project.csproj b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source.cs index 38324f38b37..85de4bd5a0a 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source.cs @@ -2,21 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListConstructorExample1 { - public static void Main() + public static void Run() { // Create a new sorted list of strings, with string keys and // a case-insensitive comparer for the current culture. SortedList openWith = new SortedList( - StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the list. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the list. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Try to add a fifth element with a key that is the same // except for case; this would be allowed with the default @@ -32,7 +33,7 @@ public static void Main() // List the contents of the sorted list. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source1.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source1.cs index e2d9dbeeee0..060a49f4294 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source1.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source1.cs @@ -2,20 +2,21 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListConstructorExample2 { - public static void Main() + public static void Run() { // Create a new Dictionary of strings, with string keys. // Dictionary openWith = - new Dictionary(); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary() + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Create a SortedList of strings with string keys, // and initialize it with the contents of the Dictionary. @@ -24,7 +25,7 @@ public static void Main() // List the contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source2.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source2.cs index 9ee2a703b90..4935f1c4dca 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source2.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source2.cs @@ -2,22 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListConstructorExample3 { - public static void Main() + public static void Run() { // Create a new Dictionary of strings, with string keys and // a case-insensitive equality comparer for the current // culture. Dictionary openWith = - new Dictionary - (StringComparer.CurrentCultureIgnoreCase); - - // Add some elements to the dictionary. - openWith.Add("txt", "notepad.exe"); - openWith.Add("Bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new Dictionary(StringComparer.CurrentCultureIgnoreCase) + { + // Add some elements to the dictionary. + { "txt", "notepad.exe" }, + { "Bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Create a SortedList of strings with string keys and a // case-insensitive equality comparer for the current culture, @@ -28,7 +28,7 @@ public static void Main() // List the sorted contents of the copy. Console.WriteLine(); - foreach( KeyValuePair kvp in copy ) + foreach (KeyValuePair kvp in copy) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source3.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source3.cs index c8e93aa09e9..1b04a75988d 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source3.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source3.cs @@ -2,24 +2,25 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListConstructorExample4 { - public static void Main() + public static void Run() { // Create a new sorted list of strings, with string keys and // an initial capacity of 4. SortedList openWith = - new SortedList(4); - - // Add 4 elements to the list. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new SortedList(4) + { + // Add 4 elements to the list. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // List the contents of the sorted list. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source4.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source4.cs index b3c798f96f1..bb1b40e445d 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source4.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/.ctor/source4.cs @@ -2,21 +2,22 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListConstructorExample5 { - public static void Main() + public static void Run() { // Create a new sorted list of strings, with string keys, an // initial capacity of 5, and a case-insensitive comparer. SortedList openWith = new SortedList(5, - StringComparer.CurrentCultureIgnoreCase); - - // Add 4 elements to the list. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("DIB", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + StringComparer.CurrentCultureIgnoreCase) + { + // Add 4 elements to the list. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "DIB", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // Try to add a fifth element with a key that is the same // except for case; this would be allowed with the default @@ -32,7 +33,7 @@ public static void Main() // List the contents of the sorted list. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Program.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Program.cs new file mode 100644 index 00000000000..8e17a9f8ba3 --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Program.cs @@ -0,0 +1,2 @@ +SortedListOverviewExample1.Run(); +SortedListOverviewExample2.Run(); diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs index 839484f2e43..2744e88974e 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/remarks.cs @@ -1,21 +1,22 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListOverviewExample1 { - public static void Main() + public static void Run() { // Create a new sorted list of strings, with string // keys. SortedList mySortedList = - new SortedList(); - - // Add some elements to the list. There are no - // duplicate keys, but some of the values are duplicates. - mySortedList.Add(0, "notepad.exe"); - mySortedList.Add(1, "paint.exe"); - mySortedList.Add(2, "paint.exe"); - mySortedList.Add(3, "wordpad.exe"); + new() + { + // Add some elements to the list. There are no + // duplicate keys, but some of the values are duplicates. + { 0, "notepad.exe" }, + { 1, "paint.exe" }, + { 2, "paint.exe" }, + { 3, "wordpad.exe" } + }; // string v = mySortedList.Values[3]; @@ -24,10 +25,10 @@ public static void Main() Console.WriteLine("Value at index 3: {0}", v); // - foreach( KeyValuePair kvp in mySortedList ) + foreach (KeyValuePair kvp in mySortedList) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } // } -} \ No newline at end of file +} diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs index 12afb816945..4b8a631aa58 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/Overview/source.cs @@ -2,22 +2,23 @@ using System; using System.Collections.Generic; -public class Example +public class SortedListOverviewExample2 { - public static void Main() + public static void Run() { // // Create a new sorted list of strings, with string // keys. SortedList openWith = - new SortedList(); - - // Add some elements to the list. There are no - // duplicate keys, but some of the values are duplicates. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + new() + { + // Add some elements to the list. There are no + // duplicate keys, but some of the values are duplicates. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // The Add method throws an exception if the new key is // already in the list. @@ -92,7 +93,7 @@ public static void Main() // When you use foreach to enumerate list elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); - foreach( KeyValuePair kvp in openWith ) + foreach (KeyValuePair kvp in openWith) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); @@ -106,7 +107,7 @@ public static void Main() // The elements of the list are strongly typed with the // type that was specified for the SortedList values. Console.WriteLine(); - foreach( string s in ilistValues ) + foreach (string s in ilistValues) { Console.WriteLine("Value = {0}", s); } @@ -124,7 +125,7 @@ public static void Main() // The elements of the list are strongly typed with the // type that was specified for the SortedList keys. Console.WriteLine(); - foreach( string s in ilistKeys ) + foreach (string s in ilistKeys) { Console.WriteLine("Key = {0}", s); } diff --git a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/System.Collections.IDictionary.Add/source.cs b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/System.Collections.IDictionary.Add/source.cs index 49d2fc81205..68d822be4a4 100644 --- a/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/System.Collections.IDictionary.Add/source.cs +++ b/snippets/csharp/System.Collections.Generic/SortedListTKey,TValue/System.Collections.IDictionary.Add/source.cs @@ -31,18 +31,19 @@ public static void Main() // Create a new sorted list of strings, with string keys, // and access it using the IDictionary interface. // - IDictionary openWith = new SortedList(); - - // Add some elements to the sorted list. There are no - // duplicate keys, but some of the values are duplicates. - // IDictionary.Add throws an exception if incorrect types - // are supplied for key or value. - openWith.Add("txt", "notepad.exe"); - openWith.Add("bmp", "paint.exe"); - openWith.Add("dib", "paint.exe"); - openWith.Add("rtf", "wordpad.exe"); + IDictionary openWith = new SortedList + { + // Add some elements to the sorted list. There are no + // duplicate keys, but some of the values are duplicates. + // IDictionary.Add throws an exception if incorrect types + // are supplied for key or value. + { "txt", "notepad.exe" }, + { "bmp", "paint.exe" }, + { "dib", "paint.exe" }, + { "rtf", "wordpad.exe" } + }; // -// + // // try { @@ -132,7 +133,7 @@ public static void Main() // with the IDictionary interface, the elements are retrieved // as DictionaryEntry objects instead of KeyValuePair objects. Console.WriteLine(); - foreach( DictionaryEntry de in openWith ) + foreach (DictionaryEntry de in openWith) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); @@ -148,7 +149,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Value = {0}", s); } @@ -163,7 +164,7 @@ public static void Main() // even though the ICollection interface is not strongly // typed. Console.WriteLine(); - foreach( string s in icoll ) + foreach (string s in icoll) { Console.WriteLine("Key = {0}", s); } @@ -180,7 +181,7 @@ public static void Main() Console.WriteLine("Key \"dib\" is not found."); } // -// + // } } // diff --git a/snippets/csharp/System.Collections.Generic/SortedSetT/Overview/program.cs b/snippets/csharp/System.Collections.Generic/SortedSetT/Overview/program.cs index 83f6aba4cdd..f3c4f2e21a7 100644 --- a/snippets/csharp/System.Collections.Generic/SortedSetT/Overview/program.cs +++ b/snippets/csharp/System.Collections.Generic/SortedSetT/Overview/program.cs @@ -17,7 +17,7 @@ static void Main(string[] args) // // Create a sorted set using the ByFileExtension comparer. - var mediaFiles1 = new SortedSet(new ByFileExtension()); + SortedSet mediaFiles1 = new(new ByFileExtension()); // // Note that there is a SortedSet constructor that takes an IEnumerable, @@ -58,7 +58,7 @@ static void Main(string[] args) Directory.EnumerateFiles(@"\\archives\2008\media", "*", SearchOption.AllDirectories); - var mediaFiles2 = new SortedSet(new ByFileExtension()); + SortedSet mediaFiles2 = new(new ByFileExtension()); foreach (string f in files2) { @@ -86,12 +86,14 @@ static void Main(string[] args) IEqualityComparer> comparer = SortedSet.CreateSetComparer(); - var allMedia = new HashSet>(comparer); - allMedia.Add(mediaFiles1); - allMedia.Add(mediaFiles2); + HashSet> allMedia = new(comparer) + { + mediaFiles1, + mediaFiles2 + }; // } - catch(IOException ioEx) + catch (IOException ioEx) { Console.WriteLine(ioEx.Message); } @@ -125,7 +127,7 @@ public class ByFileExtension : IComparer { string xExt, yExt; - CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer(); + CaseInsensitiveComparer caseiComp = new(); public int Compare(string x, string y) { diff --git a/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs index f87cdf9b4af..614c2380f4b 100644 --- a/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs @@ -6,7 +6,7 @@ class Example { public static void Main() { - Stack numbers = new Stack(); + Stack numbers = new(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); @@ -14,7 +14,7 @@ public static void Main() numbers.Push("five"); // A stack can be enumerated without disturbing its contents. - foreach( string number in numbers ) + foreach (string number in numbers) { Console.WriteLine(number); } @@ -26,10 +26,10 @@ public static void Main() // Create a copy of the stack, using the ToArray method and the // constructor that accepts an IEnumerable. - Stack stack2 = new Stack(numbers.ToArray()); + Stack stack2 = new([.. numbers]); Console.WriteLine("\nContents of the first copy:"); - foreach( string number in stack2 ) + foreach (string number in stack2) { Console.WriteLine(number); } @@ -42,10 +42,10 @@ public static void Main() // Create a second stack, using the constructor that accepts an // IEnumerable(Of T). - Stack stack3 = new Stack(array2); + Stack stack3 = new(array2); Console.WriteLine("\nContents of the second copy, with duplicates and nulls:"); - foreach( string number in stack3 ) + foreach (string number in stack3) { Console.WriteLine(number); } From 02c14c7d1556742516fd09aa545beaff55a6d927 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Wed, 19 Aug 2026 12:41:39 -0700 Subject: [PATCH 2/3] respond to feedback --- .../LinkedListT/Overview/Project.csproj | 2 +- .../LinkedListT/Overview/source.cs | 37 +++++++++---------- .../ListT/.ctor/source1.cs | 8 ++-- .../ListT/Contains/program1.cs | 1 - .../ListT/Sort/program.cs | 2 +- .../QueueT/Overview/source.cs | 18 ++++----- .../StackT/Overview/source.cs | 2 +- 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs index 6c69fd3ab4f..d876ef395dc 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs @@ -81,7 +81,7 @@ public static void Main() } catch (InvalidOperationException ex) { - Console.WriteLine("Exception message: {0}", ex.Message); + Console.WriteLine($"Exception message: {ex.Message}"); } Console.WriteLine(); @@ -153,7 +153,7 @@ private static void Display(LinkedList words, string test) Console.WriteLine(test); foreach (string word in words) { - Console.Write(word + " "); + Console.Write($"{word} "); } Console.WriteLine(); Console.WriteLine(); @@ -164,12 +164,11 @@ private static void IndicateNode(LinkedListNode node, string test) Console.WriteLine(test); if (node.List == null) { - Console.WriteLine("Node '{0}' is not in the list.\n", - node.Value); + Console.WriteLine($"Node '{node.Value}' is not in the list.\n"); return; } - StringBuilder result = new("(" + node.Value + ")"); + StringBuilder result = new($"({node.Value})"); LinkedListNode nodeP = node.Previous; while (nodeP != null) @@ -191,33 +190,33 @@ private static void IndicateNode(LinkedListNode node, string test) } //This code example produces the following output: -// + //The linked list values: //the fox jumps over the dog //Test 1: Add 'today' to beginning of the list: -//today the fox jumps over the dog. +//today the fox jumps over the dog //Test 2: Move first node to be last node: -//the fox jumps over the dog today. +//the fox jumps over the dog today //Test 3: Change the last node to 'yesterday': -//the fox jumps over the dog yesterday. +//the fox jumps over the dog yesterday //Test 4: Move last node to be first node: -//yesterday the fox jumps over the dog. +//yesterday the fox jumps over the dog //Test 5: Indicate last occurence of 'the': -//the fox jumps over (the) dog. +//the fox jumps over (the) dog //Test 6: Add 'lazy' and 'old' after 'the': -//the fox jumps over (the) lazy old dog. +//the fox jumps over (the) lazy old dog //Test 7: Indicate the 'fox' node: -//the (fox) jumps over the lazy old dog. +//the (fox) jumps over the lazy old dog //Test 8: Add 'quick' and 'brown' before 'fox': -//the quick brown (fox) jumps over the lazy old dog. +//the quick brown (fox) jumps over the lazy old dog //Test 9: Indicate the 'dog' node: //the quick brown fox jumps over the lazy old (dog) @@ -232,13 +231,13 @@ private static void IndicateNode(LinkedListNode node, string test) //Node 'dog' is not in the list. //Test 13: Add node removed in test 12 after a referenced node (brown): -//the quick brown (dog) jumps over the lazy old fox. +//the quick brown (dog) jumps over the lazy old fox //Test 14: Remove node that has the value 'old': -//the quick brown dog jumps over the lazy fox. +//the quick brown dog jumps over the lazy fox //Test 15: Remove last node, cast to ICollection, and add 'rhinoceros': -//the quick brown dog jumps over the lazy rhinoceros. +//the quick brown dog jumps over the lazy rhinoceros //Test 16: Copy the list to an array: //the @@ -251,8 +250,8 @@ private static void IndicateNode(LinkedListNode node, string test) //lazy //rhinoceros. -//Test 17: linked list Contains 'jumps'= True. +//Test 17: linked list Contains 'jumps'= True //Test 18: Cleared linked list Contains 'jumps' = False -// + // diff --git a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs index 2129c52d600..a15565d0158 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/.ctor/source1.cs @@ -6,13 +6,13 @@ public class ListConstructorExample2 public static void Run() { // - string[] input = [ "Apple", + string[] input = { "Apple", "Banana", - "Orange" ]; + "Orange" }; List fruits = new List(input); - Console.WriteLine("\nCapacity: {0}", fruits.Capacity); + Console.WriteLine($"\nCapacity: {fruits.Capacity}"); Console.WriteLine(); foreach (string fruit in fruits) @@ -52,7 +52,7 @@ public static void Run() } Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()"); - string[] output = [.. fruits.GetRange(2, 3)]; + string[] output = fruits.GetRange(2, 3).ToArray(); Console.WriteLine(); foreach (string fruit in output) diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs index ea7de23687f..8b52d6ec0df 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs @@ -45,7 +45,6 @@ public static void Main() new Part() { PartName = "cassette", PartId = 1534 }, new Part() { PartName = "shift lever", PartId = 1634 }, ]; - ; // Write out the parts in the list. This will call the overridden ToString method // in the Part class. diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs index 81fe83a6ba2..a9ffd89305a 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs @@ -61,7 +61,7 @@ public static void Run() new Part() { PartName= "crank arm", PartId = 1234 }, new Part() { PartName = "shift lever", PartId = 1634 }, ]; - ; + // Name intentionally left null. parts.Add(new Part() { PartId = 1334 }); parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); diff --git a/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs index ab295a659e6..b1ad993681f 100644 --- a/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/QueueT/Overview/source.cs @@ -19,14 +19,13 @@ public static void Main() Console.WriteLine(number); } - Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue()); - Console.WriteLine("Peek at next item to dequeue: {0}", - numbers.Peek()); - Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue()); + Console.WriteLine($"\nDequeuing '{numbers.Dequeue()}'"); + Console.WriteLine($"Peek at next item to dequeue: '{numbers.Peek()}'"); + Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'"); // Create a copy of the queue, using the ToArray method and the // constructor that accepts an IEnumerable. - Queue queueCopy = new([.. numbers]); + Queue queueCopy = new(numbers.ToArray()); Console.WriteLine("\nContents of the first copy:"); foreach (string number in queueCopy) @@ -50,12 +49,11 @@ public static void Main() Console.WriteLine(number); } - Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}", - queueCopy.Contains("four")); + Console.WriteLine($"\nqueueCopy.Contains(\"four\") = {queueCopy.Contains("four")}"); Console.WriteLine("\nqueueCopy.Clear()"); queueCopy.Clear(); - Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count); + Console.WriteLine($"\nqueueCopy.Count = {queueCopy.Count}"); } } @@ -89,5 +87,7 @@ public static void Main() queueCopy.Clear() queueCopy.Count = 0 - */ + +*/ + // diff --git a/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs index 614c2380f4b..7de82892e52 100644 --- a/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/StackT/Overview/source.cs @@ -26,7 +26,7 @@ public static void Main() // Create a copy of the stack, using the ToArray method and the // constructor that accepts an IEnumerable. - Stack stack2 = new([.. numbers]); + Stack stack2 = new(numbers.ToArray()); Console.WriteLine("\nContents of the first copy:"); foreach (string number in stack2) From 7b292dd084750d936020bb6ebd45049b5bf8bac9 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 20 Aug 2026 20:39:42 -0700 Subject: [PATCH 3/3] respond to feedback --- .../LinkedListT/Overview/source.cs | 6 +---- .../ListT/Contains/Project.csproj | 2 +- .../ListT/Contains/Run.cs | 2 ++ .../ListT/Contains/program.cs | 27 +++++++++---------- .../ListT/Contains/program1.cs | 20 ++++++++------ .../ListT/Sort/RunExamples.cs | 4 +-- .../ListT/Sort/Sort1.cs | 7 +++-- .../ListT/Sort/program.cs | 22 +++++++-------- .../QueueT/Overview/Project.csproj | 2 +- 9 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 snippets/csharp/System.Collections.Generic/ListT/Contains/Run.cs diff --git a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs index d876ef395dc..3cc78708233 100644 --- a/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs +++ b/snippets/csharp/System.Collections.Generic/LinkedListT/Overview/source.cs @@ -151,11 +151,7 @@ public static void Main() private static void Display(LinkedList words, string test) { Console.WriteLine(test); - foreach (string word in words) - { - Console.Write($"{word} "); - } - Console.WriteLine(); + Console.WriteLine(string.Join(" ", words)); Console.WriteLine(); } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/Project.csproj b/snippets/csharp/System.Collections.Generic/ListT/Contains/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/Project.csproj +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/Run.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/Run.cs new file mode 100644 index 00000000000..8b977efa20e --- /dev/null +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/Run.cs @@ -0,0 +1,2 @@ +CubeExample.Run(); +PartExample.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs index 6ab44a5b520..83ff5ae2bb4 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/program.cs @@ -2,13 +2,13 @@ using System; using System.Collections.Generic; -class Program +class CubeExample { - static void Main(string[] args) + public static void Run() { - List cubes = [new Cube(8, 8, 4), new Cube(8, 4, 8), new Cube(8, 6, 4)]; + List cubes = [new(8, 8, 4), new(8, 4, 8), new(8, 6, 4)]; - if (cubes.Contains(new Cube(8, 6, 4))) + if (cubes.Contains(new(8, 6, 4))) { Console.WriteLine("An equal cube is already in the collection."); } @@ -17,7 +17,7 @@ static void Main(string[] args) Console.WriteLine("Cube can be added."); } - //Outputs "An equal cube is already in the collection." + // Outputs "An equal cube is already in the collection." } } @@ -25,9 +25,9 @@ public class Cube : IEquatable { public Cube(int h, int l, int w) { - this.Height = h; - this.Length = l; - this.Width = w; + Height = h; + Length = l; + Width = w; } public int Height { get; set; } public int Length { get; set; } @@ -35,15 +35,14 @@ public Cube(int h, int l, int w) public bool Equals(Cube other) { - if (this.Height == other.Height && this.Length == other.Length - && this.Width == other.Width) + if (Height == other.Height + && Length == other.Length + && Width == other.Width) { return true; } - else - { - return false; - } + + return false; } } // diff --git a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs index 8b52d6ec0df..aa421137cc8 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Contains/program1.cs @@ -1,6 +1,7 @@ // using System; using System.Collections.Generic; + // Simple business object. A PartId is used to identify a part // but the part name can change. public class Part : IEquatable @@ -10,29 +11,31 @@ public class Part : IEquatable public override string ToString() { - return "ID: " + PartId + " Name: " + PartName; + return $"ID: {PartId} Name: {PartName}"; } public override bool Equals(object obj) { if (obj == null) return false; - Part objAsPart = obj as Part; - if (objAsPart == null) return false; + if (obj is not Part objAsPart) return false; else return Equals(objAsPart); } + public override int GetHashCode() { return PartId; } + public bool Equals(Part other) { if (other == null) return false; - return (this.PartId.Equals(other.PartId)); + return (PartId.Equals(other.PartId)); } // Should also override == and != operators. } -public class Example + +public class PartExample { - public static void Main() + public static void Run() { // Create a list of parts. List parts = @@ -67,7 +70,7 @@ public static void Main() Console.WriteLine("\nExists: Part with Id=1444: {0}", parts.Exists(x => x.PartId == 1444)); - /*This code example produces the following output: + /* This code example produces the following output: ID: 1234 Name: crank arm ID: 1334 Name: chain ring @@ -81,7 +84,8 @@ public static void Main() Find: Part where name contains "seat": ID: 1434 Name: regular seat Exists: Part with Id=1444: True - */ + + */ } } // diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs index 40e8d7ea655..21c22922e25 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/RunExamples.cs @@ -1,2 +1,2 @@ -ListSortSnippet1.Example.Run(); -ListSortSnippet2.Example.Run(); +PartExample.Run(); +ListSortExample.Run(); diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs index eb8635b2248..244c10729a6 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/Sort1.cs @@ -1,14 +1,12 @@ using System; using System.Collections.Generic; -namespace ListSortSnippet1; - -public class Example +public class ListSortExample { public static void Run() { // - string[] names = [ "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Jody", + ReadOnlySpan names = [ "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Jody", "Yiska", "Yuma", "Jody", "Nikita" ]; List nameList = []; nameList.AddRange(names); @@ -31,6 +29,7 @@ public static void Run() // // List in sorted order: // Dakota Jody Jody Koani Nikita Samuel Saya Vanya Yiska Yuma + // } } diff --git a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs index a9ffd89305a..9e47c2b316b 100644 --- a/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs +++ b/snippets/csharp/System.Collections.Generic/ListT/Sort/program.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -namespace ListSortSnippet2; // Simple business object. A PartId is used to identify the type of part // but the part name can change. public class Part : IEquatable, IComparable @@ -13,7 +12,7 @@ public class Part : IEquatable, IComparable public override string ToString() { - return "ID: " + PartId + " Name: " + PartName; + return $"ID: {PartId} Name: {PartName}"; } public override bool Equals(object obj) { @@ -24,7 +23,6 @@ public override bool Equals(object obj) } public int SortByNameAscending(string name1, string name2) { - return name1.CompareTo(name2); } @@ -36,7 +34,7 @@ public int CompareTo(Part comparePart) return 1; else - return this.PartId.CompareTo(comparePart.PartId); + return PartId.CompareTo(comparePart.PartId); } public override int GetHashCode() { @@ -45,11 +43,11 @@ public override int GetHashCode() public bool Equals(Part other) { if (other == null) return false; - return (this.PartId.Equals(other.PartId)); + return PartId.Equals(other.PartId); } // Should also override == and != operators. } -public class Example +public class PartExample { public static void Run() { @@ -57,11 +55,11 @@ public static void Run() List parts = [ // Add parts to the list. - new Part() { PartName = "regular seat", PartId = 1434 }, - new Part() { PartName= "crank arm", PartId = 1234 }, - new Part() { PartName = "shift lever", PartId = 1634 }, + new() { PartName = "regular seat", PartId = 1434 }, + new() { PartName= "crank arm", PartId = 1234 }, + new() { PartName = "shift lever", PartId = 1634 }, ]; - + // Name intentionally left null. parts.Add(new Part() { PartId = 1334 }); parts.Add(new Part() { PartName = "banana seat", PartId = 1444 }); @@ -105,7 +103,7 @@ public static void Run() /* - Before sort: + Before sort: ID: 1434 Name: regular seat ID: 1234 Name: crank arm ID: 1634 Name: shift lever @@ -129,7 +127,7 @@ public static void Run() ID: 1434 Name: regular seat ID: 1634 Name: shift lever - */ + */ } } // diff --git a/snippets/csharp/System.Collections.Generic/QueueT/Overview/Project.csproj b/snippets/csharp/System.Collections.Generic/QueueT/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Generic/QueueT/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Generic/QueueT/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0