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
152 changes: 152 additions & 0 deletions csharp/tutorials/RangesIndexes/IndicesAndRanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Linq;

namespace RangesIndexes
{
class IndicesAndRanges
{
// <SnippetIndicesAndRanges_Initialization>
private string[] words = new string[]
{
// index from start index from end
"The", // 0 ^9
"quick", // 1 ^8
"brown", // 2 ^7
"fox", // 3 ^6
"jumped", // 4 ^5
"over", // 5 ^4
"the", // 6 ^3
"lazy", // 7 ^2
"dog" // 8 ^1
};
// </SnippetIndicesAndRanges_Initialization>

internal int Syntax_LastIndex()
{
// <SnippetIndicesAndRanges_LastIndex>
Console.WriteLine($"The last word is {words[^1]}");
// </SnippetIndicesAndRanges_LastIndex>
return 0;
}

internal int Syntax_Range()
{
// <SnippetIndicesAndRanges_Range>
var quickBrownFox = words[1..4];
foreach (var word in quickBrownFox)
Console.Write($"< {word} >");
Console.WriteLine();
// </SnippetIndicesAndRanges_Range>
return 0;
}

internal int Syntax_LastRange()
{
// <SnippetIndicesAndRanges_LastRange>
var lazyDog = words[^2..^0];
foreach (var word in lazyDog)
Console.Write($"< {word} >");
Console.WriteLine();
// </SnippetIndicesAndRanges_LastRange>
return 0;
}

internal int Syntax_PartialRange()
{
// <SnippetIndicesAndRanges_PartialRanges>
var allWords = words[..]; // contains "The" through "dog".
var firstPhrase = words[..4]; // contains "The" through "fox"
var lastPhrase = words[6..]; // contains "the, "lazy" and "dog"
foreach (var word in allWords)
Console.Write($"< {word} >");
Console.WriteLine();
foreach (var word in firstPhrase)
Console.Write($"< {word} >");
Console.WriteLine();
foreach (var word in lastPhrase)
Console.Write($"< {word} >");
Console.WriteLine();
// </SnippetIndicesAndRanges_PartialRanges>
return 0;
}

internal int Syntax_IndexRangeType()
{
// <SnippetIndicesAndRanges_RangeIndexTypes>
Index the = ^3;
Console.WriteLine(words[the]);
Range phrase = 1..4;
var text = words[phrase];
foreach (var word in text)
Console.Write($"< {word} >");
Console.WriteLine();
// </SnippetIndicesAndRanges_RangeIndexTypes>
return 0;
}

internal int Syntax_WhyChosenSemantics()
{
// <SnippetIndicesAndRanges_Semantics>
var numbers = Enumerable.Range(0, 100).ToArray();
int x = 12;
int y = 25;
int z = 36;

Console.WriteLine($"{numbers[^x]} is the same as {numbers[numbers.Length - x]}");
Console.WriteLine($"{numbers[x..y].Length} is the same as {y - x}");

Console.WriteLine("numbers[x..y] and numbers[y..z] are consecutive and disjoint:");
Span<int> x_y = numbers[x..y];
Span<int> y_z = numbers[y..z];
Console.WriteLine($"\tnumbers[x..y] is {x_y[0]} through {x_y[^1]}, numbers[y..z] is {y_z[0]} through {y_z[^1]}");

Console.WriteLine("numbers[x..^x] removes x elements at each end:");
Span<int> x_x = numbers[x..^x];
Console.WriteLine($"\tnumbers[x..^x] starts with {x_x[0]} and ends with {x_x[^1]}");

Console.WriteLine("numbers[..x] means numbers[0..x] and numbers[x..] means numbers[x..^0]");
Span<int> start_x = numbers[..x];
Span<int> zero_x = numbers[0..x];
Console.WriteLine($"\t{start_x[0]}..{start_x[^1]} is the same as {zero_x[0]}..{zero_x[^1]}");
Span<int> z_end = numbers[z..];
Span<int> z_zero = numbers[z..];
Console.WriteLine($"\t{z_end[0]}..{z_end[^1]} is the same as {z_zero[0]}..{z_zero[^1]}");
// </SnippetIndicesAndRanges_Semantics>
return 0;
}

internal int ComputeMovingAverages()
{
// <SnippetIndicesAndRanges_MovingAverage>
int[] sequence = Sequence(1000);


for(int start = 0; start < sequence.Length; start += 100)
{
Range r = start..start+10;
var (min, max, average) = MovingAverage(sequence, r);
Console.WriteLine($"From {r.Start} to {r.End}: \tMin: {min},\tMax: {max},\tAverage: {average}");
}

for (int start = 0; start < sequence.Length; start += 100)
{
Range r = ^(start + 10)..^start;
var (min, max, average) = MovingAverage(sequence, r);
Console.WriteLine($"From {r.Start} to {r.End}: \tMin: {min},\tMax: {max},\tAverage: {average}");
}

(int min, int max, double average) MovingAverage(int[] subSequence, Range range) =>
(
subSequence[range].Min(),
subSequence[range].Max(),
subSequence[range].Average()
);

int[] Sequence(int count) =>
Enumerable.Range(0, count).Select(x => (int)(Math.Sqrt(x) * 100)).ToArray();
// </SnippetIndicesAndRanges_MovingAverage>

return 0;
}
}
}
27 changes: 27 additions & 0 deletions csharp/tutorials/RangesIndexes/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace RangesIndexes
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("========== Starting Index and Range Samples. ==========");
var indexSamples = new IndicesAndRanges();
Console.WriteLine(" ========== Last Index. ==========");
indexSamples.Syntax_LastIndex();
Console.WriteLine(" ========== Range. ==========");
indexSamples.Syntax_Range();
Console.WriteLine(" ========== Last Range. ==========");
indexSamples.Syntax_LastRange();
Console.WriteLine(" ========== Partial Range. ==========");
indexSamples.Syntax_PartialRange();
Console.WriteLine(" ========== Index and Range types. ==========");
indexSamples.Syntax_IndexRangeType();
Console.WriteLine(" ========== Why this syntax. ==========");
indexSamples.Syntax_WhyChosenSemantics();
Console.WriteLine(" ========== Scenario. ==========");
indexSamples.ComputeMovingAverages();
}
}
}
8 changes: 8 additions & 0 deletions csharp/tutorials/RangesIndexes/RangesIndexes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void IPAddresses( String^ server )

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static void IPAddresses(string server)
//<Snippet3>

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

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

}
}
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ private static void Get45PlusFromRegistry()
// Checking the version using >= enables forward compatibility.
string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey > 461814)
return "4.7.2 or later";
if (releaseKey >= 528049)
return "4.8 or later";
if (releaseKey >= 461808)
return "4.7.2";
if (releaseKey >= 461308)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static void Examples()

Console.WriteLine("==== Compound assignment");
CompoundAssignment();
CompoundAssignmentWithCast();

Console.WriteLine("==== Special cases");
CheckedUnchecked();
Expand Down Expand Up @@ -194,6 +195,21 @@ private static void CompoundAssignment()
// </SnippetCompoundAssignment>
}

private static void CompoundAssignmentWithCast()
{
// <SnippetCompoundAssignmentWithCast>
byte a = 200;
byte b = 100;

var c = a + b;
Console.WriteLine(c.GetType()); // output: System.Int32
Console.WriteLine(c); // output: 300

a += b;
Console.WriteLine(a); // output: 44
// </SnippetCompoundAssignmentWithCast>
}

private static void CheckedUnchecked()
{
// <SnippetCheckedUnchecked>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
' <Snippet1>
' This program shows how to use the IPAddress class to obtain a server
' IP addressess and related information.
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.RegularExpressions
Expand All @@ -22,7 +21,10 @@ Namespace Mssc.Services.ConnectionManagement
Module M_TestIPAddress

Class TestIPAddress
'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.

'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.
Private Shared Sub IPAddresses(ByVal server As String)
Try
Dim ASCII As New System.Text.ASCIIEncoding()
Expand All @@ -36,7 +38,7 @@ Namespace Mssc.Services.ConnectionManagement

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

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

If args.Length < 1 Then
Expand All @@ -132,4 +131,4 @@ Namespace Mssc.Services.ConnectionManagement
End Class 'TestIPAddress
End Module
End Namespace
' </Snippet1>
' </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Public Module GetDotNetVersion

' Checking the version using >= will enable forward compatibility.
Private Function CheckFor45PlusVersion(releaseKey As Integer) As String
If releaseKey > 461814 Then
Return "4.7.2 or later"
If releaseKey >= 528049 Then
Return "4.8 or later"
Else If releaseKey >= 461808 Then
Return "4.7.2"
Else If releaseKey >= 461308 Then
Expand Down