Skip to content

Commit 1c0c11c

Browse files
authored
Fix code snippet fragmentation in Dictionary documentation (#12148)
1 parent a1ac477 commit 1c0c11c

File tree

37 files changed

+877
-69
lines changed

37 files changed

+877
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Create a new dictionary of strings, with string keys,
10+
// and access it using the IDictionary interface.
11+
//
12+
IDictionary openWith = new Dictionary<string, string>();
13+
14+
// Add some elements to the dictionary. There are no
15+
// duplicate keys, but some of the values are duplicates.
16+
// IDictionary.Add throws an exception if incorrect types
17+
// are supplied for key or value.
18+
openWith.Add("txt", "notepad.exe");
19+
openWith.Add("bmp", "paint.exe");
20+
openWith.Add("dib", "paint.exe");
21+
openWith.Add("rtf", "wordpad.exe");
22+
23+
// Contains can be used to test keys before inserting
24+
// them.
25+
if (!openWith.Contains("ht"))
26+
{
27+
openWith.Add("ht", "hypertrm.exe");
28+
Console.WriteLine($"""Value added for key = "ht": {openWith["ht"]}""");
29+
}
30+
31+
// IDictionary.Contains returns false if the wrong data
32+
// type is supplied.
33+
Console.WriteLine($"openWith.Contains(29.7) returns {openWith.Contains(29.7)}");
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Create a new dictionary of strings, with string keys,
10+
// and access it using the IDictionary interface.
11+
//
12+
IDictionary openWith = new Dictionary<string, string>();
13+
14+
// Add some elements to the dictionary. There are no
15+
// duplicate keys, but some of the values are duplicates.
16+
// IDictionary.Add throws an exception if incorrect types
17+
// are supplied for key or value.
18+
openWith.Add("txt", "notepad.exe");
19+
openWith.Add("bmp", "paint.exe");
20+
openWith.Add("dib", "paint.exe");
21+
openWith.Add("rtf", "wordpad.exe");
22+
23+
// When you use foreach to enumerate dictionary elements
24+
// with the IDictionary interface, the elements are retrieved
25+
// as DictionaryEntry objects instead of KeyValuePair objects.
26+
foreach( DictionaryEntry de in openWith )
27+
{
28+
Console.WriteLine($"Key = {de.Key}, Value = {de.Value}");
29+
}
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Create a new dictionary of strings, with string keys,
10+
// and access it using the IDictionary interface.
11+
//
12+
IDictionary openWith = new Dictionary<string, string>();
13+
14+
// Add some elements to the dictionary. There are no
15+
// duplicate keys, but some of the values are duplicates.
16+
// IDictionary.Add throws an exception if incorrect types
17+
// are supplied for key or value.
18+
openWith.Add("txt", "notepad.exe");
19+
openWith.Add("bmp", "paint.exe");
20+
openWith.Add("dib", "paint.exe");
21+
openWith.Add("rtf", "wordpad.exe");
22+
23+
// The Item property is another name for the indexer, so you
24+
// can omit its name when accessing elements.
25+
Console.WriteLine($"""For key = "rtf", value = {openWith["rtf"]}.""");
26+
27+
// The indexer can be used to change the value associated
28+
// with a key.
29+
openWith["rtf"] = "winword.exe";
30+
Console.WriteLine($"""For key = "rtf", value = {openWith["rtf"]}.""");
31+
32+
// If a key does not exist, setting the indexer for that key
33+
// adds a new key/value pair.
34+
openWith["doc"] = "winword.exe";
35+
36+
// The indexer returns null if the key is of the wrong data
37+
// type.
38+
Console.WriteLine("The indexer returns null"
39+
+ " if the key is of the wrong type:");
40+
Console.WriteLine($"For key = 2, value = {openWith[2]}.");
41+
42+
// The indexer throws an exception when setting a value
43+
// if the key is of the wrong data type.
44+
try
45+
{
46+
openWith[2] = "This does not get added.";
47+
}
48+
catch (ArgumentException)
49+
{
50+
Console.WriteLine("A key of the wrong type was specified"
51+
+ " when assigning to the indexer.");
52+
}
53+
54+
// Unlike the default Item property on the Dictionary class
55+
// itself, IDictionary.Item does not throw an exception
56+
// if the requested key is not in the dictionary.
57+
Console.WriteLine($"""For key = "tif", value = {openWith["tif"]}.""");
58+
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Create a new dictionary of strings, with string keys,
10+
// and access it using the IDictionary interface.
11+
//
12+
IDictionary openWith = new Dictionary<string, string>();
13+
14+
// Add some elements to the dictionary. There are no
15+
// duplicate keys, but some of the values are duplicates.
16+
// IDictionary.Add throws an exception if incorrect types
17+
// are supplied for key or value.
18+
openWith.Add("txt", "notepad.exe");
19+
openWith.Add("bmp", "paint.exe");
20+
openWith.Add("dib", "paint.exe");
21+
openWith.Add("rtf", "wordpad.exe");
22+
23+
// To get the keys alone, use the Keys property.
24+
ICollection icoll = openWith.Keys;
25+
26+
// The elements of the collection are strongly typed
27+
// with the type that was specified for dictionary keys,
28+
// even though the ICollection interface is not strongly
29+
// typed.
30+
Console.WriteLine();
31+
foreach( string s in icoll )
32+
{
33+
Console.WriteLine($"Key = {s}");
34+
}
35+
36+
// When you use foreach to enumerate dictionary elements
37+
// with the IDictionary interface, the elements are retrieved
38+
// as DictionaryEntry objects instead of KeyValuePair objects.
39+
Console.WriteLine();
40+
foreach( DictionaryEntry de in openWith )
41+
{
42+
Console.WriteLine($"Key = {de.Key}, Value = {de.Value}");
43+
}
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Create a new dictionary of strings, with string keys,
10+
// and access it using the IDictionary interface.
11+
//
12+
IDictionary openWith = new Dictionary<string, string>();
13+
14+
// Add some elements to the dictionary. There are no
15+
// duplicate keys, but some of the values are duplicates.
16+
// IDictionary.Add throws an exception if incorrect types
17+
// are supplied for key or value.
18+
openWith.Add("txt", "notepad.exe");
19+
openWith.Add("bmp", "paint.exe");
20+
openWith.Add("dib", "paint.exe");
21+
openWith.Add("rtf", "wordpad.exe");
22+
23+
// Use the Remove method to remove a key/value pair. No
24+
// exception is thrown if the wrong data type is supplied.
25+
Console.WriteLine($"""{"\n"}Remove("dib")""");
26+
openWith.Remove("dib");
27+
28+
if (!openWith.Contains("dib"))
29+
{
30+
Console.WriteLine("""Key "dib" is not found.""");
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)