这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# VS Code folder:
**/.vscode/

# Mono auto generated files
mono_crash.*

Expand Down Expand Up @@ -350,3 +353,4 @@ MigrationBackup/

# binlog files for dotnet-try
**/*.binlog
NuGet.config
14 changes: 14 additions & 0 deletions csharp7/ExploreCsharpSeven/ExploreCsharpSeven.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.2.0-alpha.19174.3" />
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions csharp7/ExploreCsharpSeven/GenericConstraints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;

namespace ExploreCsharpSeven
{
public static class GenericConstraints
{
#region DeclareEnumConstraint
public static Dictionary<int, string> EnumNamedValues<T>() where T : System.Enum
{
var result = new Dictionary<int, string>();
var values = Enum.GetValues(typeof(T));

foreach (int item in values)
result.Add(item, Enum.GetName(typeof(T), item));
return result;
}
#endregion

#region DeclareEnum
enum Rainbow
{
Red,
Orange,
Yellow,
Green,
Blue,
Indigo,
Violet
}
#endregion

public static int TestEnumNamedValues()
{
#region TestMapEnumValues
var map = EnumNamedValues<Rainbow>();

foreach (var pair in map)
Console.WriteLine($"{pair.Key}:\t{pair.Value}");
#endregion
return 0;
}
}
}
65 changes: 65 additions & 0 deletions csharp7/ExploreCsharpSeven/GenericPatterns.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ExploreCsharpSeven
{
static class GenericPatterns
{
#region GenericSwitchTypePattern
public static void TestType(object obj)
{
switch (obj)
{
case 5:
Console.WriteLine("The object is 5");
break;
case int i:
Console.WriteLine($"The object is an integer: {i}");
break;
case null:
Console.WriteLine($"The object is null");
break;
case long l:
Console.WriteLine($"The object is a long: {l}");
break;
case double d:
Console.WriteLine($"The object is a double: {d}");
break;
case string s when s.StartsWith("This"):
Console.WriteLine($"This was a string that started with the word 'This': {s}");
break;
case string s when s.StartsWith("This"):
Console.WriteLine($"This was a string that started with the word 'This': {s}");
break;
case string s:
Console.WriteLine($"The object is a string: {s}");
break;
default:
Console.WriteLine($"The object is some other type");
break;
}
}
#endregion

public static int CallTestType()
{
#region GenericTestTypeWithSwitch
TestType(5);
long longValue = 12;
TestType(longValue);
int? answer = 42;
TestType(answer);
double pi = 3.14;
TestType(pi);
string sum = "12";
TestType(sum);
answer = null;
TestType(answer);
string message = "This is a longer message";
TestType(message);
#endregion
return 0;
}
}
}
60 changes: 60 additions & 0 deletions csharp7/ExploreCsharpSeven/InReadonly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

namespace ExploreCsharpSeven
{

static class InReadonly
{
#region PointStructure
public struct Point3D
{
private static Point3D origin = new Point3D(0, 0, 0);

public static Point3D Origin => origin;

public double X { get; }
public double Y { get; }
public double Z { get; }

private double? distance;

public Point3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
distance = null;
}

public double ComputeDistance()
{
if (!distance.HasValue)
distance = Math.Sqrt(X * X + Y * Y + Z * Z);
return distance.Value;
}

public static Point3D Translate(in Point3D source, double dX, double dY, double dZ) =>
new Point3D(source.X + dX, source.Y + dY, source.Z + dZ);

public override string ToString()
=> $"({X}, {Y}, {Z})";
}
#endregion

public static int ModifyTheOrigin()
{
#region UsePointstructure
var start = Point3D.Origin;
Console.WriteLine($"Start at the origin: {start}");

// Move the start:
start = Point3D.Translate(in start, 5, 5, 5);
Console.WriteLine($"Translate by (5,5,5): {start}");

Console.WriteLine($"Check the origin again: {Point3D.Origin}");
#endregion
return 0;
}

}
}
20 changes: 20 additions & 0 deletions csharp7/ExploreCsharpSeven/IsExpressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace ExploreCsharpSeven
{
static class IsExpressions
{
public static int ExploreIsPattern()
{
#region IsTypePattern
object count = 5;

if (count is int number)
Console.WriteLine(number);
else
Console.WriteLine($"{count} is not an integer");
#endregion
return 0;
}
}
}
89 changes: 89 additions & 0 deletions csharp7/ExploreCsharpSeven/LocalFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ExploreCsharpSeven
{
class LocalFunctions
{
#region LocalFunctionFactorial
public static int CalculateFactorial(int n)
{
return nthFactorial(n);

int nthFactorial(int number) => (number < 2) ?
1 : number * nthFactorial(number - 1);
}
#endregion

public static int TestLocalFactorial()
{
#region LocalFunctionFactorialTest
Console.WriteLine(CalculateFactorial(6));
#endregion
return 0;
}

#region LocalFuntionIteratorMethod
static IEnumerable<char> AlphabetSubset(char start, char end)
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");

if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");
for (var c = start; c < end; c++)
yield return c;
}
#endregion

#region LocalFunctionIteratorWithLocal
static IEnumerable<char> AlphabetSubsetLocal(char start, char end)
{
if (start < 'a' || start > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter");
if (end < 'a' || end > 'z')
throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter");

if (end <= start)
throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}");

return alphabetSubsetImplementation();

IEnumerable<char> alphabetSubsetImplementation()
{
for (var c = start; c < end; c++)
yield return c;
}
}
#endregion



public static int TestSubset()
{
#region LocalFunctionsIteratorTest
try
{
var resultSet1 = AlphabetSubset('d', 'r');
var resultSet2 = AlphabetSubset('f', 'a');
Console.WriteLine("iterators created");
foreach (var thing1 in resultSet1)
Console.Write($"{thing1}, ");
Console.WriteLine();
foreach (var thing2 in resultSet2)
Console.Write($"{thing2}, ");
Console.WriteLine();
}
catch (ArgumentException)
{
Console.WriteLine("Caught an argument exception");
}
return 1;
#endregion
}

}
}
46 changes: 46 additions & 0 deletions csharp7/ExploreCsharpSeven/OutVariableDeclarations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Linq;

namespace ExploreCsharpSeven
{
static class OutVariableDeclarations
{
public static int DeclareAtUse()
{
#region OutVariableDeclarations
var input = "1234";
if (int.TryParse(input, out int result))
Console.WriteLine(result);
else
Console.WriteLine("Could not parse input");
#endregion
return 0;
}

public static int ExploreScope()
{
#region OutVariableDeclarationScope
var input = "1234";
if (!int.TryParse(input, out int result))
{
Console.WriteLine("Could not parse input");
return 1;
}
Console.WriteLine(result);
#endregion
return 0;
}

public static int OutVarQuery()
{
#region DeclareOutQueryVariable
string[] input = { "1", "2", "3", "4", "five", "6", "7" };
var numbers = from s in input
select (success: int.TryParse(s, out int result), result);
foreach (var item in numbers)
Console.WriteLine($"{(item.success ? "Success result is: " : "Failed to parse")}\t{(item.success ? item.result.ToString() : string.Empty)}");
#endregion
return 0;
}
}
}
Loading