Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ class Program
static void Main(string[] args)
{
// <Snippet2>
List<Box> Boxes = new List<Box>();
Boxes.Add(new Box(4, 20, 14));
Boxes.Add(new Box(12, 12, 12));
Boxes.Add(new Box(8, 20, 10));
Boxes.Add(new Box(6, 10, 2));
Boxes.Add(new Box(2, 8, 4));
Boxes.Add(new Box(2, 6, 8));
Boxes.Add(new Box(4, 12, 20));
Boxes.Add(new Box(18, 10, 4));
Boxes.Add(new Box(24, 4, 18));
Boxes.Add(new Box(10, 4, 16));
Boxes.Add(new Box(10, 2, 10));
Boxes.Add(new Box(6, 18, 2));
Boxes.Add(new Box(8, 12, 4));
Boxes.Add(new Box(12, 10, 8));
Boxes.Add(new Box(14, 6, 6));
Boxes.Add(new Box(16, 6, 16));
Boxes.Add(new Box(2, 8, 12));
Boxes.Add(new Box(4, 24, 8));
Boxes.Add(new Box(8, 6, 20));
Boxes.Add(new Box(18, 18, 12));
List<Box> Boxes =
[
new Box(4, 20, 14),
new Box(12, 12, 12),
new Box(8, 20, 10),
new Box(6, 10, 2),
new Box(2, 8, 4),
new Box(2, 6, 8),
new Box(4, 12, 20),
new Box(18, 10, 4),
new Box(24, 4, 18),
new Box(10, 4, 16),
new Box(10, 2, 10),
new Box(6, 18, 2),
new Box(8, 12, 4),
new Box(12, 10, 8),
new Box(14, 6, 6),
new Box(16, 6, 16),
new Box(2, 8, 12),
new Box(4, 24, 8),
new Box(8, 6, 20),
new Box(18, 18, 12),
];

// Sort by an Comparer<T> implementation that sorts
// first by the length.
Expand Down Expand Up @@ -72,12 +74,12 @@ static void Main(string[] args)
// compares first by the length.
// Returns -1 because the length of BoxA
// is less than the length of BoxB.
BoxLengthFirst LengthFirst = new BoxLengthFirst();
BoxLengthFirst LengthFirst = new();

Comparer<Box> bc = (Comparer<Box>) LengthFirst;
Comparer<Box> bc = (Comparer<Box>)LengthFirst;

Box BoxA = new Box(2, 6, 8);
Box BoxB = new Box(10, 12, 14);
Box BoxA = new(2, 6, 8);
Box BoxB = new(10, 12, 14);
int x = LengthFirst.Compare(BoxA, BoxB);
Console.WriteLine();
Console.WriteLine(x.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DictionaryConstructorExample1.Run();
DictionaryConstructorExample2.Run();
DictionaryConstructorExample3.Run();
DictionaryConstructorExample4.Run();
DictionaryConstructorExample5.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryConstructorExample1
{
public static void Main()
public static void Run()
{
// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith =
new SortedDictionary<string, string>();

// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
new SortedDictionary<string, string>()
{
// Add some elements to the dictionary.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "dib", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// Create a Dictionary of strings with string keys, and
// initialize it with the contents of the sorted dictionary.
Expand All @@ -24,7 +25,7 @@ public static void Main()

// List the contents of the copy.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in copy )
foreach (KeyValuePair<string, string> kvp in copy)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryConstructorExample2
{
public static void Main()
public static void Run()
{
// Create a new sorted dictionary of strings, with string
// keys and a case-insensitive comparer.
SortedDictionary<string, string> openWith =
new SortedDictionary<string, string>(
StringComparer.CurrentCultureIgnoreCase);

// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("Bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
StringComparer.CurrentCultureIgnoreCase)
{
// Add some elements to the dictionary.
{ "txt", "notepad.exe" },
{ "Bmp", "paint.exe" },
{ "DIB", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// Create a Dictionary of strings with string keys and a
// case-insensitive equality comparer, and initialize it
Expand All @@ -27,7 +28,7 @@ public static void Main()

// List the contents of the copy.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in copy )
foreach (KeyValuePair<string, string> kvp in copy)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryConstructorExample3
{
public static void Main()
public static void Run()
{
// Create a new Dictionary of strings, with string keys
// and a case-insensitive comparer for the current culture.
Dictionary<string, string> openWith =
new Dictionary<string, string>(
StringComparer.CurrentCultureIgnoreCase);

// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
StringComparer.CurrentCultureIgnoreCase)
{
// Add some elements to the dictionary.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "DIB", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// Try to add a fifth element with a key that is the same
// except for case; this would be allowed with the default
Expand All @@ -32,7 +33,7 @@ public static void Main()

// List the contents of the sorted dictionary.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
kvp.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryConstructorExample4
{
public static void Main()
public static void Run()
{
// Create a new dictionary of strings, with string keys and
// an initial capacity of 4.
Dictionary<string, string> openWith =
new Dictionary<string, string>(4);

// Add 4 elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
new Dictionary<string, string>(4)
{
// Add 4 elements to the dictionary.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "dib", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// List the contents of the dictionary.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryConstructorExample5
{
public static void Main()
public static void Run()
{
// Create a new dictionary of strings, with string keys, an
// initial capacity of 5, and a case-insensitive equality
// comparer.
Dictionary<string, string> openWith =
new Dictionary<string, string>(5,
StringComparer.CurrentCultureIgnoreCase);

// Add 4 elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
StringComparer.CurrentCultureIgnoreCase)
{
// Add 4 elements to the dictionary.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "DIB", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// Try to add a fifth element with a key that is the same
// except for case; this would be allowed with the default
Expand All @@ -33,7 +34,7 @@ public static void Main()

// List the contents of the dictionary.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
kvp.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DictionaryOverviewExample1.Run();
DictionaryOverviewExample2.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryOverviewExample1
{
public static void Main()
public static void Run()
{
//<Snippet1>
//<Snippet2>
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
new()
{
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "dib", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

// The Add method throws an exception if the new key is
// already in the dictionary.
Expand Down Expand Up @@ -92,7 +93,7 @@ public static void Main()
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
Expand All @@ -107,7 +108,7 @@ public static void Main()
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )
foreach (string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
}
Expand All @@ -121,7 +122,7 @@ public static void Main()
// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
foreach (string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
using System;
using System.Collections.Generic;

public class Example
public class DictionaryOverviewExample2
{
public static void Main()
public static void Run()
{
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> myDictionary =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
myDictionary.Add("txt", "notepad.exe");
myDictionary.Add("bmp", "paint.exe");
myDictionary.Add("dib", "paint.exe");
myDictionary.Add("rtf", "wordpad.exe");
new()
{
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
{ "txt", "notepad.exe" },
{ "bmp", "paint.exe" },
{ "dib", "paint.exe" },
{ "rtf", "wordpad.exe" }
};

//<Snippet11>
foreach( KeyValuePair<string, string> kvp in myDictionary )
foreach (KeyValuePair<string, string> kvp in myDictionary)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
Expand Down
Loading
Loading