diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/bitvector32_sections.cs b/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/bitvector32_sections.cs index b97d9628d48..89e2b0d2670 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/bitvector32_sections.cs +++ b/snippets/csharp/System.Collections.Specialized/BitVector32+Section/Overview/bitvector32_sections.cs @@ -4,46 +4,48 @@ using System; using System.Collections.Specialized; -public class SamplesBitVector32 { +public class SamplesBitVector32 +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a BitVector32. - BitVector32 myBV = new BitVector32( 0 ); + // Creates and initializes a BitVector32. + BitVector32 myBV = new(0); - // Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15. - // mySect3, which uses exactly one bit, can also be used as a bit flag. - BitVector32.Section mySect1 = BitVector32.CreateSection( 6 ); - BitVector32.Section mySect2 = BitVector32.CreateSection( 3, mySect1 ); - BitVector32.Section mySect3 = BitVector32.CreateSection( 1, mySect2 ); - BitVector32.Section mySect4 = BitVector32.CreateSection( 15, mySect3 ); + // Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15. + // mySect3, which uses exactly one bit, can also be used as a bit flag. + BitVector32.Section mySect1 = BitVector32.CreateSection(6); + BitVector32.Section mySect2 = BitVector32.CreateSection(3, mySect1); + BitVector32.Section mySect3 = BitVector32.CreateSection(1, mySect2); + BitVector32.Section mySect4 = BitVector32.CreateSection(15, mySect3); - // Displays the values of the sections. - Console.WriteLine( "Initial values:" ); - Console.WriteLine( "\tmySect1: {0}", myBV[mySect1] ); - Console.WriteLine( "\tmySect2: {0}", myBV[mySect2] ); - Console.WriteLine( "\tmySect3: {0}", myBV[mySect3] ); - Console.WriteLine( "\tmySect4: {0}", myBV[mySect4] ); + // Displays the values of the sections. + Console.WriteLine("Initial values:"); + Console.WriteLine($"\tmySect1: {myBV[mySect1]}"); + Console.WriteLine($"\tmySect2: {myBV[mySect2]}"); + Console.WriteLine($"\tmySect3: {myBV[mySect3]}"); + Console.WriteLine($"\tmySect4: {myBV[mySect4]}"); - // Sets each section to a new value and displays the value of the BitVector32 at each step. - Console.WriteLine( "Changing the values of each section:" ); - Console.WriteLine( "\tInitial: \t{0}", myBV.ToString() ); - myBV[mySect1] = 5; - Console.WriteLine( "\tmySect1 = 5:\t{0}", myBV.ToString() ); - myBV[mySect2] = 3; - Console.WriteLine( "\tmySect2 = 3:\t{0}", myBV.ToString() ); - myBV[mySect3] = 1; - Console.WriteLine( "\tmySect3 = 1:\t{0}", myBV.ToString() ); - myBV[mySect4] = 9; - Console.WriteLine( "\tmySect4 = 9:\t{0}", myBV.ToString() ); + // Sets each section to a new value and displays the value of the BitVector32 at each step. + Console.WriteLine("Changing the values of each section:"); + Console.WriteLine($"\tInitial: \t{myBV}"); + myBV[mySect1] = 5; + Console.WriteLine($"\tmySect1 = 5:\t{myBV}"); + myBV[mySect2] = 3; + Console.WriteLine($"\tmySect2 = 3:\t{myBV}"); + myBV[mySect3] = 1; + Console.WriteLine($"\tmySect3 = 1:\t{myBV}"); + myBV[mySect4] = 9; + Console.WriteLine($"\tmySect4 = 9:\t{myBV}"); - // Displays the values of the sections. - Console.WriteLine( "New values:" ); - Console.WriteLine( "\tmySect1: {0}", myBV[mySect1] ); - Console.WriteLine( "\tmySect2: {0}", myBV[mySect2] ); - Console.WriteLine( "\tmySect3: {0}", myBV[mySect3] ); - Console.WriteLine( "\tmySect4: {0}", myBV[mySect4] ); - } + // Displays the values of the sections. + Console.WriteLine("New values:"); + Console.WriteLine($"\tmySect1: {myBV[mySect1]}"); + Console.WriteLine($"\tmySect2: {myBV[mySect2]}"); + Console.WriteLine($"\tmySect3: {myBV[mySect3]}"); + Console.WriteLine($"\tmySect4: {myBV[mySect4]}"); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/Project.csproj b/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/bitvector32_createmasks.cs b/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/bitvector32_createmasks.cs index a7a9f63a730..ab735ed603d 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/bitvector32_createmasks.cs +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/CreateMask/bitvector32_createmasks.cs @@ -4,31 +4,33 @@ using System; using System.Collections.Specialized; -public class SamplesBitVector32 { - - public static void Main() { - - // Creates and initializes a BitVector32 with all bit flags set to FALSE. - BitVector32 myBV = new BitVector32( 0 ); - - // Creates masks to isolate each of the first five bit flags. - int myBit1 = BitVector32.CreateMask(); - int myBit2 = BitVector32.CreateMask( myBit1 ); - int myBit3 = BitVector32.CreateMask( myBit2 ); - int myBit4 = BitVector32.CreateMask( myBit3 ); - int myBit5 = BitVector32.CreateMask( myBit4 ); - Console.WriteLine( "Initial: \t{0}", myBV.ToString() ); - - // Sets the third bit to TRUE. - myBV[myBit3] = true; - Console.WriteLine( "myBit3 = TRUE \t{0}", myBV.ToString() ); - - // Combines two masks to access multiple bits at a time. - myBV[myBit4 + myBit5] = true; - Console.WriteLine( "myBit4 + myBit5 = TRUE \t{0}", myBV.ToString() ); - myBV[myBit1 | myBit2] = true; - Console.WriteLine( "myBit1 | myBit2 = TRUE \t{0}", myBV.ToString() ); - } +public class SamplesBitVector32 +{ + + public static void Main() + { + + // Creates and initializes a BitVector32 with all bit flags set to FALSE. + BitVector32 myBV = new(0); + + // Creates masks to isolate each of the first five bit flags. + int myBit1 = BitVector32.CreateMask(); + int myBit2 = BitVector32.CreateMask(myBit1); + int myBit3 = BitVector32.CreateMask(myBit2); + int myBit4 = BitVector32.CreateMask(myBit3); + int myBit5 = BitVector32.CreateMask(myBit4); + Console.WriteLine($"Initial: \t{myBV}"); + + // Sets the third bit to TRUE. + myBV[myBit3] = true; + Console.WriteLine($"myBit3 = TRUE \t{myBV}"); + + // Combines two masks to access multiple bits at a time. + myBV[myBit4 + myBit5] = true; + Console.WriteLine($"myBit4 + myBit5 = TRUE \t{myBV}"); + myBV[myBit1 | myBit2] = true; + Console.WriteLine($"myBit1 | myBit2 = TRUE \t{myBV}"); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/Project.csproj b/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/bitvector32_equals.cs b/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/bitvector32_equals.cs index b8e330a8f67..e430616fa68 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/bitvector32_equals.cs +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/Equals/bitvector32_equals.cs @@ -4,57 +4,59 @@ using System; using System.Collections.Specialized; -public class SamplesBitVector32 { +public class SamplesBitVector32 +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a BitVector32 with the value 123. - // This is the BitVector32 that will be compared to different types. - BitVector32 myBV = new BitVector32( 123 ); + // Creates and initializes a BitVector32 with the value 123. + // This is the BitVector32 that will be compared to different types. + BitVector32 myBV = new(123); - // Creates and initializes a new BitVector32 which will be set up as sections. - BitVector32 myBVsect = new BitVector32( 0 ); + // Creates and initializes a new BitVector32 which will be set up as sections. + BitVector32 myBVsect = new(0); - // Compares myBV and myBVsect. - Console.WriteLine( "myBV : {0}", myBV.ToString() ); - Console.WriteLine( "myBVsect : {0}", myBVsect.ToString() ); - if ( myBV.Equals( myBVsect ) ) - Console.WriteLine( " myBV({0}) equals myBVsect({1}).", myBV.Data, myBVsect.Data ); - else - Console.WriteLine( " myBV({0}) does not equal myBVsect({1}).", myBV.Data, myBVsect.Data ); - Console.WriteLine(); + // Compares myBV and myBVsect. + Console.WriteLine($"myBV : {myBV}"); + Console.WriteLine($"myBVsect : {myBVsect}"); + if (myBV.Equals(myBVsect)) + Console.WriteLine($" myBV({myBV.Data}) equals myBVsect({myBVsect.Data})."); + else + Console.WriteLine($" myBV({myBV.Data}) does not equal myBVsect({myBVsect.Data})."); + Console.WriteLine(); - // Assigns values to the sections of myBVsect. - BitVector32.Section mySect1 = BitVector32.CreateSection( 5 ); - BitVector32.Section mySect2 = BitVector32.CreateSection( 1, mySect1 ); - BitVector32.Section mySect3 = BitVector32.CreateSection( 20, mySect2 ); - myBVsect[mySect1] = 3; - myBVsect[mySect2] = 1; - myBVsect[mySect3] = 7; + // Assigns values to the sections of myBVsect. + BitVector32.Section mySect1 = BitVector32.CreateSection(5); + BitVector32.Section mySect2 = BitVector32.CreateSection(1, mySect1); + BitVector32.Section mySect3 = BitVector32.CreateSection(20, mySect2); + myBVsect[mySect1] = 3; + myBVsect[mySect2] = 1; + myBVsect[mySect3] = 7; - // Compares myBV and myBVsect. - Console.WriteLine( "myBV : {0}", myBV.ToString() ); - Console.WriteLine( "myBVsect with values : {0}", myBVsect.ToString() ); - if ( myBV.Equals( myBVsect ) ) - Console.WriteLine( " myBV({0}) equals myBVsect({1}).", myBV.Data, myBVsect.Data ); - else - Console.WriteLine( " myBV({0}) does not equal myBVsect({1}).", myBV.Data, myBVsect.Data ); - Console.WriteLine(); + // Compares myBV and myBVsect. + Console.WriteLine($"myBV : {myBV}"); + Console.WriteLine($"myBVsect with values : {myBVsect}"); + if (myBV.Equals(myBVsect)) + Console.WriteLine($" myBV({myBV.Data}) equals myBVsect({myBVsect.Data})."); + else + Console.WriteLine($" myBV({myBV.Data}) does not equal myBVsect({myBVsect.Data})."); + Console.WriteLine(); - // Compare myBV with an Int32. - Console.WriteLine( "Comparing myBV with an Int32: " ); - Int32 myInt32 = 123; - // Using Equals will fail because Int32 is not compatible with BitVector32. - if ( myBV.Equals( myInt32 ) ) - Console.WriteLine( " Using BitVector32.Equals, myBV({0}) equals myInt32({1}).", myBV.Data, myInt32 ); - else - Console.WriteLine( " Using BitVector32.Equals, myBV({0}) does not equal myInt32({1}).", myBV.Data, myInt32 ); - // To compare a BitVector32 with an Int32, use the "==" operator. - if ( myBV.Data == myInt32 ) - Console.WriteLine( " Using the \"==\" operator, myBV.Data({0}) equals myInt32({1}).", myBV.Data, myInt32 ); - else - Console.WriteLine( " Using the \"==\" operator, myBV.Data({0}) does not equal myInt32({1}).", myBV.Data, myInt32 ); - } + // Compare myBV with an Int32. + Console.WriteLine("Comparing myBV with an Int32: "); + int myInt32 = 123; + // Using Equals will fail because Int32 is not compatible with BitVector32. + if (myBV.Equals(myInt32)) + Console.WriteLine($" Using BitVector32.Equals, myBV({myBV.Data}) equals myInt32({myInt32})."); + else + Console.WriteLine($" Using BitVector32.Equals, myBV({myBV.Data}) does not equal myInt32({myInt32})."); + // To compare a BitVector32 with an Int32, use the "==" operator. + if (myBV.Data == myInt32) + Console.WriteLine($" Using the \"==\" operator, myBV.Data({myBV.Data}) equals myInt32({myInt32})."); + else + Console.WriteLine($" Using the \"==\" operator, myBV.Data({myBV.Data}) does not equal myInt32({myInt32})."); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/bitvector32_bitflags.cs b/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/bitvector32_bitflags.cs index 24fe6453105..abcdfde3c27 100644 --- a/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/bitvector32_bitflags.cs +++ b/snippets/csharp/System.Collections.Specialized/BitVector32/Overview/bitvector32_bitflags.cs @@ -4,30 +4,32 @@ using System; using System.Collections.Specialized; -public class SamplesBitVector32 { - - public static void Main() { - - // Creates and initializes a BitVector32 with all bit flags set to FALSE. - BitVector32 myBV = new BitVector32( 0 ); - - // Creates masks to isolate each of the first five bit flags. - int myBit1 = BitVector32.CreateMask(); - int myBit2 = BitVector32.CreateMask( myBit1 ); - int myBit3 = BitVector32.CreateMask( myBit2 ); - int myBit4 = BitVector32.CreateMask( myBit3 ); - int myBit5 = BitVector32.CreateMask( myBit4 ); - - // Sets the alternating bits to TRUE. - Console.WriteLine( "Setting alternating bits to TRUE:" ); - Console.WriteLine( " Initial: {0}", myBV.ToString() ); - myBV[myBit1] = true; - Console.WriteLine( " myBit1 = TRUE: {0}", myBV.ToString() ); - myBV[myBit3] = true; - Console.WriteLine( " myBit3 = TRUE: {0}", myBV.ToString() ); - myBV[myBit5] = true; - Console.WriteLine( " myBit5 = TRUE: {0}", myBV.ToString() ); - } +public class SamplesBitVector32 +{ + + public static void Main() + { + + // Creates and initializes a BitVector32 with all bit flags set to FALSE. + BitVector32 myBV = new(0); + + // Creates masks to isolate each of the first five bit flags. + int myBit1 = BitVector32.CreateMask(); + int myBit2 = BitVector32.CreateMask(myBit1); + int myBit3 = BitVector32.CreateMask(myBit2); + int myBit4 = BitVector32.CreateMask(myBit3); + int myBit5 = BitVector32.CreateMask(myBit4); + + // Sets the alternating bits to TRUE. + Console.WriteLine("Setting alternating bits to TRUE:"); + Console.WriteLine($" Initial: {myBV}"); + myBV[myBit1] = true; + Console.WriteLine($" myBit1 = TRUE: {myBV}"); + myBV[myBit3] = true; + Console.WriteLine($" myBit3 = TRUE: {myBV}"); + myBV[myBit5] = true; + Console.WriteLine($" myBit5 = TRUE: {myBV}"); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/source.cs b/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/source.cs index 8f1cb8d6e8d..a8d0ec00b7c 100644 --- a/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/source.cs +++ b/snippets/csharp/System.Collections.Specialized/CollectionsUtil/Overview/source.cs @@ -16,28 +16,28 @@ public static void Main() // Select cities from the table using mixed case. Console.WriteLine("Case insensitive hashtable results:\n"); - Console.WriteLine("{0}'s population is: {1}", "Trapperville", population1["trapperville"]); - Console.WriteLine("{0}'s population is: {1}", "Doggerton", population1["DOGGERTON"]); - Console.WriteLine("{0}'s population is: {1}", "New Hollow", population1["New hoLLow"]); - Console.WriteLine("{0}'s population is: {1}", "McHenry", population1["MchenrY"]); + Console.WriteLine($"{"Trapperville"}'s population is: {population1["trapperville"]}"); + Console.WriteLine($"{"Doggerton"}'s population is: {population1["DOGGERTON"]}"); + Console.WriteLine($"{"New Hollow"}'s population is: {population1["New hoLLow"]}"); + Console.WriteLine($"{"McHenry"}'s population is: {population1["MchenrY"]}"); SortedList population2 = CollectionsUtil.CreateCaseInsensitiveSortedList(); foreach (string city in population1.Keys) { - population2.Add(city, population1[city]); + population2.Add(city, population1[city]); } // Select cities from the sorted list using mixed case. Console.WriteLine("\nCase insensitive sorted list results:\n"); - Console.WriteLine("{0}'s population is: {1}", "Trapperville", population2["trapPeRVille"]); - Console.WriteLine("{0}'s population is: {1}", "Doggerton", population2["dOGGeRtON"]); - Console.WriteLine("{0}'s population is: {1}", "New Hollow", population2["nEW hOLLOW"]); - Console.WriteLine("{0}'s population is: {1}", "McHenry", population2["MchEnrY"]); + Console.WriteLine($"{"Trapperville"}'s population is: {population2["trapPeRVille"]}"); + Console.WriteLine($"{"Doggerton"}'s population is: {population2["dOGGeRtON"]}"); + Console.WriteLine($"{"New Hollow"}'s population is: {population2["nEW hOLLOW"]}"); + Console.WriteLine($"{"McHenry"}'s population is: {population2["MchEnrY"]}"); } } -// This program displays the following output to the console +// This program displays the following output to the console. // // Case insensitive hashtable results: // diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/Project.csproj b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/hybriddictionary_addremove.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/hybriddictionary_addremove.cs index 67ea8e73b98..d4836bf15a5 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/hybriddictionary_addremove.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Add/hybriddictionary_addremove.cs @@ -5,52 +5,55 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesHybridDictionary { +public class SamplesHybridDictionary +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new HybridDictionary. - HybridDictionary myCol = new HybridDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - myCol.Add( "Plantain Bananas", "1.49" ); - myCol.Add( "Yellow Bananas", "0.79" ); - myCol.Add( "Strawberries", "3.33" ); - myCol.Add( "Cranberries", "5.98" ); - myCol.Add( "Navel Oranges", "1.29" ); - myCol.Add( "Grapes", "1.99" ); - myCol.Add( "Honeydew Melon", "0.59" ); - myCol.Add( "Seedless Watermelon", "0.49" ); - myCol.Add( "Pineapple", "1.49" ); - myCol.Add( "Nectarine", "1.99" ); - myCol.Add( "Plums", "1.69" ); - myCol.Add( "Peaches", "1.99" ); + // Creates and initializes a new HybridDictionary. + HybridDictionary myCol = new(); + myCol.Add("Braeburn Apples", "1.49"); + myCol.Add("Fuji Apples", "1.29"); + myCol.Add("Gala Apples", "1.49"); + myCol.Add("Golden Delicious Apples", "1.29"); + myCol.Add("Granny Smith Apples", "0.89"); + myCol.Add("Red Delicious Apples", "0.99"); + myCol.Add("Plantain Bananas", "1.49"); + myCol.Add("Yellow Bananas", "0.79"); + myCol.Add("Strawberries", "3.33"); + myCol.Add("Cranberries", "5.98"); + myCol.Add("Navel Oranges", "1.29"); + myCol.Add("Grapes", "1.99"); + myCol.Add("Honeydew Melon", "0.59"); + myCol.Add("Seedless Watermelon", "0.49"); + myCol.Add("Pineapple", "1.49"); + myCol.Add("Nectarine", "1.99"); + myCol.Add("Plums", "1.69"); + myCol.Add("Peaches", "1.99"); - // Displays the values in the HybridDictionary in three different ways. - Console.WriteLine( "Initial contents of the HybridDictionary:" ); - PrintKeysAndValues( myCol ); + // Displays the values in the HybridDictionary in three different ways. + Console.WriteLine("Initial contents of the HybridDictionary:"); + PrintKeysAndValues(myCol); - // Deletes a key. - myCol.Remove( "Plums" ); - Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); - PrintKeysAndValues( myCol ); + // Deletes a key. + myCol.Remove("Plums"); + Console.WriteLine("The collection contains the following elements after removing \"Plums\":"); + PrintKeysAndValues(myCol); - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues( myCol ); - } + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues(myCol); + } - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/Project.csproj b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/hybriddictionary_contains.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/hybriddictionary_contains.cs index ebe35fc4a45..e9e72f50c1d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/hybriddictionary_contains.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Contains/hybriddictionary_contains.cs @@ -5,49 +5,54 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesHybridDictionary { +public class SamplesHybridDictionary +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new HybridDictionary. - HybridDictionary myCol = new HybridDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - myCol.Add( "Plantain Bananas", "1.49" ); - myCol.Add( "Yellow Bananas", "0.79" ); - myCol.Add( "Strawberries", "3.33" ); - myCol.Add( "Cranberries", "5.98" ); - myCol.Add( "Navel Oranges", "1.29" ); - myCol.Add( "Grapes", "1.99" ); - myCol.Add( "Honeydew Melon", "0.59" ); - myCol.Add( "Seedless Watermelon", "0.49" ); - myCol.Add( "Pineapple", "1.49" ); - myCol.Add( "Nectarine", "1.99" ); - myCol.Add( "Plums", "1.69" ); - myCol.Add( "Peaches", "1.99" ); + // Creates and initializes a new HybridDictionary. + HybridDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" }, + { "Plantain Bananas", "1.49" }, + { "Yellow Bananas", "0.79" }, + { "Strawberries", "3.33" }, + { "Cranberries", "5.98" }, + { "Navel Oranges", "1.29" }, + { "Grapes", "1.99" }, + { "Honeydew Melon", "0.59" }, + { "Seedless Watermelon", "0.49" }, + { "Pineapple", "1.49" }, + { "Nectarine", "1.99" }, + { "Plums", "1.69" }, + { "Peaches", "1.99" } + }; - // Displays the values in the HybridDictionary in three different ways. - Console.WriteLine( "Initial contents of the HybridDictionary:" ); - PrintKeysAndValues( myCol ); + // Displays the values in the HybridDictionary in three different ways. + Console.WriteLine("Initial contents of the HybridDictionary:"); + PrintKeysAndValues(myCol); - // Searches for a key. - if ( myCol.Contains( "Kiwis" ) ) - Console.WriteLine( "The collection contains the key \"Kiwis\"." ); - else - Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); - Console.WriteLine(); - } + // Searches for a key. + if (myCol.Contains("Kiwis")) + Console.WriteLine("The collection contains the key \"Kiwis\"."); + else + Console.WriteLine("The collection does not contain the key \"Kiwis\"."); + Console.WriteLine(); + } - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/Project.csproj b/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/hybriddictionary_copyto.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/hybriddictionary_copyto.cs index cb1855868b4..f2ddf444283 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/hybriddictionary_copyto.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/CopyTo/hybriddictionary_copyto.cs @@ -5,53 +5,58 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesHybridDictionary { +public class SamplesHybridDictionary +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new HybridDictionary. - HybridDictionary myCol = new HybridDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - myCol.Add( "Plantain Bananas", "1.49" ); - myCol.Add( "Yellow Bananas", "0.79" ); - myCol.Add( "Strawberries", "3.33" ); - myCol.Add( "Cranberries", "5.98" ); - myCol.Add( "Navel Oranges", "1.29" ); - myCol.Add( "Grapes", "1.99" ); - myCol.Add( "Honeydew Melon", "0.59" ); - myCol.Add( "Seedless Watermelon", "0.49" ); - myCol.Add( "Pineapple", "1.49" ); - myCol.Add( "Nectarine", "1.99" ); - myCol.Add( "Plums", "1.69" ); - myCol.Add( "Peaches", "1.99" ); + // Creates and initializes a new HybridDictionary. + HybridDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" }, + { "Plantain Bananas", "1.49" }, + { "Yellow Bananas", "0.79" }, + { "Strawberries", "3.33" }, + { "Cranberries", "5.98" }, + { "Navel Oranges", "1.29" }, + { "Grapes", "1.99" }, + { "Honeydew Melon", "0.59" }, + { "Seedless Watermelon", "0.49" }, + { "Pineapple", "1.49" }, + { "Nectarine", "1.99" }, + { "Plums", "1.69" }, + { "Peaches", "1.99" } + }; - // Displays the values in the HybridDictionary in three different ways. - Console.WriteLine( "Initial contents of the HybridDictionary:" ); - PrintKeysAndValues( myCol ); + // Displays the values in the HybridDictionary in three different ways. + Console.WriteLine("Initial contents of the HybridDictionary:"); + PrintKeysAndValues(myCol); - // Copies the HybridDictionary to an array with DictionaryEntry elements. - DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; - myCol.CopyTo( myArr, 0 ); + // Copies the HybridDictionary to an array with DictionaryEntry elements. + DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; + myCol.CopyTo(myArr, 0); - // Displays the values in the array. - Console.WriteLine( "Displays the elements in the array:" ); - Console.WriteLine( " KEY VALUE" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - } + // Displays the values in the array. + Console.WriteLine("Displays the elements in the array:"); + Console.WriteLine(" KEY VALUE"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($" {myArr[i].Key,-25} {myArr[i].Value}"); + Console.WriteLine(); + } - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/Project.csproj b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/hybriddictionary_enumerator.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/hybriddictionary_enumerator.cs index 156e157f2ec..a47f069b518 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/hybriddictionary_enumerator.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Count/hybriddictionary_enumerator.cs @@ -5,73 +5,80 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesHybridDictionary { - - public static void Main() { - - // Creates and initializes a new HybridDictionary. - HybridDictionary myCol = new HybridDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - myCol.Add( "Plantain Bananas", "1.49" ); - myCol.Add( "Yellow Bananas", "0.79" ); - myCol.Add( "Strawberries", "3.33" ); - myCol.Add( "Cranberries", "5.98" ); - myCol.Add( "Navel Oranges", "1.29" ); - myCol.Add( "Grapes", "1.99" ); - myCol.Add( "Honeydew Melon", "0.59" ); - myCol.Add( "Seedless Watermelon", "0.49" ); - myCol.Add( "Pineapple", "1.49" ); - myCol.Add( "Nectarine", "1.99" ); - myCol.Add( "Plums", "1.69" ); - myCol.Add( "Peaches", "1.99" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( IDictionary myCol ) { - IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( " {0,-25} {1}", myEnumerator.Key, myEnumerator.Value ); - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( HybridDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesHybridDictionary +{ + + public static void Main() + { + + // Creates and initializes a new HybridDictionary. + HybridDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" }, + { "Plantain Bananas", "1.49" }, + { "Yellow Bananas", "0.79" }, + { "Strawberries", "3.33" }, + { "Cranberries", "5.98" }, + { "Navel Oranges", "1.29" }, + { "Grapes", "1.99" }, + { "Honeydew Melon", "0.59" }, + { "Seedless Watermelon", "0.49" }, + { "Pineapple", "1.49" }, + { "Nectarine", "1.99" }, + { "Plums", "1.69" }, + { "Peaches", "1.99" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IDictionaryEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(IDictionary myCol) + { + IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + Console.WriteLine($" {myEnumerator.Key,-25} {myEnumerator.Value}"); + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(HybridDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Program.cs new file mode 100644 index 00000000000..f5f0fd95a1e --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Program.cs @@ -0,0 +1,2 @@ +SamplesHybridDictionary.Run(); +HybridDictSample.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/hybriddictionary.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/hybriddictionary.cs index ca82f408c6f..849dcf255dc 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/hybriddictionary.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/hybriddictionary.cs @@ -5,101 +5,108 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesHybridDictionary { - - public static void Main() { - - // Creates and initializes a new HybridDictionary. - HybridDictionary myCol = new HybridDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - myCol.Add( "Plantain Bananas", "1.49" ); - myCol.Add( "Yellow Bananas", "0.79" ); - myCol.Add( "Strawberries", "3.33" ); - myCol.Add( "Cranberries", "5.98" ); - myCol.Add( "Navel Oranges", "1.29" ); - myCol.Add( "Grapes", "1.99" ); - myCol.Add( "Honeydew Melon", "0.59" ); - myCol.Add( "Seedless Watermelon", "0.49" ); - myCol.Add( "Pineapple", "1.49" ); - myCol.Add( "Nectarine", "1.99" ); - myCol.Add( "Plums", "1.69" ); - myCol.Add( "Peaches", "1.99" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - - // Copies the HybridDictionary to an array with DictionaryEntry elements. - DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; - myCol.CopyTo( myArr, 0 ); - - // Displays the values in the array. - Console.WriteLine( "Displays the elements in the array:" ); - Console.WriteLine( " KEY VALUE" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - - // Searches for a key. - if ( myCol.Contains( "Kiwis" ) ) - Console.WriteLine( "The collection contains the key \"Kiwis\"." ); - else - Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); - Console.WriteLine(); - - // Deletes a key. - myCol.Remove( "Plums" ); - Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); - PrintKeysAndValues1( myCol ); - - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues1( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( IDictionary myCol ) { - IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( " {0,-25} {1}", myEnumerator.Key, myEnumerator.Value ); - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( HybridDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesHybridDictionary +{ + + public static void Run() + { + + // Creates and initializes a new HybridDictionary. + HybridDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" }, + { "Plantain Bananas", "1.49" }, + { "Yellow Bananas", "0.79" }, + { "Strawberries", "3.33" }, + { "Cranberries", "5.98" }, + { "Navel Oranges", "1.29" }, + { "Grapes", "1.99" }, + { "Honeydew Melon", "0.59" }, + { "Seedless Watermelon", "0.49" }, + { "Pineapple", "1.49" }, + { "Nectarine", "1.99" }, + { "Plums", "1.69" }, + { "Peaches", "1.99" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IDictionaryEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + + // Copies the HybridDictionary to an array with DictionaryEntry elements. + DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; + myCol.CopyTo(myArr, 0); + + // Displays the values in the array. + Console.WriteLine("Displays the elements in the array:"); + Console.WriteLine(" KEY VALUE"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($" {myArr[i].Key,-25} {myArr[i].Value}"); + Console.WriteLine(); + + // Searches for a key. + if (myCol.Contains("Kiwis")) + Console.WriteLine("The collection contains the key \"Kiwis\"."); + else + Console.WriteLine("The collection does not contain the key \"Kiwis\"."); + Console.WriteLine(); + + // Deletes a key. + myCol.Remove("Plums"); + Console.WriteLine("The collection contains the following elements after removing \"Plums\":"); + PrintKeysAndValues1(myCol); + + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues1(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(IDictionary myCol) + { + IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + Console.WriteLine($" {myEnumerator.Key,-25} {myEnumerator.Value}"); + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(HybridDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/source2.cs b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/source2.cs index 74613c2b90a..95f3903790d 100644 --- a/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Specialized/HybridDictionary/Overview/source2.cs @@ -4,10 +4,10 @@ public class HybridDictSample { - public static void Main() + public static void Run() { // Creates and initializes a new HybridDictionary. - HybridDictionary myHybridDictionary = new HybridDictionary(); + HybridDictionary myHybridDictionary = []; // foreach (DictionaryEntry de in myHybridDictionary) @@ -17,8 +17,8 @@ public static void Main() // // - HybridDictionary myCollection = new HybridDictionary(); - lock(myCollection.SyncRoot) + HybridDictionary myCollection = []; + lock (myCollection.SyncRoot) { foreach (object item in myCollection) { diff --git a/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs b/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs index 90001005ac9..c9aeded3b70 100644 --- a/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs +++ b/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs @@ -10,7 +10,7 @@ public class People : IOrderedDictionary public People(int numItems) { - _people = new ArrayList(numItems); + _people = new(numItems); } public int IndexOfKey(object key) @@ -21,7 +21,7 @@ public int IndexOfKey(object key) return i; } - // key not found, return -1. + // Key not found. return -1; } @@ -37,7 +37,7 @@ public object this[object key] } } - // IOrderedDictionary Members + // IOrderedDictionary members. public IDictionaryEnumerator GetEnumerator() { return new PeopleEnum(_people); @@ -69,7 +69,7 @@ public object this[int index] _people[index] = new DictionaryEntry(key, value); } } - // IDictionary Members + // IDictionary members. public void Add(object key, object value) { @@ -97,30 +97,18 @@ public bool Contains(object key) } } - public bool IsFixedSize - { - get - { - return false; - } - } + public bool IsFixedSize => false; - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; public ICollection Keys { get { - ArrayList KeyCollection = new ArrayList(_people.Count); + ArrayList KeyCollection = new(_people.Count); for (int i = 0; i < _people.Count; i++) { - KeyCollection.Add( ((DictionaryEntry)_people[i]).Key ); + KeyCollection.Add(((DictionaryEntry)_people[i]).Key); } return KeyCollection; } @@ -135,52 +123,31 @@ public ICollection Values { get { - ArrayList ValueCollection = new ArrayList(_people.Count); + ArrayList ValueCollection = new(_people.Count); for (int i = 0; i < _people.Count; i++) { - ValueCollection.Add( ((DictionaryEntry)_people[i]).Value ); + ValueCollection.Add(((DictionaryEntry)_people[i]).Value); } return ValueCollection; } } - // ICollection Members + // ICollection members. public void CopyTo(Array array, int index) { _people.CopyTo(array, index); } - public int Count - { - get - { - return _people.Count; - } - } + public int Count => _people.Count; - public bool IsSynchronized - { - get - { - return _people.IsSynchronized; - } - } + public bool IsSynchronized => _people.IsSynchronized; - public object SyncRoot - { - get - { - return _people.SyncRoot; - } - } + public object SyncRoot => _people.SyncRoot; - // IEnumerable Members + // IEnumerable members. - IEnumerator IEnumerable.GetEnumerator() - { - return new PeopleEnum(_people); - } + IEnumerator IEnumerable.GetEnumerator() => new PeopleEnum(_people); } public class PeopleEnum : IDictionaryEnumerator @@ -189,23 +156,17 @@ public class PeopleEnum : IDictionaryEnumerator // Enumerators are positioned before the first element // until the first MoveNext() call. - int position = -1; + int _position = -1; - public PeopleEnum(ArrayList list) - { - _people = list; - } + public PeopleEnum(ArrayList list) => _people = list; public bool MoveNext() { - position++; - return (position < _people.Count); + _position++; + return (_position < _people.Count); } - public void Reset() - { - position = -1; - } + public void Reset() => _position = -1; public object Current { @@ -213,7 +174,7 @@ public object Current { try { - return _people[position]; + return _people[_position]; } catch (IndexOutOfRangeException) { @@ -222,13 +183,7 @@ public object Current } } - public DictionaryEntry Entry - { - get - { - return (DictionaryEntry)Current; - } - } + public DictionaryEntry Entry => (DictionaryEntry)Current; public object Key { @@ -236,7 +191,7 @@ public object Key { try { - return ((DictionaryEntry)_people[position]).Key; + return ((DictionaryEntry)_people[_position]).Key; } catch (IndexOutOfRangeException) { @@ -251,7 +206,7 @@ public object Value { try { - return ((DictionaryEntry)_people[position]).Value; + return ((DictionaryEntry)_people[_position]).Value; } catch (IndexOutOfRangeException) { @@ -262,19 +217,21 @@ public object Value } // -class App +class PeopleApp { - static void Main() + public static void Main() { - People peopleCollection = new People(3); - peopleCollection.Add("John", "Smith"); - peopleCollection.Add("Jim", "Johnson"); - peopleCollection.Add("Sue", "Rabon"); + People peopleCollection = new(3) + { + { "John", "Smith" }, + { "Jim", "Johnson" }, + { "Sue", "Rabon" } + }; Console.WriteLine("Displaying the entries in peopleCollection:"); foreach (DictionaryEntry de in peopleCollection) { - Console.WriteLine("{0} {1}", de.Key, de.Value); + Console.WriteLine($"{de.Key} {de.Value}"); } Console.WriteLine(); @@ -283,12 +240,15 @@ static void Main() peopleCollection.Remove("Sue"); peopleCollection.Insert(0, "Fred", "Anderson"); + // foreach (DictionaryEntry de in peopleCollection) { - Console.WriteLine("{0} {1}", de.Key, de.Value); + Console.WriteLine($"{de.Key} {de.Value}"); } + // } } + /* This code produces output similar to the following: * * Displaying the entries in peopleCollection: @@ -301,4 +261,5 @@ static void Main() * John Smith * Jim Jackson */ -// \ No newline at end of file + +// diff --git a/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.cs b/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.cs deleted file mode 100644 index 9744f2250a2..00000000000 --- a/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.cs +++ /dev/null @@ -1,283 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Specialized; - -public class SimpleOD : IOrderedDictionary -{ - private ArrayList itemlist; - - public SimpleOD(int numItems) - { - itemlist = new ArrayList(numItems); - } - - public int IndexOfKey(object key) - { - for (int i = 0; i < itemlist.Count; i++) - { - if (((DictionaryEntry)itemlist[i]).Key == key) - return i; - } - - // key not found, return -1. - return -1; - } - - public object this[object key] - { - get - { - return ((DictionaryEntry)itemlist[IndexOfKey(key)]).Value; - } - set - { - itemlist[IndexOfKey(key)] = new DictionaryEntry(key, value); - } - } - - // IOrderedDictionary Members - public IDictionaryEnumerator GetEnumerator() - { - return new ODEnum(itemlist); - } - - public void Insert(int index, object key, object value) - { - if (IndexOfKey(key) != -1) - { - throw new ArgumentException("An element with the same key already exists in the collection."); - } - itemlist.Insert(index, new DictionaryEntry(key, value)); - } - - public void RemoveAt(int index) - { - itemlist.RemoveAt(index); - } - - public object this[int index] - { - get - { - return ((DictionaryEntry)itemlist[index]).Value; - } - set - { - object key = ((DictionaryEntry)itemlist[index]).Key; - itemlist[index] = new DictionaryEntry(key, value); - } - } - // IDictionary Members - - public void Add(object key, object value) - { - if (IndexOfKey(key) != -1) - { - throw new ArgumentException("An element with the same key already exists in the collection."); - } - itemlist.Add(new DictionaryEntry(key, value)); - } - - public void Clear() - { - itemlist.Clear(); - } - - public bool Contains(object key) - { - if (IndexOfKey(key) == -1) - { - return false; - } - else - { - return true; - } - } - - public bool IsFixedSize - { - get - { - return false; - } - } - - public bool IsReadOnly - { - get - { - return false; - } - } - - public ICollection Keys - { - get - { - ArrayList KeyCollection = new ArrayList(itemlist.Count); - for (int i = 0; i < itemlist.Count; i++) - { - KeyCollection[i] = ((DictionaryEntry)itemlist[i]).Key; - } - return KeyCollection; - } - } - - public void Remove(object key) - { - itemlist.RemoveAt(IndexOfKey(key)); - } - - public ICollection Values - { - get - { - ArrayList ValueCollection = new ArrayList(itemlist.Count); - for (int i = 0; i < itemlist.Count; i++) - { - ValueCollection[i] = ((DictionaryEntry)itemlist[i]).Value; - } - return ValueCollection; - } - } - - // ICollection Members - - public void CopyTo(Array array, int index) - { - itemlist.CopyTo(array, index); - } - - public int Count - { - get - { - return itemlist.Count; - } - } - - public bool IsSynchronized - { - get - { - return itemlist.IsSynchronized; - } - } - - public object SyncRoot - { - get - { - return itemlist.SyncRoot; - } - } - - // IEnumerable Members - - IEnumerator IEnumerable.GetEnumerator() - { - return new ODEnum(itemlist); - } -} - -public class ODEnum : IDictionaryEnumerator -{ - public ArrayList itemlist; - - // Enumerators are positioned before the first element - // until the first MoveNext() call. - int position = -1; - - public ODEnum(ArrayList list) - { - itemlist = list; - } - - public bool MoveNext() - { - position++; - return (position < itemlist.Count); - } - - public void Reset() - { - position = -1; - } - - public object Current - { - get - { - try - { - return itemlist[position]; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } - } - } - - public DictionaryEntry Entry - { - get - { - return (DictionaryEntry)Current; - } - } - - public object Key - { - get - { - try - { - return ((DictionaryEntry)itemlist[position]).Key; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } - } - } - - public object Value - { - get - { - try - { - return ((DictionaryEntry)itemlist[position]).Value; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } - } - } -} - -class App -{ - static void Main() - { - int index = 1; - - SimpleOD myOrderedDictionary = new SimpleOD(2); - myOrderedDictionary.Add("Way", "ToGo"); - myOrderedDictionary.Add("Far", "Out"); - - object obj; - // - obj = myOrderedDictionary[index]; - // - // - foreach (DictionaryEntry de in myOrderedDictionary) - { - //... - } - // - } -} diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/Project.csproj b/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/listdictionary_addremove.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/listdictionary_addremove.cs index 758b03be73d..85a65590d65 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/listdictionary_addremove.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Add/listdictionary_addremove.cs @@ -5,40 +5,43 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary { +public class SamplesListDictionary +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new ListDictionary. - ListDictionary myCol = new ListDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); + // Creates and initializes a new ListDictionary. + ListDictionary myCol = new(); + myCol.Add("Braeburn Apples", "1.49"); + myCol.Add("Fuji Apples", "1.29"); + myCol.Add("Gala Apples", "1.49"); + myCol.Add("Golden Delicious Apples", "1.29"); + myCol.Add("Granny Smith Apples", "0.89"); + myCol.Add("Red Delicious Apples", "0.99"); - // Displays the values in the ListDictionary in three different ways. - Console.WriteLine( "Initial contents of the ListDictionary:" ); - PrintKeysAndValues( myCol ); + // Displays the values in the ListDictionary in three different ways. + Console.WriteLine("Initial contents of the ListDictionary:"); + PrintKeysAndValues(myCol); - // Deletes a key. - myCol.Remove( "Gala Apples" ); - Console.WriteLine( "The collection contains the following elements after removing \"Gala Apples\":" ); - PrintKeysAndValues( myCol ); + // Deletes a key. + myCol.Remove("Gala Apples"); + Console.WriteLine("The collection contains the following elements after removing \"Gala Apples\":"); + PrintKeysAndValues(myCol); - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues( myCol ); - } + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues(myCol); + } - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/Project.csproj b/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/listdictionary_contains.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/listdictionary_contains.cs index 69991e5601b..f7579b70e96 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/listdictionary_contains.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Contains/listdictionary_contains.cs @@ -5,37 +5,42 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary { - - public static void Main() { - - // Creates and initializes a new ListDictionary. - ListDictionary myCol = new ListDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - - // Displays the values in the ListDictionary in three different ways. - Console.WriteLine( "Initial contents of the ListDictionary:" ); - PrintKeysAndValues( myCol ); - - // Searches for a key. - if ( myCol.Contains( "Kiwis" ) ) - Console.WriteLine( "The collection contains the key \"Kiwis\"." ); - else - Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); - Console.WriteLine(); - } - - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } +public class SamplesListDictionary +{ + + public static void Main() + { + + // Creates and initializes a new ListDictionary. + ListDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" } + }; + + // Displays the values in the ListDictionary in three different ways. + Console.WriteLine("Initial contents of the ListDictionary:"); + PrintKeysAndValues(myCol); + + // Searches for a key. + if (myCol.Contains("Kiwis")) + Console.WriteLine("The collection contains the key \"Kiwis\"."); + else + Console.WriteLine("The collection does not contain the key \"Kiwis\"."); + Console.WriteLine(); + } + + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/Project.csproj b/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/listdictionary_copyto.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/listdictionary_copyto.cs index e865765e5b1..1fb219b16e9 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/listdictionary_copyto.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/CopyTo/listdictionary_copyto.cs @@ -5,41 +5,46 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary { +public class SamplesListDictionary +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new ListDictionary. - ListDictionary myCol = new ListDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); + // Creates and initializes a new ListDictionary. + ListDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" } + }; - // Displays the values in the ListDictionary in three different ways. - Console.WriteLine( "Initial contents of the ListDictionary:" ); - PrintKeysAndValues( myCol ); + // Displays the values in the ListDictionary in three different ways. + Console.WriteLine("Initial contents of the ListDictionary:"); + PrintKeysAndValues(myCol); - // Copies the ListDictionary to an array with DictionaryEntry elements. - DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; - myCol.CopyTo( myArr, 0 ); + // Copies the ListDictionary to an array with DictionaryEntry elements. + DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; + myCol.CopyTo(myArr, 0); - // Displays the values in the array. - Console.WriteLine( "Displays the elements in the array:" ); - Console.WriteLine( " KEY VALUE" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - } + // Displays the values in the array. + Console.WriteLine("Displays the elements in the array:"); + Console.WriteLine(" KEY VALUE"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($" {myArr[i].Key,-25} {myArr[i].Value}"); + Console.WriteLine(); + } - public static void PrintKeysAndValues( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } + public static void PrintKeysAndValues(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } } diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/Project.csproj b/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/listdictionary_enumerator.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/listdictionary_enumerator.cs index e721cfd9145..dfc9f909072 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/listdictionary_enumerator.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Count/listdictionary_enumerator.cs @@ -5,61 +5,68 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary { - - public static void Main() { - - // Creates and initializes a new ListDictionary. - ListDictionary myCol = new ListDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( IDictionary myCol ) { - IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( " {0,-25} {1}", myEnumerator.Key, myEnumerator.Value ); - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( ListDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesListDictionary +{ + + public static void Main() + { + + // Creates and initializes a new ListDictionary. + ListDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IDictionaryEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(IDictionary myCol) + { + IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + Console.WriteLine($" {myEnumerator.Key,-25} {myEnumerator.Value}"); + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(ListDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Program.cs new file mode 100644 index 00000000000..d7f2986fdea --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Program.cs @@ -0,0 +1,2 @@ +SamplesListDictionary.Run(); +SamplesListDictionarySyncRoot.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/listdictionary.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/listdictionary.cs index 31e0efaad5c..b8c9d2783f2 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/listdictionary.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/listdictionary.cs @@ -5,89 +5,96 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary { - - public static void Main() { - - // Creates and initializes a new ListDictionary. - ListDictionary myCol = new ListDictionary(); - myCol.Add( "Braeburn Apples", "1.49" ); - myCol.Add( "Fuji Apples", "1.29" ); - myCol.Add( "Gala Apples", "1.49" ); - myCol.Add( "Golden Delicious Apples", "1.29" ); - myCol.Add( "Granny Smith Apples", "0.89" ); - myCol.Add( "Red Delicious Apples", "0.99" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - - // Copies the ListDictionary to an array with DictionaryEntry elements. - DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; - myCol.CopyTo( myArr, 0 ); - - // Displays the values in the array. - Console.WriteLine( "Displays the elements in the array:" ); - Console.WriteLine( " KEY VALUE" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - - // Searches for a key. - if ( myCol.Contains( "Kiwis" ) ) - Console.WriteLine( "The collection contains the key \"Kiwis\"." ); - else - Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); - Console.WriteLine(); - - // Deletes a key. - myCol.Remove( "Plums" ); - Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); - PrintKeysAndValues1( myCol ); - - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues1( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( IDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( IDictionary myCol ) { - IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( " {0,-25} {1}", myEnumerator.Key, myEnumerator.Value ); - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( ListDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesListDictionary +{ + + public static void Run() + { + + // Creates and initializes a new ListDictionary. + ListDictionary myCol = new() + { + { "Braeburn Apples", "1.49" }, + { "Fuji Apples", "1.29" }, + { "Gala Apples", "1.49" }, + { "Golden Delicious Apples", "1.29" }, + { "Granny Smith Apples", "0.89" }, + { "Red Delicious Apples", "0.99" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IDictionaryEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + + // Copies the ListDictionary to an array with DictionaryEntry elements. + DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; + myCol.CopyTo(myArr, 0); + + // Displays the values in the array. + Console.WriteLine("Displays the elements in the array:"); + Console.WriteLine(" KEY VALUE"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($" {myArr[i].Key,-25} {myArr[i].Value}"); + Console.WriteLine(); + + // Searches for a key. + if (myCol.Contains("Kiwis")) + Console.WriteLine("The collection contains the key \"Kiwis\"."); + else + Console.WriteLine("The collection does not contain the key \"Kiwis\"."); + Console.WriteLine(); + + // Deletes a key. + myCol.Remove("Plums"); + Console.WriteLine("The collection contains the following elements after removing \"Plums\":"); + PrintKeysAndValues1(myCol); + + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues1(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(IDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(IDictionary myCol) + { + IDictionaryEnumerator myEnumerator = myCol.GetEnumerator(); + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + Console.WriteLine($" {myEnumerator.Key,-25} {myEnumerator.Value}"); + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(ListDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/source2.cs b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/source2.cs index c50fe2eafef..8b4ef603a60 100644 --- a/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Specialized/ListDictionary/Overview/source2.cs @@ -2,13 +2,13 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesListDictionary +public class SamplesListDictionarySyncRoot { - public static void Main() + public static void Run() { // - ListDictionary myCollection = new ListDictionary(); - lock(myCollection.SyncRoot) + ListDictionary myCollection = []; + lock (myCollection.SyncRoot) { foreach (object item in myCollection) { @@ -20,7 +20,7 @@ public static void Main() public static void Dummy() { - ListDictionary myListDictionary = new ListDictionary(); + ListDictionary myListDictionary = []; // foreach (DictionaryEntry de in myListDictionary) { diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/source.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/source.cs index d91cb46b3a0..26a31d06982 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/source.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase+KeysCollection/System.Collections.ICollection.IsSynchronized/source.cs @@ -4,83 +4,103 @@ public class DerivedCollection : NameObjectCollectionBase { - // Creates an empty collection. - public DerivedCollection() { - } + // Creates an empty collection. + public DerivedCollection() + { + } - // Adds elements from an IDictionary into the new collection. - public DerivedCollection( IDictionary d, Boolean bReadOnly ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - this.IsReadOnly = bReadOnly; - } + // Adds elements from an IDictionary into the new collection. + public DerivedCollection(IDictionary d, bool bReadOnly) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + this.IsReadOnly = bReadOnly; + } - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - return ( new DictionaryEntry( - this.BaseGetKey(index), this.BaseGet(index) ) ); - } - } + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + return (new DictionaryEntry( + this.BaseGetKey(index), this.BaseGet(index))); + } + } - // Gets or sets the value associated with the specified key. - public Object this[ String key ] { - get { - return( this.BaseGet( key ) ); - } - set { - this.BaseSet( key, value ); - } - } + // Gets or sets the value associated with the specified key. + public object this[string key] + { + get + { + return (this.BaseGet(key)); + } + set + { + this.BaseSet(key, value); + } + } - // Gets a String array that contains all the keys in the collection. - public String[] AllKeys { - get { - return( this.BaseGetAllKeys() ); - } - } + // Gets a String array that contains all the keys in the collection. + public string[] AllKeys + { + get + { + return (this.BaseGetAllKeys()); + } + } - // Gets an Object array that contains all the values in the collection. - public Array AllValues { - get { - return( this.BaseGetAllValues() ); - } - } + // Gets an Object array that contains all the values in the collection. + public Array AllValues + { + get + { + return (this.BaseGetAllValues()); + } + } - // Gets a String array that contains all the values in the collection. - public String[] AllStringValues { - get { - return( (String[]) this.BaseGetAllValues( typeof( string ) )); - } - } + // Gets a String array that contains all the values in the collection. + public string[] AllStringValues + { + get + { + return ((string[])this.BaseGetAllValues(typeof(string))); + } + } - // Gets a value indicating if the collection contains keys that are not null. - public Boolean HasKeys { - get { - return( this.BaseHasKeys() ); - } - } + // Gets a value indicating if the collection contains keys that are not null. + public bool HasKeys + { + get + { + return (this.BaseHasKeys()); + } + } - // Adds an entry to the collection. - public void Add( String key, Object value ) { - this.BaseAdd( key, value ); - } + // Adds an entry to the collection. + public void Add(string key, object value) + { + this.BaseAdd(key, value); + } - // Removes an entry with the specified key from the collection. - public void Remove( String key ) { - this.BaseRemove( key ); - } + // Removes an entry with the specified key from the collection. + public void Remove(string key) + { + this.BaseRemove(key); + } - // Removes an entry in the specified index from the collection. - public void Remove( int index ) { - this.BaseRemoveAt( index ); - } + // Removes an entry in the specified index from the collection. + public void Remove(int index) + { + this.BaseRemoveAt(index); + } - // Clears all the elements in the collection. - public void Clear() { - this.BaseClear(); - } + // Clears all the elements in the collection. + public void Clear() + { + this.BaseClear(); + } } public class SamplesNameObjectCollectionBaseKeys @@ -88,11 +108,11 @@ public class SamplesNameObjectCollectionBaseKeys public static void Main() { // - // Create a collection derived from NameObjectCollectionBase + // Create a collection derived from NameObjectCollectionBase. NameObjectCollectionBase myBaseCollection = new DerivedCollection(); - // Get the ICollection from NameObjectCollectionBase.KeysCollection + // Get the ICollection from NameObjectCollectionBase.KeysCollection. ICollection myKeysCollection = myBaseCollection.Keys; - lock(myKeysCollection.SyncRoot) + lock (myKeysCollection.SyncRoot) { foreach (object item in myKeysCollection) { diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/nocb_baseadd.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/nocb_baseadd.cs index b5bac8bf5fa..969439ba96f 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/nocb_baseadd.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseAdd/nocb_baseadd.cs @@ -6,43 +6,53 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - private DictionaryEntry _de = new DictionaryEntry(); - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } +public class MyCollection : NameObjectCollectionBase +{ + + private DictionaryEntry _de = new(); + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } } -public class SamplesNameObjectCollectionBase { +public class SamplesNameObjectCollectionBase +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); - // Displays the keys and values of the MyCollection instance. - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } + // Displays the keys and values of the MyCollection instance. + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/nocb_baseclear.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/nocb_baseclear.cs index 48f016093f4..c1a780127d4 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/nocb_baseclear.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseClear/nocb_baseclear.cs @@ -6,56 +6,68 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { +public class MyCollection : NameObjectCollectionBase +{ - private DictionaryEntry _de = new DictionaryEntry(); + private DictionaryEntry _de = new(); - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } - // Clears all the elements in the collection. - public void Clear() { - this.BaseClear(); - } + // Clears all the elements in the collection. + public void Clear() + { + this.BaseClear(); + } } -public class SamplesNameObjectCollectionBase { +public class SamplesNameObjectCollectionBase +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); - // Removes all elements from the collection. - myCol.Clear(); - Console.WriteLine( "After clearing the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - } + // Removes all elements from the collection. + myCol.Clear(); + Console.WriteLine($"After clearing the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + } - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/nocb_basegetall.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/nocb_basegetall.cs index e9b99edc936..6ec00372aa4 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/nocb_basegetall.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetAllKeys/nocb_basegetall.cs @@ -6,85 +6,105 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - private DictionaryEntry _de = new DictionaryEntry(); - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } - - // Gets a String array that contains all the keys in the collection. - public String[] AllKeys { - get { - return( this.BaseGetAllKeys() ); - } - } - - // Gets an Object array that contains all the values in the collection. - public Array AllValues { - get { - return( this.BaseGetAllValues() ); - } - } - - // Gets a String array that contains all the values in the collection. - public String[] AllStringValues { - get { - return( (String[]) this.BaseGetAllValues( typeof(System.String) ) ); - } - } +public class MyCollection : NameObjectCollectionBase +{ + + private DictionaryEntry _de = new(); + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } + + // Gets a String array that contains all the keys in the collection. + public string[] AllKeys + { + get + { + return (this.BaseGetAllKeys()); + } + } + + // Gets an Object array that contains all the values in the collection. + public Array AllValues + { + get + { + return (this.BaseGetAllValues()); + } + } + + // Gets a String array that contains all the values in the collection. + public string[] AllStringValues + { + get + { + return ((string[])this.BaseGetAllValues(typeof(string))); + } + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - - // Displays the list of keys. - Console.WriteLine( "The list of keys:" ); - foreach ( String s in myCol.AllKeys ) { - Console.WriteLine( " {0}", s ); - } - - // Displays the list of values of type Object. - Console.WriteLine( "The list of values (Object):" ); - foreach ( Object o in myCol.AllValues ) { - Console.WriteLine( " {0}", o.ToString() ); - } - - // Displays the list of values of type String. - Console.WriteLine( "The list of values (String):" ); - foreach ( String s in myCol.AllValues ) { - Console.WriteLine( " {0}", s ); - } - } - - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Main() + { + + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + + // Displays the list of keys. + Console.WriteLine("The list of keys:"); + foreach (string s in myCol.AllKeys) + { + Console.WriteLine($" {s}"); + } + + // Displays the list of values of type Object. + Console.WriteLine("The list of values (Object):"); + foreach (object o in myCol.AllValues) + { + Console.WriteLine($" {o}"); + } + + // Displays the list of values of type String. + Console.WriteLine("The list of values (String):"); + foreach (string s in myCol.AllValues) + { + Console.WriteLine($" {s}"); + } + } + + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/nocb_baseget.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/nocb_baseget.cs index e1d96b3eb24..131ec2f01e5 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/nocb_baseget.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseGetKey/nocb_baseget.cs @@ -6,61 +6,75 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { +public class MyCollection : NameObjectCollectionBase +{ - private DictionaryEntry _de = new DictionaryEntry(); + private DictionaryEntry _de = new(); - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } - // Gets or sets the value associated with the specified key. - public Object this[ String key ] { - get { - return( this.BaseGet( key ) ); - } - set { - this.BaseSet( key, value ); - } - } + // Gets or sets the value associated with the specified key. + public object this[string key] + { + get + { + return (this.BaseGet(key)); + } + set + { + this.BaseSet(key, value); + } + } - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } } -public class SamplesNameObjectCollectionBase { +public class SamplesNameObjectCollectionBase +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); - // Gets specific keys and values. - Console.WriteLine( "The key at index 0 is {0}.", myCol[0].Key ); - Console.WriteLine( "The value at index 0 is {0}.", myCol[0].Value ); - Console.WriteLine( "The value associated with the key \"green\" is {0}.", myCol["green"] ); - } + // Gets specific keys and values. + Console.WriteLine($"The key at index 0 is {myCol[0].Key}."); + Console.WriteLine($"The value at index 0 is {myCol[0].Value}."); + Console.WriteLine($"The value associated with the key \"green\" is {myCol["green"]}."); + } - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/nocb_basehaskeys.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/nocb_basehaskeys.cs index df0338a7053..facfa15cd82 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/nocb_basehaskeys.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseHasKeys/nocb_basehaskeys.cs @@ -6,60 +6,71 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - private DictionaryEntry _de = new DictionaryEntry(); - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } - - // Creates an empty collection. - public MyCollection() { - } - - // Adds an entry to the collection. - public void Add( String key, Object value ) { - this.BaseAdd( key, value ); - } - - // Gets a value indicating whether the collection contains keys that are not a null reference. - public Boolean HasKeys { - get { - return( this.BaseHasKeys() ); - } - } +public class MyCollection : NameObjectCollectionBase +{ + + private DictionaryEntry _de = new(); + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } + + // Creates an empty collection. + public MyCollection() + { + } + + // Adds an entry to the collection. + public void Add(string key, object value) + { + this.BaseAdd(key, value); + } + + // Gets a value indicating whether the collection contains keys that are not a null reference. + public bool HasKeys + { + get + { + return (this.BaseHasKeys()); + } + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates an empty MyCollection instance. - MyCollection myCol = new MyCollection(); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - Console.WriteLine( "HasKeys? {0}", myCol.HasKeys ); - - Console.WriteLine(); - - // Adds an item to the collection. - myCol.Add( "blue", "sky" ); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - Console.WriteLine( "HasKeys? {0}", myCol.HasKeys ); - } - - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Main() + { + + // Creates an empty MyCollection instance. + MyCollection myCol = []; + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + Console.WriteLine($"HasKeys? {myCol.HasKeys}"); + + Console.WriteLine(); + + // Adds an item to the collection. + myCol.Add("blue", "sky"); + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + Console.WriteLine($"HasKeys? {myCol.HasKeys}"); + } + + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/nocb_baseremove.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/nocb_baseremove.cs index e57e6c434d8..1f79d0a78cb 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/nocb_baseremove.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseRemove/nocb_baseremove.cs @@ -6,66 +6,79 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - private DictionaryEntry _de = new DictionaryEntry(); - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } - - // Removes an entry with the specified key from the collection. - public void Remove( String key ) { - this.BaseRemove( key ); - } - - // Removes an entry in the specified index from the collection. - public void Remove( int index ) { - this.BaseRemoveAt( index ); - } +public class MyCollection : NameObjectCollectionBase +{ + + private DictionaryEntry _de = new(); + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } + + // Removes an entry with the specified key from the collection. + public void Remove(string key) + { + this.BaseRemove(key); + } + + // Removes an entry in the specified index from the collection. + public void Remove(int index) + { + this.BaseRemoveAt(index); + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); - Console.WriteLine( "Initial state of the collection (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - - // Removes an element at a specific index. - myCol.Remove( 1 ); - Console.WriteLine( "After removing the element at index 1 (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - - // Removes an element with a specific key. - myCol.Remove( "red" ); - Console.WriteLine( "After removing the element with the key \"red\" (Count = {0}):", myCol.Count ); - PrintKeysAndValues( myCol ); - } - - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Main() + { + + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); + Console.WriteLine($"Initial state of the collection (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + + // Removes an element at a specific index. + myCol.Remove(1); + Console.WriteLine($"After removing the element at index 1 (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + + // Removes an element with a specific key. + myCol.Remove("red"); + Console.WriteLine($"After removing the element with the key \"red\" (Count = {myCol.Count}):"); + PrintKeysAndValues(myCol); + } + + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/nocb_baseset.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/nocb_baseset.cs index 0eb3dd28fae..30937377bd3 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/nocb_baseset.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/BaseSet/nocb_baseset.cs @@ -6,74 +6,91 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - // Gets or sets the value at the specified index. - public Object this[ int index ] { - get { - return( this.BaseGet( index ) ); - } - set { - this.BaseSet( index, value ); - } - } - - // Gets or sets the value associated with the specified key. - public Object this[ String key ] { - get { - return( this.BaseGet( key ) ); - } - set { - this.BaseSet( key, value ); - } - } - - // Gets a String array that contains all the keys in the collection. - public String[] AllKeys { - get { - return( this.BaseGetAllKeys() ); - } - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - } +public class MyCollection : NameObjectCollectionBase +{ + + // Gets or sets the value at the specified index. + public object this[int index] + { + get + { + return (this.BaseGet(index)); + } + set + { + this.BaseSet(index, value); + } + } + + // Gets or sets the value associated with the specified key. + public object this[string key] + { + get + { + return (this.BaseGet(key)); + } + set + { + this.BaseSet(key, value); + } + } + + // Gets a String array that contains all the keys in the collection. + public string[] AllKeys + { + get + { + return (this.BaseGetAllKeys()); + } + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates and initializes a new MyCollection instance. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myCol = new MyCollection( d ); - Console.WriteLine( "Initial state of the collection:" ); - PrintKeysAndValues2( myCol ); - Console.WriteLine(); - - // Sets the value at index 1. - myCol[1] = "sunflower"; - Console.WriteLine( "After setting the value at index 1:" ); - PrintKeysAndValues2( myCol ); - Console.WriteLine(); - - // Sets the value associated with the key "red". - myCol["red"] = "tulip"; - Console.WriteLine( "After setting the value associated with the key \"red\":" ); - PrintKeysAndValues2( myCol ); - } - - public static void PrintKeysAndValues2( MyCollection myCol ) { - foreach ( String s in myCol.AllKeys ) { - Console.WriteLine( "{0}, {1}", s, myCol[s] ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Main() + { + + // Creates and initializes a new MyCollection instance. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myCol = new(d); + Console.WriteLine("Initial state of the collection:"); + PrintKeysAndValues2(myCol); + Console.WriteLine(); + + // Sets the value at index 1. + myCol[1] = "sunflower"; + Console.WriteLine("After setting the value at index 1:"); + PrintKeysAndValues2(myCol); + Console.WriteLine(); + + // Sets the value associated with the key "red". + myCol["red"] = "tulip"; + Console.WriteLine("After setting the value associated with the key \"red\":"); + PrintKeysAndValues2(myCol); + } + + public static void PrintKeysAndValues2(MyCollection myCol) + { + foreach (string s in myCol.AllKeys) + { + Console.WriteLine($"{s}, {myCol[s]}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/nocb_isreadonly.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/nocb_isreadonly.cs index 8e42018f9c0..d90528e35b8 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/nocb_isreadonly.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/IsReadOnly/nocb_isreadonly.cs @@ -6,62 +6,76 @@ using System.Collections; using System.Collections.Specialized; -public class MyCollection : NameObjectCollectionBase { - - private DictionaryEntry _de = new DictionaryEntry(); - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - _de.Key = this.BaseGetKey( index ); - _de.Value = this.BaseGet( index ); - return( _de ); - } - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d, Boolean bReadOnly ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - this.IsReadOnly = bReadOnly; - } - - // Adds an entry to the collection. - public void Add( String key, Object value ) { - this.BaseAdd( key, value ); - } +public class MyCollection : NameObjectCollectionBase +{ + + private DictionaryEntry _de = new(); + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + _de.Key = this.BaseGetKey(index); + _de.Value = this.BaseGet(index); + return (_de); + } + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d, bool bReadOnly) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + this.IsReadOnly = bReadOnly; + } + + // Adds an entry to the collection. + public void Add(string key, object value) + { + this.BaseAdd(key, value); + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates and initializes a new MyCollection that is read-only. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myROCol = new MyCollection( d, true ); - - // Tries to add a new item. - try { - myROCol.Add( "blue", "sky" ); - } - catch ( NotSupportedException e ) { - Console.WriteLine( e.ToString() ); - } - - // Displays the keys and values of the MyCollection. - Console.WriteLine( "Read-Only Collection:" ); - PrintKeysAndValues( myROCol ); - } - - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Main() + { + + // Creates and initializes a new MyCollection that is read-only. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myROCol = new(d, true); + + // Tries to add a new item. + try + { + myROCol.Add("blue", "sky"); + } + catch (NotSupportedException e) + { + Console.WriteLine(e); + } + + // Displays the keys and values of the MyCollection. + Console.WriteLine("Read-Only Collection:"); + PrintKeysAndValues(myROCol); + } + + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } } diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Program.cs new file mode 100644 index 00000000000..dc3d0d96398 --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Program.cs @@ -0,0 +1,2 @@ +SamplesNameObjectCollectionBase.Run(); +SamplesDerivedCollection.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/nameobjectcollectionbase.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/nameobjectcollectionbase.cs index 24eaf6682ff..68430a0eb3a 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/nameobjectcollectionbase.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/nameobjectcollectionbase.cs @@ -7,147 +7,178 @@ public class MyCollection : NameObjectCollectionBase { - // Creates an empty collection. - public MyCollection() { - } - - // Adds elements from an IDictionary into the new collection. - public MyCollection( IDictionary d, Boolean bReadOnly ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - this.IsReadOnly = bReadOnly; - } - - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - return ( new DictionaryEntry( - this.BaseGetKey(index), this.BaseGet(index) ) ); - } - } - - // Gets or sets the value associated with the specified key. - public Object this[ String key ] { - get { - return( this.BaseGet( key ) ); - } - set { - this.BaseSet( key, value ); - } - } - - // Gets a String array that contains all the keys in the collection. - public String[] AllKeys { - get { - return( this.BaseGetAllKeys() ); - } - } - - // Gets an Object array that contains all the values in the collection. - public Array AllValues { - get { - return( this.BaseGetAllValues() ); - } - } - - // Gets a String array that contains all the values in the collection. - public String[] AllStringValues { - get { - return( (String[]) this.BaseGetAllValues( typeof( string ) )); - } - } - - // Gets a value indicating if the collection contains keys that are not null. - public Boolean HasKeys { - get { - return( this.BaseHasKeys() ); - } - } - - // Adds an entry to the collection. - public void Add( String key, Object value ) { - this.BaseAdd( key, value ); - } - - // Removes an entry with the specified key from the collection. - public void Remove( String key ) { - this.BaseRemove( key ); - } - - // Removes an entry in the specified index from the collection. - public void Remove( int index ) { - this.BaseRemoveAt( index ); - } - - // Clears all the elements in the collection. - public void Clear() { - this.BaseClear(); - } + // Creates an empty collection. + public MyCollection() + { + } + + // Adds elements from an IDictionary into the new collection. + public MyCollection(IDictionary d, bool bReadOnly) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + this.IsReadOnly = bReadOnly; + } + + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + return (new DictionaryEntry( + this.BaseGetKey(index), this.BaseGet(index))); + } + } + + // Gets or sets the value associated with the specified key. + public object this[string key] + { + get + { + return (this.BaseGet(key)); + } + set + { + this.BaseSet(key, value); + } + } + + // Gets a String array that contains all the keys in the collection. + public string[] AllKeys + { + get + { + return (this.BaseGetAllKeys()); + } + } + + // Gets an Object array that contains all the values in the collection. + public Array AllValues + { + get + { + return (this.BaseGetAllValues()); + } + } + + // Gets a String array that contains all the values in the collection. + public string[] AllStringValues + { + get + { + return ((string[])this.BaseGetAllValues(typeof(string))); + } + } + + // Gets a value indicating if the collection contains keys that are not null. + public bool HasKeys + { + get + { + return (this.BaseHasKeys()); + } + } + + // Adds an entry to the collection. + public void Add(string key, object value) + { + this.BaseAdd(key, value); + } + + // Removes an entry with the specified key from the collection. + public void Remove(string key) + { + this.BaseRemove(key); + } + + // Removes an entry in the specified index from the collection. + public void Remove(int index) + { + this.BaseRemoveAt(index); + } + + // Clears all the elements in the collection. + public void Clear() + { + this.BaseClear(); + } } -public class SamplesNameObjectCollectionBase { - - public static void Main() { - - // Creates and initializes a new MyCollection that is read-only. - IDictionary d = new ListDictionary(); - d.Add( "red", "apple" ); - d.Add( "yellow", "banana" ); - d.Add( "green", "pear" ); - MyCollection myROCol = new MyCollection( d, true ); - - // Tries to add a new item. - try { - myROCol.Add( "blue", "sky" ); - } - catch ( NotSupportedException e ) { - Console.WriteLine( e.ToString() ); - } - - // Displays the keys and values of the MyCollection. - Console.WriteLine( "Read-Only Collection:" ); - PrintKeysAndValues( myROCol ); - - // Creates and initializes an empty MyCollection that is writable. - MyCollection myRWCol = new MyCollection(); - - // Adds new items to the collection. - myRWCol.Add( "purple", "grape" ); - myRWCol.Add( "orange", "tangerine" ); - myRWCol.Add( "black", "berries" ); - Console.WriteLine( "Writable Collection (after adding values):" ); - PrintKeysAndValues( myRWCol ); - - // Changes the value of one element. - myRWCol["orange"] = "grapefruit"; - Console.WriteLine( "Writable Collection (after changing one value):" ); - PrintKeysAndValues( myRWCol ); - - // Removes one item from the collection. - myRWCol.Remove( "black" ); - Console.WriteLine( "Writable Collection (after removing one value):" ); - PrintKeysAndValues( myRWCol ); - - // Removes all elements from the collection. - myRWCol.Clear(); - Console.WriteLine( "Writable Collection (after clearing the collection):" ); - PrintKeysAndValues( myRWCol ); - } - - // Prints the indexes, keys, and values. - public static void PrintKeysAndValues( MyCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) { - Console.WriteLine( "[{0}] : {1}, {2}", i, myCol[i].Key, myCol[i].Value ); - } - } - - // Prints the keys and values using AllKeys. - public static void PrintKeysAndValues2( MyCollection myCol ) { - foreach ( String s in myCol.AllKeys ) { - Console.WriteLine( "{0}, {1}", s, myCol[s] ); - } - } +public class SamplesNameObjectCollectionBase +{ + + public static void Run() + { + + // Creates and initializes a new MyCollection that is read-only. + IDictionary d = new ListDictionary + { + { "red", "apple" }, + { "yellow", "banana" }, + { "green", "pear" } + }; + MyCollection myROCol = new(d, true); + + // Tries to add a new item. + try + { + myROCol.Add("blue", "sky"); + } + catch (NotSupportedException e) + { + Console.WriteLine(e); + } + + // Displays the keys and values of the MyCollection. + Console.WriteLine("Read-Only Collection:"); + PrintKeysAndValues(myROCol); + + // Creates and initializes an empty MyCollection that is writable. + MyCollection myRWCol = new() + { + // Adds new items to the collection. + { "purple", "grape" }, + { "orange", "tangerine" }, + { "black", "berries" } + }; + Console.WriteLine("Writable Collection (after adding values):"); + PrintKeysAndValues(myRWCol); + + // Changes the value of one element. + myRWCol["orange"] = "grapefruit"; + Console.WriteLine("Writable Collection (after changing one value):"); + PrintKeysAndValues(myRWCol); + + // Removes one item from the collection. + myRWCol.Remove("black"); + Console.WriteLine("Writable Collection (after removing one value):"); + PrintKeysAndValues(myRWCol); + + // Removes all elements from the collection. + myRWCol.Clear(); + Console.WriteLine("Writable Collection (after clearing the collection):"); + PrintKeysAndValues(myRWCol); + } + + // Prints the indexes, keys, and values. + public static void PrintKeysAndValues(MyCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + { + Console.WriteLine($"[{i}] : {myCol[i].Key}, {myCol[i].Value}"); + } + } + + // Prints the keys and values using AllKeys. + public static void PrintKeysAndValues2(MyCollection myCol) + { + foreach (string s in myCol.AllKeys) + { + Console.WriteLine($"{s}, {myCol[s]}"); + } + } } @@ -156,7 +187,7 @@ This code produces the following output. System.NotSupportedException: Collection is read-only. at System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(String name, Object value) - at SamplesNameObjectCollectionBase.Main() + at SamplesNameObjectCollectionBase.Run() Read-Only Collection: [0] : red, apple [1] : yellow, banana diff --git a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/remarks.cs b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/remarks.cs index 78532fcb322..8a8d1831935 100644 --- a/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/remarks.cs +++ b/snippets/csharp/System.Collections.Specialized/NameObjectCollectionBase/Overview/remarks.cs @@ -4,93 +4,113 @@ public class DerivedCollection : NameObjectCollectionBase { - // Creates an empty collection. - public DerivedCollection() { - } + // Creates an empty collection. + public DerivedCollection() + { + } - // Adds elements from an IDictionary into the new collection. - public DerivedCollection( IDictionary d, Boolean bReadOnly ) { - foreach ( DictionaryEntry de in d ) { - this.BaseAdd( (String) de.Key, de.Value ); - } - this.IsReadOnly = bReadOnly; - } + // Adds elements from an IDictionary into the new collection. + public DerivedCollection(IDictionary d, bool bReadOnly) + { + foreach (DictionaryEntry de in d) + { + this.BaseAdd((string)de.Key, de.Value); + } + this.IsReadOnly = bReadOnly; + } - // Gets a key-and-value pair (DictionaryEntry) using an index. - public DictionaryEntry this[ int index ] { - get { - return ( new DictionaryEntry( - this.BaseGetKey(index), this.BaseGet(index) ) ); - } - } + // Gets a key-and-value pair (DictionaryEntry) using an index. + public DictionaryEntry this[int index] + { + get + { + return (new DictionaryEntry( + this.BaseGetKey(index), this.BaseGet(index))); + } + } - // Gets or sets the value associated with the specified key. - public Object this[ String key ] { - get { - return( this.BaseGet( key ) ); - } - set { - this.BaseSet( key, value ); - } - } + // Gets or sets the value associated with the specified key. + public object this[string key] + { + get + { + return (this.BaseGet(key)); + } + set + { + this.BaseSet(key, value); + } + } - // Gets a String array that contains all the keys in the collection. - public String[] AllKeys { - get { - return( this.BaseGetAllKeys() ); - } - } + // Gets a String array that contains all the keys in the collection. + public string[] AllKeys + { + get + { + return (this.BaseGetAllKeys()); + } + } - // Gets an Object array that contains all the values in the collection. - public Array AllValues { - get { - return( this.BaseGetAllValues() ); - } - } + // Gets an Object array that contains all the values in the collection. + public Array AllValues + { + get + { + return (this.BaseGetAllValues()); + } + } - // Gets a String array that contains all the values in the collection. - public String[] AllStringValues { - get { - return( (String[]) this.BaseGetAllValues( typeof( string ) )); - } - } + // Gets a String array that contains all the values in the collection. + public string[] AllStringValues + { + get + { + return ((string[])this.BaseGetAllValues(typeof(string))); + } + } - // Gets a value indicating if the collection contains keys that are not null. - public Boolean HasKeys { - get { - return( this.BaseHasKeys() ); - } - } + // Gets a value indicating if the collection contains keys that are not null. + public bool HasKeys + { + get + { + return (this.BaseHasKeys()); + } + } - // Adds an entry to the collection. - public void Add( String key, Object value ) { - this.BaseAdd( key, value ); - } + // Adds an entry to the collection. + public void Add(string key, object value) + { + this.BaseAdd(key, value); + } - // Removes an entry with the specified key from the collection. - public void Remove( String key ) { - this.BaseRemove( key ); - } + // Removes an entry with the specified key from the collection. + public void Remove(string key) + { + this.BaseRemove(key); + } - // Removes an entry in the specified index from the collection. - public void Remove( int index ) { - this.BaseRemoveAt( index ); - } + // Removes an entry in the specified index from the collection. + public void Remove(int index) + { + this.BaseRemoveAt(index); + } - // Clears all the elements in the collection. - public void Clear() { - this.BaseClear(); - } + // Clears all the elements in the collection. + public void Clear() + { + this.BaseClear(); + } } -public class SamplesNameObjectCollectionBase +public class SamplesDerivedCollection { - public static void Main() + public static void Run() { // - // Create a collection derived from NameObjectCollectionBase + // Create a collection derived from NameObjectCollectionBase. ICollection myCollection = new DerivedCollection(); - lock(myCollection.SyncRoot) + lock (myCollection.SyncRoot) { foreach (object item in myCollection) { diff --git a/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/nvc.cs b/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/nvc.cs index 95f59a2a967..6bd32da3c4b 100644 --- a/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/nvc.cs +++ b/snippets/csharp/System.Collections.Specialized/NameValueCollection/Overview/nvc.cs @@ -5,60 +5,66 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesNameValueCollection { - - public static void Main() { - - // Creates and initializes a new NameValueCollection. - NameValueCollection myCol = new NameValueCollection(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - myCol.Add( "red", "rouge" ); - - // Displays the values in the NameValueCollection in two different ways. - Console.WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" ); - PrintKeysAndValues( myCol ); - Console.WriteLine( "Displays the elements using GetKey and Get:" ); - PrintKeysAndValues2( myCol ); - - // Gets a value either by index or by key. - Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] ); - Console.WriteLine( "Key \"red\" has the value {0}.", myCol["red"] ); - Console.WriteLine(); - - // Copies the values to a string array and displays the string array. - String[] myStrArr = new String[myCol.Count]; - myCol.CopyTo( myStrArr, 0 ); - Console.WriteLine( "The string array contains:" ); - foreach ( String s in myStrArr ) - Console.WriteLine( " {0}", s ); - Console.WriteLine(); - - // Searches for a key and deletes it. - myCol.Remove( "green" ); - Console.WriteLine( "The collection contains the following elements after removing \"green\":" ); - PrintKeysAndValues( myCol ); - - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues( myCol ); - } - - public static void PrintKeysAndValues( NameValueCollection myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( String s in myCol.AllKeys ) - Console.WriteLine( " {0,-10} {1}", s, myCol[s] ); - Console.WriteLine(); - } - - public static void PrintKeysAndValues2( NameValueCollection myCol ) { - Console.WriteLine( " [INDEX] KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " [{0}] {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) ); - Console.WriteLine(); - } +public class SamplesNameValueCollection +{ + + public static void Main() + { + + // Creates and initializes a new NameValueCollection. + NameValueCollection myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" }, + { "red", "rouge" } + }; + + // Displays the values in the NameValueCollection in two different ways. + Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:"); + PrintKeysAndValues(myCol); + Console.WriteLine("Displays the elements using GetKey and Get:"); + PrintKeysAndValues2(myCol); + + // Gets a value either by index or by key. + Console.WriteLine($"Index 1 contains the value {myCol[1]}."); + Console.WriteLine($"Key \"red\" has the value {myCol["red"]}."); + Console.WriteLine(); + + // Copies the values to a string array and displays the string array. + string[] myStrArr = new string[myCol.Count]; + myCol.CopyTo(myStrArr, 0); + Console.WriteLine("The string array contains:"); + foreach (string s in myStrArr) + Console.WriteLine($" {s}"); + Console.WriteLine(); + + // Searches for a key and deletes it. + myCol.Remove("green"); + Console.WriteLine("The collection contains the following elements after removing \"green\":"); + PrintKeysAndValues(myCol); + + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues(myCol); + } + + public static void PrintKeysAndValues(NameValueCollection myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (string s in myCol.AllKeys) + Console.WriteLine($" {s,-10} {myCol[s]}"); + Console.WriteLine(); + } + + public static void PrintKeysAndValues2(NameValueCollection myCol) + { + Console.WriteLine(" [INDEX] KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" [{i}] {myCol.GetKey(i),-10} {myCol.Get(i)}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/OrderedDictionary1.cs b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/OrderedDictionary1.cs index bdb067aceb7..303ff3aaaa2 100644 --- a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/OrderedDictionary1.cs +++ b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/OrderedDictionary1.cs @@ -6,38 +6,40 @@ public class OrderedDictionarySample { - public static void Main() + public static void Run() { // // Creates and initializes a OrderedDictionary. - OrderedDictionary myOrderedDictionary = new OrderedDictionary(); - myOrderedDictionary.Add("testKey1", "testValue1"); - myOrderedDictionary.Add("testKey2", "testValue2"); - myOrderedDictionary.Add("keyToDelete", "valueToDelete"); - myOrderedDictionary.Add("testKey3", "testValue3"); + OrderedDictionary myOrderedDictionary = new() + { + { "testKey1", "testValue1" }, + { "testKey2", "testValue2" }, + { "keyToDelete", "valueToDelete" }, + { "testKey3", "testValue3" } + }; ICollection keyCollection = myOrderedDictionary.Keys; ICollection valueCollection = myOrderedDictionary.Values; - // Display the contents using the key and value collections + // Display the contents using the key and value collections. DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count); // // - // Modifying the OrderedDictionary + // Modifying the OrderedDictionary. if (!myOrderedDictionary.IsReadOnly) { - // Insert a new key to the beginning of the OrderedDictionary + // Insert a new key to the beginning of the OrderedDictionary. myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1"); - // Modify the value of the entry with the key "testKey2" + // Modify the value of the entry with the key "testKey2". myOrderedDictionary["testKey2"] = "modifiedValue"; - // Remove the last entry from the OrderedDictionary: "testKey3" + // Remove the last entry from the OrderedDictionary: "testKey3". myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1); - // Remove the "keyToDelete" entry, if it exists + // Remove the "keyToDelete" entry, if it exists. if (myOrderedDictionary.Contains("keyToDelete")) { myOrderedDictionary.Remove("keyToDelete"); @@ -45,60 +47,54 @@ public static void Main() } // - Console.WriteLine( - "{0}Displaying the entries of a modified OrderedDictionary.", - Environment.NewLine); + Console.WriteLine($"{Environment.NewLine}Displaying the entries of a modified OrderedDictionary."); DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count); // - // Clear the OrderedDictionary and add new values + // Clear the OrderedDictionary and add new values. myOrderedDictionary.Clear(); myOrderedDictionary.Add("newKey1", "newValue1"); myOrderedDictionary.Add("newKey2", "newValue2"); myOrderedDictionary.Add("newKey3", "newValue3"); - // Display the contents of the "new" Dictionary using an enumerator + // Display the contents of the "new" Dictionary using an enumerator. IDictionaryEnumerator myEnumerator = myOrderedDictionary.GetEnumerator(); - Console.WriteLine( - "{0}Displaying the entries of a \"new\" OrderedDictionary.", - Environment.NewLine); + Console.WriteLine($"{Environment.NewLine}Displaying the entries of a \"new\" OrderedDictionary."); DisplayEnumerator(myEnumerator); // } // - // Displays the contents of the OrderedDictionary from its keys and values + // Displays the contents of the OrderedDictionary from its keys and values. public static void DisplayContents( ICollection keyCollection, ICollection valueCollection, int dictionarySize) { - String[] myKeys = new String[dictionarySize]; - String[] myValues = new String[dictionarySize]; + string[] myKeys = new string[dictionarySize]; + string[] myValues = new string[dictionarySize]; keyCollection.CopyTo(myKeys, 0); valueCollection.CopyTo(myValues, 0); - // Displays the contents of the OrderedDictionary + // Displays the contents of the OrderedDictionary. Console.WriteLine(" INDEX KEY VALUE"); for (int i = 0; i < dictionarySize; i++) { - Console.WriteLine(" {0,-5} {1,-25} {2}", - i, myKeys[i], myValues[i]); + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myValues[i]}"); } Console.WriteLine(); } // // - // Displays the contents of the OrderedDictionary using its enumerator + // Displays the contents of the OrderedDictionary using its enumerator. public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator) { Console.WriteLine(" KEY VALUE"); while (myEnumerator.MoveNext()) { - Console.WriteLine(" {0,-25} {1}", - myEnumerator.Key, myEnumerator.Value); + Console.WriteLine($" {myEnumerator.Key,-25} {myEnumerator.Value}"); } } // @@ -128,4 +124,4 @@ newKey2 newValue2 newKey3 newValue3 */ -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Program.cs new file mode 100644 index 00000000000..d4cff4feded --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Program.cs @@ -0,0 +1,2 @@ +OrderedDictionarySample.Run(); +OrderedDictionarySyncRootSample.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/source2.cs b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/source2.cs index e4e6baa602b..dd9db6ea616 100644 --- a/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Specialized/OrderedDictionary/Overview/source2.cs @@ -2,11 +2,11 @@ using System.Collections; using System.Collections.Specialized; -public class OrderedDictionarySample +public class OrderedDictionarySyncRootSample { - public static void Main() + public static void Run() { - OrderedDictionary myOrderedDictionary = new OrderedDictionary(); + OrderedDictionary myOrderedDictionary = []; // foreach (DictionaryEntry de in myOrderedDictionary) { diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Add/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringCollection/Add/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Add/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Add/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Add/stringcollectionadd.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Add/stringcollectionadd.cs index c8759006211..c6a64315f00 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Add/stringcollectionadd.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Add/stringcollectionadd.cs @@ -5,36 +5,39 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection { +public class SamplesStringCollection +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new StringCollection. - StringCollection myCol = new StringCollection(); + // Creates and initializes a new StringCollection. + StringCollection myCol = []; - Console.WriteLine( "Initial contents of the StringCollection:" ); - PrintValues( myCol ); + Console.WriteLine("Initial contents of the StringCollection:"); + PrintValues(myCol); - // Adds a range of elements from an array to the end of the StringCollection. - String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" }; - myCol.AddRange( myArr ); + // Adds a range of elements from an array to the end of the StringCollection. + string[] myArr = ["RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"]; + myCol.AddRange(myArr); - Console.WriteLine( "After adding a range of elements:" ); - PrintValues( myCol ); + Console.WriteLine("After adding a range of elements:"); + PrintValues(myCol); - // Adds one element to the end of the StringCollection and inserts another at index 3. - myCol.Add( "* white" ); - myCol.Insert( 3, "* gray" ); + // Adds one element to the end of the StringCollection and inserts another at index 3. + myCol.Add("* white"); + myCol.Insert(3, "* gray"); - Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" ); - PrintValues( myCol ); - } + Console.WriteLine("After adding \"* white\" to the end and inserting \"* gray\" at index 3:"); + PrintValues(myCol); + } - public static void PrintValues( IEnumerable myCol ) { - foreach ( Object obj in myCol ) - Console.WriteLine( " {0}", obj ); - Console.WriteLine(); - } + public static void PrintValues(IEnumerable myCol) + { + foreach (object obj in myCol) + Console.WriteLine($" {obj}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/stringcollectionremove.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/stringcollectionremove.cs index 83d72a9f134..8ae9c34c1bc 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/stringcollectionremove.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Clear/stringcollectionremove.cs @@ -5,49 +5,52 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection { - - public static void Main() { - - // Creates and initializes a new StringCollection. - StringCollection myCol = new StringCollection(); - String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" }; - myCol.AddRange( myArr ); - - Console.WriteLine( "Initial contents of the StringCollection:" ); - PrintValues( myCol ); - - // Removes one element from the StringCollection. - myCol.Remove( "yellow" ); - - Console.WriteLine( "After removing \"yellow\":" ); - PrintValues( myCol ); - - // Removes all occurrences of a value from the StringCollection. - int i = myCol.IndexOf( "RED" ); - while ( i > -1 ) { - myCol.RemoveAt( i ); - i = myCol.IndexOf( "RED" ); - } - - Console.WriteLine( "After removing all occurrences of \"RED\":" ); - PrintValues( myCol ); - - // Clears the entire collection. - myCol.Clear(); - - Console.WriteLine( "After clearing the collection:" ); - PrintValues( myCol ); - } - - public static void PrintValues( IEnumerable myCol ) { - foreach ( Object obj in myCol ) - Console.WriteLine( " {0}", obj ); - Console.WriteLine(); - } +public class SamplesStringCollection +{ + public static void Main() + { + // Creates and initializes a new StringCollection. + StringCollection myCol = []; + string[] myArr = ["RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"]; + myCol.AddRange(myArr); + + Console.WriteLine("Initial contents of the StringCollection:"); + PrintValues(myCol); + + // Removes one element from the StringCollection. + myCol.Remove("yellow"); + + Console.WriteLine("After removing \"yellow\":"); + PrintValues(myCol); + + // Removes all occurrences of a value from the StringCollection. + int i = myCol.IndexOf("RED"); + while (i > -1) + { + myCol.RemoveAt(i); + i = myCol.IndexOf("RED"); + } + + Console.WriteLine("After removing all occurrences of \"RED\":"); + PrintValues(myCol); + + // Clears the entire collection. + myCol.Clear(); + + Console.WriteLine("After clearing the collection:"); + PrintValues(myCol); + } + + public static void PrintValues(IEnumerable myCol) + { + foreach (object obj in myCol) + Console.WriteLine($" {obj}"); + Console.WriteLine(); + } } /* + This code produces the following output. Initial contents of the StringCollection: @@ -83,4 +86,5 @@ This code produces the following output. After clearing the collection: */ + // diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/stringcollectioncontains.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/stringcollectioncontains.cs index 6253f941a21..248bbe11ba3 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/stringcollectioncontains.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Contains/stringcollectioncontains.cs @@ -5,30 +5,33 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection { - - public static void Main() { - - // Creates and initializes a new StringCollection. - StringCollection myCol = new StringCollection(); - String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" }; - myCol.AddRange( myArr ); - - Console.WriteLine( "Initial contents of the StringCollection:" ); - PrintValues( myCol ); - - // Checks whether the collection contains "orange" and, if so, displays its index. - if ( myCol.Contains( "orange" ) ) - Console.WriteLine( "The collection contains \"orange\" at index {0}.", myCol.IndexOf( "orange" ) ); - else - Console.WriteLine( "The collection does not contain \"orange\"." ); - } - - public static void PrintValues( IEnumerable myCol ) { - foreach ( Object obj in myCol ) - Console.WriteLine( " {0}", obj ); - Console.WriteLine(); - } +public class SamplesStringCollection +{ + + public static void Main() + { + + // Creates and initializes a new StringCollection. + StringCollection myCol = []; + string[] myArr = ["RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"]; + myCol.AddRange(myArr); + + Console.WriteLine("Initial contents of the StringCollection:"); + PrintValues(myCol); + + // Checks whether the collection contains "orange" and, if so, displays its index. + if (myCol.Contains("orange")) + Console.WriteLine($"The collection contains \"orange\" at index {myCol.IndexOf("orange")}."); + else + Console.WriteLine("The collection does not contain \"orange\"."); + } + + public static void PrintValues(IEnumerable myCol) + { + foreach (object obj in myCol) + Console.WriteLine($" {obj}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/stringcollectioncopyto.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/stringcollectioncopyto.cs index d8a480451b5..791b8efc482 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/stringcollectioncopyto.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/CopyTo/stringcollectioncopyto.cs @@ -5,34 +5,38 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection { +public class SamplesStringCollection +{ - public static void Main() { + public static void Main() + { - // Creates and initializes a new StringCollection. - StringCollection myCol = new StringCollection(); - String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" }; - myCol.AddRange( myArr ); + // Creates and initializes a new StringCollection. + StringCollection myCol = []; + string[] myArr = ["RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"]; + myCol.AddRange(myArr); - Console.WriteLine( "Initial contents of the StringCollection:" ); - PrintValues( myCol ); + Console.WriteLine("Initial contents of the StringCollection:"); + PrintValues(myCol); - // Copies the collection to a new array starting at index 0. - String[] myArr2 = new String[myCol.Count]; - myCol.CopyTo( myArr2, 0 ); + // Copies the collection to a new array starting at index 0. + string[] myArr2 = new string[myCol.Count]; + myCol.CopyTo(myArr2, 0); - Console.WriteLine( "The new array contains:" ); - for ( int i = 0; i < myArr2.Length; i++ ) { - Console.WriteLine( " [{0}] {1}", i, myArr2[i] ); - } - Console.WriteLine(); - } + Console.WriteLine("The new array contains:"); + for (int i = 0; i < myArr2.Length; i++) + { + Console.WriteLine($" [{i}] {myArr2[i]}"); + } + Console.WriteLine(); + } - public static void PrintValues( IEnumerable myCol ) { - foreach ( Object obj in myCol ) - Console.WriteLine( " {0}", obj ); - Console.WriteLine(); - } + public static void PrintValues(IEnumerable myCol) + { + foreach (object obj in myCol) + Console.WriteLine($" {obj}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Program.cs new file mode 100644 index 00000000000..80a820d2763 --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Program.cs @@ -0,0 +1,2 @@ +SamplesStringCollection.Run(); +SamplesStringCollectionSyncRoot.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/remarks.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/remarks.cs index c451fec1d76..eddd91112fd 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/remarks.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/remarks.cs @@ -2,13 +2,13 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection +public class SamplesStringCollectionSyncRoot { - public static void Main() + public static void Run() { // - StringCollection myCollection = new StringCollection(); - lock(myCollection.SyncRoot) + StringCollection myCollection = []; + lock (myCollection.SyncRoot) { foreach (object item in myCollection) { diff --git a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/stringcollection.cs b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/stringcollection.cs index 580d25ca6c6..7f952e8e646 100644 --- a/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/stringcollection.cs +++ b/snippets/csharp/System.Collections.Specialized/StringCollection/Overview/stringcollection.cs @@ -5,96 +5,103 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringCollection { - - public static void Main() { - - // Create and initializes a new StringCollection. - StringCollection myCol = new StringCollection(); - - // Add a range of elements from an array to the end of the StringCollection. - String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" }; - myCol.AddRange( myArr ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IEnumerator:" ); - PrintValues2( myCol ); - - // Display the contents of the collection using the Count and Item properties. - Console.WriteLine( "Displays the elements using the Count and Item properties:" ); - PrintValues3( myCol ); - - // Add one element to the end of the StringCollection and insert another at index 3. - myCol.Add( "* white" ); - myCol.Insert( 3, "* gray" ); - - Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" ); - PrintValues1( myCol ); - - // Remove one element from the StringCollection. - myCol.Remove( "yellow" ); - - Console.WriteLine( "After removing \"yellow\":" ); - PrintValues1( myCol ); - - // Remove all occurrences of a value from the StringCollection. - int i = myCol.IndexOf( "RED" ); - while ( i > -1 ) { - myCol.RemoveAt( i ); - i = myCol.IndexOf( "RED" ); - } - - // Verify that all occurrences of "RED" are gone. - if ( myCol.Contains( "RED" ) ) - Console.WriteLine( "*** The collection still contains \"RED\"." ); - - Console.WriteLine( "After removing all occurrences of \"RED\":" ); - PrintValues1( myCol ); - - // Copy the collection to a new array starting at index 0. - String[] myArr2 = new String[myCol.Count]; - myCol.CopyTo( myArr2, 0 ); - - Console.WriteLine( "The new array contains:" ); - for ( i = 0; i < myArr2.Length; i++ ) { - Console.WriteLine( " [{0}] {1}", i, myArr2[i] ); - } - Console.WriteLine(); - - // Clears the entire collection. - myCol.Clear(); - - Console.WriteLine( "After clearing the collection:" ); - PrintValues1( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintValues1( StringCollection myCol ) { - foreach ( Object obj in myCol ) - Console.WriteLine( " {0}", obj ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintValues2( StringCollection myCol ) { - StringEnumerator myEnumerator = myCol.GetEnumerator(); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( " {0}", myEnumerator.Current ); - Console.WriteLine(); - } - - // Uses the Count and Item properties. - public static void PrintValues3( StringCollection myCol ) { - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0}", myCol[i] ); - Console.WriteLine(); - } +public class SamplesStringCollection +{ + + public static void Run() + { + + // Create and initializes a new StringCollection. + StringCollection myCol = []; + + // Add a range of elements from an array to the end of the StringCollection. + string[] myArr = ["RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"]; + myCol.AddRange(myArr); + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IEnumerator:"); + PrintValues2(myCol); + + // Display the contents of the collection using the Count and Item properties. + Console.WriteLine("Displays the elements using the Count and Item properties:"); + PrintValues3(myCol); + + // Add one element to the end of the StringCollection and insert another at index 3. + myCol.Add("* white"); + myCol.Insert(3, "* gray"); + + Console.WriteLine("After adding \"* white\" to the end and inserting \"* gray\" at index 3:"); + PrintValues1(myCol); + + // Remove one element from the StringCollection. + myCol.Remove("yellow"); + + Console.WriteLine("After removing \"yellow\":"); + PrintValues1(myCol); + + // Remove all occurrences of a value from the StringCollection. + int i = myCol.IndexOf("RED"); + while (i > -1) + { + myCol.RemoveAt(i); + i = myCol.IndexOf("RED"); + } + + // Verify that all occurrences of "RED" are gone. + if (myCol.Contains("RED")) + Console.WriteLine("*** The collection still contains \"RED\"."); + + Console.WriteLine("After removing all occurrences of \"RED\":"); + PrintValues1(myCol); + + // Copy the collection to a new array starting at index 0. + string[] myArr2 = new string[myCol.Count]; + myCol.CopyTo(myArr2, 0); + + Console.WriteLine("The new array contains:"); + for (i = 0; i < myArr2.Length; i++) + { + Console.WriteLine($" [{i}] {myArr2[i]}"); + } + Console.WriteLine(); + + // Clears the entire collection. + myCol.Clear(); + + Console.WriteLine("After clearing the collection:"); + PrintValues1(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintValues1(StringCollection myCol) + { + foreach (object obj in myCol) + Console.WriteLine($" {obj}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintValues2(StringCollection myCol) + { + StringEnumerator myEnumerator = myCol.GetEnumerator(); + while (myEnumerator.MoveNext()) + Console.WriteLine($" {myEnumerator.Current}"); + Console.WriteLine(); + } + + // Uses the Count and Item properties. + public static void PrintValues3(StringCollection myCol) + { + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {myCol[i]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/stringdictionary_addremove.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/stringdictionary_addremove.cs index 4e65fc56dc0..fe0ab0c397d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/stringdictionary_addremove.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Add/stringdictionary_addremove.cs @@ -5,37 +5,40 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary { - - public static void Main() { - - // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - - // Displays the values in the StringDictionary. - Console.WriteLine( "Initial contents of the StringDictionary:" ); - PrintKeysAndValues( myCol ); - - // Deletes an element. - myCol.Remove( "green" ); - Console.WriteLine( "The collection contains the following elements after removing \"green\":" ); - PrintKeysAndValues( myCol ); - - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues( myCol ); - } - - public static void PrintKeysAndValues( StringDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-10} {1}", de.Key, de.Value ); - Console.WriteLine(); - } +public class SamplesStringDictionary +{ + + public static void Main() + { + + // Creates and initializes a new StringDictionary. + StringDictionary myCol = new(); + myCol.Add("red", "rojo"); + myCol.Add("green", "verde"); + myCol.Add("blue", "azul"); + + // Displays the values in the StringDictionary. + Console.WriteLine("Initial contents of the StringDictionary:"); + PrintKeysAndValues(myCol); + + // Deletes an element. + myCol.Remove("green"); + Console.WriteLine("The collection contains the following elements after removing \"green\":"); + PrintKeysAndValues(myCol); + + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues(myCol); + } + + public static void PrintKeysAndValues(StringDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-10} {de.Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/stringdictionary_contains.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/stringdictionary_contains.cs index 482a781f201..ccbf746f454 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/stringdictionary_contains.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/ContainsKey/stringdictionary_contains.cs @@ -5,41 +5,46 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary { - - public static void Main() { - - // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - - // Displays the values in the StringDictionary. - Console.WriteLine( "Initial contents of the StringDictionary:" ); - PrintKeysAndValues( myCol ); - - // Searches for a key. - if ( myCol.ContainsKey( "red" ) ) - Console.WriteLine( "The collection contains the key \"red\"." ); - else - Console.WriteLine( "The collection does not contain the key \"red\"." ); - Console.WriteLine(); - - // Searches for a value. - if ( myCol.ContainsValue( "amarillo" ) ) - Console.WriteLine( "The collection contains the value \"amarillo\"." ); - else - Console.WriteLine( "The collection does not contain the value \"amarillo\"." ); - Console.WriteLine(); - } - - public static void PrintKeysAndValues( StringDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-10} {1}", de.Key, de.Value ); - Console.WriteLine(); - } +public class SamplesStringDictionary +{ + + public static void Main() + { + + // Creates and initializes a new StringDictionary. + StringDictionary myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" } + }; + + // Displays the values in the StringDictionary. + Console.WriteLine("Initial contents of the StringDictionary:"); + PrintKeysAndValues(myCol); + + // Searches for a key. + if (myCol.ContainsKey("red")) + Console.WriteLine("The collection contains the key \"red\"."); + else + Console.WriteLine("The collection does not contain the key \"red\"."); + Console.WriteLine(); + + // Searches for a value. + if (myCol.ContainsValue("amarillo")) + Console.WriteLine("The collection contains the value \"amarillo\"."); + else + Console.WriteLine("The collection does not contain the value \"amarillo\"."); + Console.WriteLine(); + } + + public static void PrintKeysAndValues(StringDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-10} {de.Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/stringdictionary_copyto.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/stringdictionary_copyto.cs index 97fb5e1e49c..97a0647de87 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/stringdictionary_copyto.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/CopyTo/stringdictionary_copyto.cs @@ -5,34 +5,38 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary { - - public static void Main() { - - // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - - // Displays the values in the StringDictionary. - Console.WriteLine( "KEYS\tVALUES in the StringDictionary" ); - foreach ( DictionaryEntry myDE in myCol ) - Console.WriteLine( "{0}\t{1}", myDE.Key, myDE.Value ); - Console.WriteLine(); - - // Creates an array with DictionaryEntry elements. - DictionaryEntry[] myArr = { new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry() }; - - // Copies the StringDictionary to the array. - myCol.CopyTo( myArr, 0 ); - - // Displays the values in the array. - Console.WriteLine( "KEYS\tVALUES in the array" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( "{0}\t{1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - } +public class SamplesStringDictionary +{ + + public static void Main() + { + + // Creates and initializes a new StringDictionary. + StringDictionary myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" } + }; + + // Displays the values in the StringDictionary. + Console.WriteLine("KEYS\tVALUES in the StringDictionary"); + foreach (DictionaryEntry myDE in myCol) + Console.WriteLine($"{myDE.Key}\t{myDE.Value}"); + Console.WriteLine(); + + // Creates an array with DictionaryEntry elements. + DictionaryEntry[] myArr = [new DictionaryEntry(), new DictionaryEntry(), new DictionaryEntry()]; + + // Copies the StringDictionary to the array. + myCol.CopyTo(myArr, 0); + + // Displays the values in the array. + Console.WriteLine("KEYS\tVALUES in the array"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($"{myArr[i].Key}\t{myArr[i].Value}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Program.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Program.cs new file mode 100644 index 00000000000..aa0507318a1 --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Program.cs @@ -0,0 +1,2 @@ +SamplesStringDictionaryEnumeration.Run(); +SamplesStringDictionaryValues.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/stringdictionary_enumeration.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/stringdictionary_enumeration.cs index 281d6c43faa..5035b32200f 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/stringdictionary_enumeration.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/stringdictionary_enumeration.cs @@ -5,61 +5,69 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary { - - public static void Main() { - - // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( StringDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( StringDictionary myCol ) { - IEnumerator myEnumerator = myCol.GetEnumerator(); - DictionaryEntry de; - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) { - de = (DictionaryEntry) myEnumerator.Current; - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - } - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( StringDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesStringDictionaryEnumeration +{ + + public static void Run() + { + + // Creates and initializes a new StringDictionary. + StringDictionary myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(StringDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(StringDictionary myCol) + { + IEnumerator myEnumerator = myCol.GetEnumerator(); + DictionaryEntry de; + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + { + de = (DictionaryEntry)myEnumerator.Current; + Console.WriteLine($" {de.Key,-25} {de.Value}"); + } + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(StringDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/values.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/values.cs index 1d4f4eee0c4..6909537bd30 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/values.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Count/values.cs @@ -5,15 +5,17 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary +public class SamplesStringDictionaryValues { - public static void Main() + public static void Run() { // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); + StringDictionary myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" } + }; Console.WriteLine("VALUES"); foreach (string val in myCol.Values) diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Program.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Program.cs new file mode 100644 index 00000000000..55c88686a8e --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Program.cs @@ -0,0 +1,2 @@ +SamplesStringDictionary.Run(); +SamplesStringDictionarySyncRoot.Run(); diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/source2.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/source2.cs index b2659f52715..2634afa3ca7 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/source2.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/source2.cs @@ -2,15 +2,15 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary +public class SamplesStringDictionarySyncRoot { - public static void Main() + public static void Run() { // - StringDictionary myCollection = new StringDictionary(); - lock(myCollection.SyncRoot) + StringDictionary myCollection = []; + lock (myCollection.SyncRoot) { - foreach (Object item in myCollection) + foreach (object item in myCollection) { // Insert your code here. } diff --git a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/stringdictionary.cs b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/stringdictionary.cs index 6c6486db6e6..45557646788 100644 --- a/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/stringdictionary.cs +++ b/snippets/csharp/System.Collections.Specialized/StringDictionary/Overview/stringdictionary.cs @@ -5,90 +5,98 @@ using System.Collections; using System.Collections.Specialized; -public class SamplesStringDictionary { - - public static void Main() { - - // Creates and initializes a new StringDictionary. - StringDictionary myCol = new StringDictionary(); - myCol.Add( "red", "rojo" ); - myCol.Add( "green", "verde" ); - myCol.Add( "blue", "azul" ); - - // Display the contents of the collection using foreach. This is the preferred method. - Console.WriteLine( "Displays the elements using foreach:" ); - PrintKeysAndValues1( myCol ); - - // Display the contents of the collection using the enumerator. - Console.WriteLine( "Displays the elements using the IEnumerator:" ); - PrintKeysAndValues2( myCol ); - - // Display the contents of the collection using the Keys, Values, Count, and Item properties. - Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); - PrintKeysAndValues3( myCol ); - - // Copies the StringDictionary to an array with DictionaryEntry elements. - DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; - myCol.CopyTo( myArr, 0 ); - - // Displays the values in the array. - Console.WriteLine( "Displays the elements in the array:" ); - Console.WriteLine( " KEY VALUE" ); - for ( int i = 0; i < myArr.Length; i++ ) - Console.WriteLine( " {0,-10} {1}", myArr[i].Key, myArr[i].Value ); - Console.WriteLine(); - - // Searches for a value. - if ( myCol.ContainsValue( "amarillo" ) ) - Console.WriteLine( "The collection contains the value \"amarillo\"." ); - else - Console.WriteLine( "The collection does not contain the value \"amarillo\"." ); - Console.WriteLine(); - - // Searches for a key and deletes it. - if ( myCol.ContainsKey( "green" ) ) - myCol.Remove( "green" ); - Console.WriteLine( "The collection contains the following elements after removing \"green\":" ); - PrintKeysAndValues1( myCol ); - - // Clears the entire collection. - myCol.Clear(); - Console.WriteLine( "The collection contains the following elements after it is cleared:" ); - PrintKeysAndValues1( myCol ); - } - - // Uses the foreach statement which hides the complexity of the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues1( StringDictionary myCol ) { - Console.WriteLine( " KEY VALUE" ); - foreach ( DictionaryEntry de in myCol ) - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - Console.WriteLine(); - } - - // Uses the enumerator. - // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. - public static void PrintKeysAndValues2( StringDictionary myCol ) { - IEnumerator myEnumerator = myCol.GetEnumerator(); - DictionaryEntry de; - Console.WriteLine( " KEY VALUE" ); - while ( myEnumerator.MoveNext() ) { - de = (DictionaryEntry) myEnumerator.Current; - Console.WriteLine( " {0,-25} {1}", de.Key, de.Value ); - } - Console.WriteLine(); - } - - // Uses the Keys, Values, Count, and Item properties. - public static void PrintKeysAndValues3( StringDictionary myCol ) { - String[] myKeys = new String[myCol.Count]; - myCol.Keys.CopyTo( myKeys, 0 ); - - Console.WriteLine( " INDEX KEY VALUE" ); - for ( int i = 0; i < myCol.Count; i++ ) - Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] ); - Console.WriteLine(); - } +public class SamplesStringDictionary +{ + + public static void Run() + { + + // Creates and initializes a new StringDictionary. + StringDictionary myCol = new() + { + { "red", "rojo" }, + { "green", "verde" }, + { "blue", "azul" } + }; + + // Display the contents of the collection using foreach. This is the preferred method. + Console.WriteLine("Displays the elements using foreach:"); + PrintKeysAndValues1(myCol); + + // Display the contents of the collection using the enumerator. + Console.WriteLine("Displays the elements using the IEnumerator:"); + PrintKeysAndValues2(myCol); + + // Display the contents of the collection using the Keys, Values, Count, and Item properties. + Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); + PrintKeysAndValues3(myCol); + + // Copies the StringDictionary to an array with DictionaryEntry elements. + DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; + myCol.CopyTo(myArr, 0); + + // Displays the values in the array. + Console.WriteLine("Displays the elements in the array:"); + Console.WriteLine(" KEY VALUE"); + for (int i = 0; i < myArr.Length; i++) + Console.WriteLine($" {myArr[i].Key,-10} {myArr[i].Value}"); + Console.WriteLine(); + + // Searches for a value. + if (myCol.ContainsValue("amarillo")) + Console.WriteLine("The collection contains the value \"amarillo\"."); + else + Console.WriteLine("The collection does not contain the value \"amarillo\"."); + Console.WriteLine(); + + // Searches for a key and deletes it. + if (myCol.ContainsKey("green")) + myCol.Remove("green"); + Console.WriteLine("The collection contains the following elements after removing \"green\":"); + PrintKeysAndValues1(myCol); + + // Clears the entire collection. + myCol.Clear(); + Console.WriteLine("The collection contains the following elements after it is cleared:"); + PrintKeysAndValues1(myCol); + } + + // Uses the foreach statement which hides the complexity of the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues1(StringDictionary myCol) + { + Console.WriteLine(" KEY VALUE"); + foreach (DictionaryEntry de in myCol) + Console.WriteLine($" {de.Key,-25} {de.Value}"); + Console.WriteLine(); + } + + // Uses the enumerator. + // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection. + public static void PrintKeysAndValues2(StringDictionary myCol) + { + IEnumerator myEnumerator = myCol.GetEnumerator(); + DictionaryEntry de; + Console.WriteLine(" KEY VALUE"); + while (myEnumerator.MoveNext()) + { + de = (DictionaryEntry)myEnumerator.Current; + Console.WriteLine($" {de.Key,-25} {de.Value}"); + } + Console.WriteLine(); + } + + // Uses the Keys, Values, Count, and Item properties. + public static void PrintKeysAndValues3(StringDictionary myCol) + { + string[] myKeys = new string[myCol.Count]; + myCol.Keys.CopyTo(myKeys, 0); + + Console.WriteLine(" INDEX KEY VALUE"); + for (int i = 0; i < myCol.Count; i++) + Console.WriteLine($" {i,-5} {myKeys[i],-25} {myCol[myKeys[i]]}"); + Console.WriteLine(); + } } /* diff --git a/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/Project.csproj b/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/Project.csproj index a369cfa8a80..ffb97e9872d 100644 --- a/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/Project.csproj +++ b/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net10.0 diff --git a/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/stringenumerator.cs b/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/stringenumerator.cs index 4d6dd3f3091..9d168a6c0c2 100644 --- a/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/stringenumerator.cs +++ b/snippets/csharp/System.Collections.Specialized/StringEnumerator/Overview/stringenumerator.cs @@ -4,26 +4,28 @@ using System; using System.Collections.Specialized; -public class SamplesStringEnumerator { - - public static void Main() { - - // Creates and initializes a StringCollection. - StringCollection myCol = new StringCollection(); - String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" }; - myCol.AddRange( myArr ); - - // Enumerates the elements in the StringCollection. - StringEnumerator myEnumerator = myCol.GetEnumerator(); - while ( myEnumerator.MoveNext() ) - Console.WriteLine( "{0}", myEnumerator.Current ); - Console.WriteLine(); - - // Resets the enumerator and displays the first element again. - myEnumerator.Reset(); - if ( myEnumerator.MoveNext() ) - Console.WriteLine( "The first element is {0}.", myEnumerator.Current ); - } +public class SamplesStringEnumerator +{ + + public static void Main() + { + + // Creates and initializes a StringCollection. + StringCollection myCol = []; + string[] myArr = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]; + myCol.AddRange(myArr); + + // Enumerates the elements in the StringCollection. + StringEnumerator myEnumerator = myCol.GetEnumerator(); + while (myEnumerator.MoveNext()) + Console.WriteLine($"{myEnumerator.Current}"); + Console.WriteLine(); + + // Resets the enumerator and displays the first element again. + myEnumerator.Reset(); + if (myEnumerator.MoveNext()) + Console.WriteLine($"The first element is {myEnumerator.Current}."); + } } /* diff --git a/xml/System.Collections.Specialized/IOrderedDictionary.xml b/xml/System.Collections.Specialized/IOrderedDictionary.xml index e7076dd23e4..9bdc8105f12 100644 --- a/xml/System.Collections.Specialized/IOrderedDictionary.xml +++ b/xml/System.Collections.Specialized/IOrderedDictionary.xml @@ -54,26 +54,26 @@ elements can be accessed either with the key or with the index. - Each element is a key/value pair stored in a structure. + elements can be accessed either with the key or with the index. - Each pair must have a unique key that is not `null`, but the value can be `null` and does not have to be unique. The interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order. +Each element is a key/value pair stored in a structure. - The `foreach` statement of the C# language (`For Each` in Visual Basic) returns an object of the type of the elements in the collection. Because each element of the is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is , as the following example shows. +Each pair must have a unique key that is not `null`, but the value can be `null` and does not have to be unique. The interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order. - :::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.cs" id="Snippet03"::: - :::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.vb" id="Snippet03"::: - - The `foreach` statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection. +The `foreach` statement of the C# language (`For Each` in Visual Basic) returns an object of the type of the elements in the collection. Because each element of the is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is , as the following example shows. +:::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs" id="SnippetDictionaryEntry"::: +:::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.vb" id="Snippet03"::: +The `foreach` statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection. ## Examples - The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. - :::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs" id="Snippet00"::: - :::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.vb" id="Snippet00"::: +The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. + +:::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs" id="Snippet00"::: +:::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.vb" id="Snippet00"::: ]]> @@ -139,8 +139,6 @@ The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. - - ## Examples The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the class. @@ -207,8 +205,6 @@ In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accommodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped together, such as a hash table. - - ## Examples The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the class. @@ -286,13 +282,17 @@ The C# language uses the [this](/dotnet/csharp/language-reference/keywords/this) This property allows you to access a specific element in the collection by using the following syntax: - :::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.cs" id="Snippet04"::: - :::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/remarks.vb" id="Snippet04"::: - +```csharp +obj = myOrderedDictionary[index]; +``` +```vb +obj = myOrderedDictionary(index) +``` ## Examples - The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the class. + +The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the class. :::code language="csharp" source="~/snippets/csharp/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.cs" id="Snippet01"::: :::code language="vb" source="~/snippets/visualbasic/System.Collections.Specialized/IOrderedDictionary/Overview/iordereddictionary.vb" id="Snippet01"::: @@ -349,8 +349,6 @@ This property allows you to access a specific element in the collection by using ## Remarks In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped together, such as a hash table. - - ## Examples The following code example demonstrates the implementation of a simple based on the class. The implemented stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the class.