Skip to content

Commit 3bc6f0b

Browse files
Copilotgewarren
andcommitted
Move System.Collections.Generic VB snippets to new organization
Co-authored-by: gewarren <[email protected]>
1 parent ce1e118 commit 3bc6f0b

File tree

29 files changed

+1026
-72
lines changed

29 files changed

+1026
-72
lines changed
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>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'<Snippet1>
2+
Imports System.Collections.Generic
3+
4+
Public Class Example
5+
6+
Public Shared Sub Main()
7+
8+
' Create a new sorted dictionary of strings, with string
9+
' keys and a case-insensitive comparer.
10+
Dim openWith As New SortedDictionary(Of String, String)( _
11+
StringComparer.CurrentCultureIgnoreCase)
12+
13+
' Add some elements to the sorted dictionary.
14+
openWith.Add("txt", "notepad.exe")
15+
openWith.Add("Bmp", "paint.exe")
16+
openWith.Add("DIB", "paint.exe")
17+
openWith.Add("rtf", "wordpad.exe")
18+
19+
' Create a Dictionary of strings with string keys and a
20+
' case-insensitive equality comparer, and initialize it
21+
' with the contents of the sorted dictionary.
22+
Dim copy As New Dictionary(Of String, String)(openWith, _
23+
StringComparer.CurrentCultureIgnoreCase)
24+
25+
' List the contents of the copy.
26+
Console.WriteLine()
27+
For Each kvp As KeyValuePair(Of String, String) In copy
28+
Console.WriteLine("Key = {0}, Value = {1}", _
29+
kvp.Key, kvp.Value)
30+
Next kvp
31+
32+
End Sub
33+
34+
End Class
35+
36+
' This code example produces the following output:
37+
'
38+
'Key = Bmp, Value = paint.exe
39+
'Key = DIB, Value = paint.exe
40+
'Key = rtf, Value = wordpad.exe
41+
'Key = txt, Value = notepad.exe
42+
'</Snippet1>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'<Snippet1>
2+
Imports System.Collections.Generic
3+
4+
Public Class Example
5+
6+
Public Shared Sub Main()
7+
8+
' Create a new dictionary of strings, with string keys and
9+
' an initial capacity of 4.
10+
Dim openWith As New Dictionary(Of String, String)(4)
11+
12+
' Add 4 elements to the dictionary.
13+
openWith.Add("txt", "notepad.exe")
14+
openWith.Add("bmp", "paint.exe")
15+
openWith.Add("dib", "paint.exe")
16+
openWith.Add("rtf", "wordpad.exe")
17+
18+
' List the contents of the dictionary.
19+
Console.WriteLine()
20+
For Each kvp As KeyValuePair(Of String, String) In openWith
21+
Console.WriteLine("Key = {0}, Value = {1}", _
22+
kvp.Key, kvp.Value)
23+
Next kvp
24+
25+
End Sub
26+
27+
End Class
28+
29+
' This code example produces the following output:
30+
'
31+
'Key = txt, Value = notepad.exe
32+
'Key = bmp, Value = paint.exe
33+
'Key = dib, Value = paint.exe
34+
'Key = rtf, Value = wordpad.exe
35+
'</Snippet1>
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>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
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>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Imports System.Collections.Generic
2+
3+
Public Class Example
4+
Public Shared Sub Main()
5+
' Create a new dictionary of strings, with string keys.
6+
'
7+
Dim exDictionary As New Dictionary(Of Integer, String)
8+
9+
' Add some elements to the dictionary. There are no
10+
' duplicate keys, but some of the values are duplicates.
11+
exDictionary.Add(0, "notepad.exe")
12+
exDictionary.Add(1, "paint.exe")
13+
exDictionary.Add(2, "paint.exe")
14+
exDictionary.Add(3, "wordpad.exe")
15+
Dim myDictionary As IDictionary(Of Integer, String) = exDictionary
16+
' <Snippet11>
17+
For Each kvp As KeyValuePair(Of Integer, String) In myDictionary
18+
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
19+
Next kvp
20+
' </Snippet11>
21+
End Sub
22+
End Class
23+
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>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
' <Snippet1>
2+
Imports System.Collections.Generic
3+
4+
Public Class Example
5+
6+
Public Shared Sub Main()
7+
8+
Dim input() As String = { "Brachiosaurus", _
9+
"Amargasaurus", _
10+
"Mamenchisaurus" }
11+
12+
Dim dinosaurs As New List(Of String)(input)
13+
14+
Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)
15+
16+
Console.WriteLine()
17+
For Each dinosaur As String In dinosaurs
18+
Console.WriteLine(dinosaur)
19+
Next
20+
21+
Console.WriteLine(vbLf & "AddRange(dinosaurs)")
22+
dinosaurs.AddRange(dinosaurs)
23+
24+
Console.WriteLine()
25+
For Each dinosaur As String In dinosaurs
26+
Console.WriteLine(dinosaur)
27+
Next
28+
29+
Console.WriteLine(vbLf & "RemoveRange(2, 2)")
30+
dinosaurs.RemoveRange(2, 2)
31+
32+
Console.WriteLine()
33+
For Each dinosaur As String In dinosaurs
34+
Console.WriteLine(dinosaur)
35+
Next
36+
37+
input = New String() { "Tyrannosaurus", _
38+
"Deinonychus", _
39+
"Velociraptor" }
40+
41+
Console.WriteLine(vbLf & "InsertRange(3, input)")
42+
dinosaurs.InsertRange(3, input)
43+
44+
Console.WriteLine()
45+
For Each dinosaur As String In dinosaurs
46+
Console.WriteLine(dinosaur)
47+
Next
48+
49+
Console.WriteLine(vbLf & "output = dinosaurs.GetRange(2, 3).ToArray")
50+
Dim output() As String = dinosaurs.GetRange(2, 3).ToArray()
51+
52+
Console.WriteLine()
53+
For Each dinosaur As String In output
54+
Console.WriteLine(dinosaur)
55+
Next
56+
57+
End Sub
58+
End Class
59+
60+
' This code example produces the following output:
61+
'
62+
'Capacity: 3
63+
'
64+
'Brachiosaurus
65+
'Amargasaurus
66+
'Mamenchisaurus
67+
'
68+
'AddRange(dinosaurs)
69+
'
70+
'Brachiosaurus
71+
'Amargasaurus
72+
'Mamenchisaurus
73+
'Brachiosaurus
74+
'Amargasaurus
75+
'Mamenchisaurus
76+
'
77+
'RemoveRange(2, 2)
78+
'
79+
'Brachiosaurus
80+
'Amargasaurus
81+
'Amargasaurus
82+
'Mamenchisaurus
83+
'
84+
'InsertRange(3, input)
85+
'
86+
'Brachiosaurus
87+
'Amargasaurus
88+
'Amargasaurus
89+
'Tyrannosaurus
90+
'Deinonychus
91+
'Velociraptor
92+
'Mamenchisaurus
93+
'
94+
'output = dinosaurs.GetRange(2, 3).ToArray
95+
'
96+
'Amargasaurus
97+
'Tyrannosaurus
98+
'Deinonychus
99+
' </Snippet1>
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>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'<Snippet1>
2+
Imports System.Collections.Generic
3+
4+
Module Example
5+
6+
Sub Main
7+
8+
Dim numbers As New Queue(Of String)
9+
numbers.Enqueue("one")
10+
numbers.Enqueue("two")
11+
numbers.Enqueue("three")
12+
numbers.Enqueue("four")
13+
numbers.Enqueue("five")
14+
15+
' A queue can be enumerated without disturbing its contents.
16+
For Each number As String In numbers
17+
Console.WriteLine(number)
18+
Next
19+
20+
Console.WriteLine(vbLf & "Dequeuing '{0}'", numbers.Dequeue())
21+
Console.WriteLine("Peek at next item to dequeue: {0}", _
22+
numbers.Peek())
23+
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue())
24+
25+
' Create a copy of the queue, using the ToArray method and the
26+
' constructor that accepts an IEnumerable(Of T).
27+
Dim queueCopy As New Queue(Of String)(numbers.ToArray())
28+
29+
Console.WriteLine(vbLf & "Contents of the first copy:")
30+
For Each number As String In queueCopy
31+
Console.WriteLine(number)
32+
Next
33+
34+
' Create an array twice the size of the queue, compensating
35+
' for the fact that Visual Basic allocates an extra array
36+
' element. Copy the elements of the queue, starting at the
37+
' middle of the array.
38+
Dim array2((numbers.Count * 2) - 1) As String
39+
numbers.CopyTo(array2, numbers.Count)
40+
41+
' Create a second queue, using the constructor that accepts an
42+
' IEnumerable(Of T).
43+
Dim queueCopy2 As New Queue(Of String)(array2)
44+
45+
Console.WriteLine(vbLf & _
46+
"Contents of the second copy, with duplicates and nulls:")
47+
For Each number As String In queueCopy2
48+
Console.WriteLine(number)
49+
Next
50+
51+
Console.WriteLine(vbLf & "queueCopy.Contains(""four"") = {0}", _
52+
queueCopy.Contains("four"))
53+
54+
Console.WriteLine(vbLf & "queueCopy.Clear()")
55+
queueCopy.Clear()
56+
Console.WriteLine(vbLf & "queueCopy.Count = {0}", _
57+
queueCopy.Count)
58+
End Sub
59+
End Module
60+
61+
' This code example produces the following output:
62+
'
63+
'one
64+
'two
65+
'three
66+
'four
67+
'five
68+
'
69+
'Dequeuing 'one'
70+
'Peek at next item to dequeue: two
71+
'
72+
'Dequeuing 'two'
73+
'
74+
'Contents of the copy:
75+
'three
76+
'four
77+
'five
78+
'
79+
'Contents of the second copy, with duplicates and nulls:
80+
'
81+
'
82+
'
83+
'three
84+
'four
85+
'five
86+
'
87+
'queueCopy.Contains("four") = True
88+
'
89+
'queueCopy.Clear()
90+
'
91+
'queueCopy.Count = 0
92+
'</Snippet1>

0 commit comments

Comments
 (0)