Skip to content

Commit d07255c

Browse files
author
Ron Petrusha
authored
Merge pull request #829 from dotnet/master
Update Live with current Master
2 parents c247dfc + 2913ab5 commit d07255c

File tree

9 files changed

+216
-14
lines changed

9 files changed

+216
-14
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace RangesIndexes
5+
{
6+
class IndicesAndRanges
7+
{
8+
// <SnippetIndicesAndRanges_Initialization>
9+
private string[] words = new string[]
10+
{
11+
// index from start index from end
12+
"The", // 0 ^9
13+
"quick", // 1 ^8
14+
"brown", // 2 ^7
15+
"fox", // 3 ^6
16+
"jumped", // 4 ^5
17+
"over", // 5 ^4
18+
"the", // 6 ^3
19+
"lazy", // 7 ^2
20+
"dog" // 8 ^1
21+
};
22+
// </SnippetIndicesAndRanges_Initialization>
23+
24+
internal int Syntax_LastIndex()
25+
{
26+
// <SnippetIndicesAndRanges_LastIndex>
27+
Console.WriteLine($"The last word is {words[^1]}");
28+
// </SnippetIndicesAndRanges_LastIndex>
29+
return 0;
30+
}
31+
32+
internal int Syntax_Range()
33+
{
34+
// <SnippetIndicesAndRanges_Range>
35+
var quickBrownFox = words[1..4];
36+
foreach (var word in quickBrownFox)
37+
Console.Write($"< {word} >");
38+
Console.WriteLine();
39+
// </SnippetIndicesAndRanges_Range>
40+
return 0;
41+
}
42+
43+
internal int Syntax_LastRange()
44+
{
45+
// <SnippetIndicesAndRanges_LastRange>
46+
var lazyDog = words[^2..^0];
47+
foreach (var word in lazyDog)
48+
Console.Write($"< {word} >");
49+
Console.WriteLine();
50+
// </SnippetIndicesAndRanges_LastRange>
51+
return 0;
52+
}
53+
54+
internal int Syntax_PartialRange()
55+
{
56+
// <SnippetIndicesAndRanges_PartialRanges>
57+
var allWords = words[..]; // contains "The" through "dog".
58+
var firstPhrase = words[..4]; // contains "The" through "fox"
59+
var lastPhrase = words[6..]; // contains "the, "lazy" and "dog"
60+
foreach (var word in allWords)
61+
Console.Write($"< {word} >");
62+
Console.WriteLine();
63+
foreach (var word in firstPhrase)
64+
Console.Write($"< {word} >");
65+
Console.WriteLine();
66+
foreach (var word in lastPhrase)
67+
Console.Write($"< {word} >");
68+
Console.WriteLine();
69+
// </SnippetIndicesAndRanges_PartialRanges>
70+
return 0;
71+
}
72+
73+
internal int Syntax_IndexRangeType()
74+
{
75+
// <SnippetIndicesAndRanges_RangeIndexTypes>
76+
Index the = ^3;
77+
Console.WriteLine(words[the]);
78+
Range phrase = 1..4;
79+
var text = words[phrase];
80+
foreach (var word in text)
81+
Console.Write($"< {word} >");
82+
Console.WriteLine();
83+
// </SnippetIndicesAndRanges_RangeIndexTypes>
84+
return 0;
85+
}
86+
87+
internal int Syntax_WhyChosenSemantics()
88+
{
89+
// <SnippetIndicesAndRanges_Semantics>
90+
var numbers = Enumerable.Range(0, 100).ToArray();
91+
int x = 12;
92+
int y = 25;
93+
int z = 36;
94+
95+
Console.WriteLine($"{numbers[^x]} is the same as {numbers[numbers.Length - x]}");
96+
Console.WriteLine($"{numbers[x..y].Length} is the same as {y - x}");
97+
98+
Console.WriteLine("numbers[x..y] and numbers[y..z] are consecutive and disjoint:");
99+
Span<int> x_y = numbers[x..y];
100+
Span<int> y_z = numbers[y..z];
101+
Console.WriteLine($"\tnumbers[x..y] is {x_y[0]} through {x_y[^1]}, numbers[y..z] is {y_z[0]} through {y_z[^1]}");
102+
103+
Console.WriteLine("numbers[x..^x] removes x elements at each end:");
104+
Span<int> x_x = numbers[x..^x];
105+
Console.WriteLine($"\tnumbers[x..^x] starts with {x_x[0]} and ends with {x_x[^1]}");
106+
107+
Console.WriteLine("numbers[..x] means numbers[0..x] and numbers[x..] means numbers[x..^0]");
108+
Span<int> start_x = numbers[..x];
109+
Span<int> zero_x = numbers[0..x];
110+
Console.WriteLine($"\t{start_x[0]}..{start_x[^1]} is the same as {zero_x[0]}..{zero_x[^1]}");
111+
Span<int> z_end = numbers[z..];
112+
Span<int> z_zero = numbers[z..];
113+
Console.WriteLine($"\t{z_end[0]}..{z_end[^1]} is the same as {z_zero[0]}..{z_zero[^1]}");
114+
// </SnippetIndicesAndRanges_Semantics>
115+
return 0;
116+
}
117+
118+
internal int ComputeMovingAverages()
119+
{
120+
// <SnippetIndicesAndRanges_MovingAverage>
121+
int[] sequence = Sequence(1000);
122+
123+
124+
for(int start = 0; start < sequence.Length; start += 100)
125+
{
126+
Range r = start..start+10;
127+
var (min, max, average) = MovingAverage(sequence, r);
128+
Console.WriteLine($"From {r.Start} to {r.End}: \tMin: {min},\tMax: {max},\tAverage: {average}");
129+
}
130+
131+
for (int start = 0; start < sequence.Length; start += 100)
132+
{
133+
Range r = ^(start + 10)..^start;
134+
var (min, max, average) = MovingAverage(sequence, r);
135+
Console.WriteLine($"From {r.Start} to {r.End}: \tMin: {min},\tMax: {max},\tAverage: {average}");
136+
}
137+
138+
(int min, int max, double average) MovingAverage(int[] subSequence, Range range) =>
139+
(
140+
subSequence[range].Min(),
141+
subSequence[range].Max(),
142+
subSequence[range].Average()
143+
);
144+
145+
int[] Sequence(int count) =>
146+
Enumerable.Range(0, count).Select(x => (int)(Math.Sqrt(x) * 100)).ToArray();
147+
// </SnippetIndicesAndRanges_MovingAverage>
148+
149+
return 0;
150+
}
151+
}
152+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace RangesIndexes
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("========== Starting Index and Range Samples. ==========");
10+
var indexSamples = new IndicesAndRanges();
11+
Console.WriteLine(" ========== Last Index. ==========");
12+
indexSamples.Syntax_LastIndex();
13+
Console.WriteLine(" ========== Range. ==========");
14+
indexSamples.Syntax_Range();
15+
Console.WriteLine(" ========== Last Range. ==========");
16+
indexSamples.Syntax_LastRange();
17+
Console.WriteLine(" ========== Partial Range. ==========");
18+
indexSamples.Syntax_PartialRange();
19+
Console.WriteLine(" ========== Index and Range types. ==========");
20+
indexSamples.Syntax_IndexRangeType();
21+
Console.WriteLine(" ========== Why this syntax. ==========");
22+
indexSamples.Syntax_WhyChosenSemantics();
23+
Console.WriteLine(" ========== Scenario. ==========");
24+
indexSamples.ComputeMovingAverages();
25+
}
26+
}
27+
}
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>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/cpp/VS_Snippets_Remoting/System.Net.IPAddress/CPP/ipaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void IPAddresses( String^ server )
3939

4040
//<Snippet3>
4141
// Display the type of address family supported by the server. If the
42-
// server is IPv6-enabled this value is: InternNetworkV6. If the server
42+
// server is IPv6-enabled this value is: InterNetworkV6. If the server
4343
// is also IPv4-enabled there will be an additional value of InterNetwork.
4444
Console::WriteLine( "AddressFamily: {0}", curAdd->AddressFamily );
4545

snippets/csharp/VS_Snippets_Remoting/System.Net.IPAddress/CS/ipaddress.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void IPAddresses(string server)
4545
//<Snippet3>
4646

4747
// Display the type of address family supported by the server. If the
48-
// server is IPv6-enabled this value is: InternNetworkV6. If the server
48+
// server is IPv6-enabled this value is: InterNetworkV6. If the server
4949
// is also IPv4-enabled there will be an additional value of InterNetwork.
5050
Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());
5151

@@ -152,4 +152,4 @@ public static void Main(string[] args)
152152

153153
}
154154
}
155-
// </Snippet1>
155+
// </Snippet1>

snippets/csharp/framework/migration-guide/versions-installed3.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ private static void Get45PlusFromRegistry()
2525
// Checking the version using >= enables forward compatibility.
2626
string CheckFor45PlusVersion(int releaseKey)
2727
{
28-
if (releaseKey > 461814)
29-
return "4.7.2 or later";
28+
if (releaseKey >= 528049)
29+
return "4.8 or later";
3030
if (releaseKey >= 461808)
3131
return "4.7.2";
3232
if (releaseKey >= 461308)

snippets/csharp/language-reference/operators/ArithmeticOperators.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static void Examples()
2828

2929
Console.WriteLine("==== Compound assignment");
3030
CompoundAssignment();
31+
CompoundAssignmentWithCast();
3132

3233
Console.WriteLine("==== Special cases");
3334
CheckedUnchecked();
@@ -194,6 +195,21 @@ private static void CompoundAssignment()
194195
// </SnippetCompoundAssignment>
195196
}
196197

198+
private static void CompoundAssignmentWithCast()
199+
{
200+
// <SnippetCompoundAssignmentWithCast>
201+
byte a = 200;
202+
byte b = 100;
203+
204+
var c = a + b;
205+
Console.WriteLine(c.GetType()); // output: System.Int32
206+
Console.WriteLine(c); // output: 300
207+
208+
a += b;
209+
Console.WriteLine(a); // output: 44
210+
// </SnippetCompoundAssignmentWithCast>
211+
}
212+
197213
private static void CheckedUnchecked()
198214
{
199215
// <SnippetCheckedUnchecked>

snippets/visualbasic/VS_Snippets_Remoting/System.Net.IPAddress/VB/ipaddress.vb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
' <Snippet1>
1212
' This program shows how to use the IPAddress class to obtain a server
1313
' IP addressess and related information.
14-
Imports System
1514
Imports System.Net
1615
Imports System.Net.Sockets
1716
Imports System.Text.RegularExpressions
@@ -22,7 +21,10 @@ Namespace Mssc.Services.ConnectionManagement
2221
Module M_TestIPAddress
2322

2423
Class TestIPAddress
25-
'The IPAddresses method obtains the selected server IP address information. 'It then displays the type of address family supported by the server and 'its IP address in standard and byte format.
24+
25+
'The IPAddresses method obtains the selected server IP address information.
26+
'It then displays the type of address family supported by the server and
27+
'its IP address in standard and byte format.
2628
Private Shared Sub IPAddresses(ByVal server As String)
2729
Try
2830
Dim ASCII As New System.Text.ASCIIEncoding()
@@ -36,7 +38,7 @@ Namespace Mssc.Services.ConnectionManagement
3638

3739
'<Snippet3>
3840
' Display the type of address family supported by the server. If the
39-
' server is IPv6-enabled this value is: InternNetworkV6. If the server
41+
' server is IPv6-enabled this value is: InterNetworkV6. If the server
4042
' is also IPv4-enabled there will be an additional value of InterNetwork.
4143
Console.WriteLine(("AddressFamily: " + curAdd.AddressFamily.ToString()))
4244

@@ -105,9 +107,6 @@ Namespace Mssc.Services.ConnectionManagement
105107
' Define a regular expression to parse user's input.
106108
' This is a security check. It allows only
107109
' alphanumeric input string between 2 to 40 character long.
108-
'Define a regular expression to parse user's input.
109-
'This is a security check. It allows only
110-
'alphanumeric input string between 2 to 40 character long.
111110
Dim rex As New Regex("^[a-zA-Z]\w{1,39}$")
112111

113112
If args.Length < 1 Then
@@ -132,4 +131,4 @@ Namespace Mssc.Services.ConnectionManagement
132131
End Class 'TestIPAddress
133132
End Module
134133
End Namespace
135-
' </Snippet1>
134+
' </Snippet1>

snippets/visualbasic/framework/migration-guide/versions-installed3.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Public Module GetDotNetVersion
1919

2020
' Checking the version using >= will enable forward compatibility.
2121
Private Function CheckFor45PlusVersion(releaseKey As Integer) As String
22-
If releaseKey > 461814 Then
23-
Return "4.7.2 or later"
22+
If releaseKey >= 528049 Then
23+
Return "4.8 or later"
2424
Else If releaseKey >= 461808 Then
2525
Return "4.7.2"
2626
Else If releaseKey >= 461308 Then

0 commit comments

Comments
 (0)