The document discusses new features in C# 8 including readonly members, default interface methods, pattern matching with switch expressions and property patterns, using declarations, static local functions, asynchronous streams with IAsyncEnumerable, and null-coalescing assignment. Some key features covered are readonly members to prevent modification of struct fields and methods, default interface methods to provide implementation in interfaces, switch expressions as a cleaner alternative to switch statements, and property patterns for matching on property values in a switch expression.
3. public struct Point
{
public double X { get; set; }
public double Y { get; set; }
public double Distance => Math.Sqrt(X * X + Y * Y);
public override string ToString() =>
$"({X}, {Y}) is {Distance} from the origin";
}
4. public struct Point
{
public double X { get; set; }
public double Y { get; set; }
public double Distance => Math.Sqrt(X * X + Y * Y);
public readonly override string ToString() =>
$"({X}, {Y}) is {Distance} from the origin";
}
5. public struct Point
{
public double X { get; set; }
public double Y { get; set; }
public readonly double Distance => Math.Sqrt(X * X +
Y * Y);
public readonly override string ToString() =>
$"({X}, {Y}) is {Distance} from the origin";
}
6. public readonly void Translate(int xOffset,
int yOffset)
{
X += xOffset;
Y += yOffset;
}
18. public static RGBColor FromRainbowClassic(Rainbow colorBand)
{
switch (colorBand)
{
case Rainbow.Red:
return new RGBColor(0xFF, 0x00, 0x00);
case Rainbow.Orange:
return new RGBColor(0xFF, 0x7F, 0x00);
case Rainbow.Yellow:
default:
throw new ArgumentException();
};
}
19. public static RGBColor FromRainbow(Rainbow colorBand) =>
colorBand switch
{
Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00),
Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00),
Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF),
Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
_ => throw new ArgumentException(),
};
23. int WriteLinesToFile(IEnumerable<string> lines)
{
int skippedLines = 0;
using (var file = new StreamWriter("WriteLines2.txt"))
{
foreach (string line in lines)
{
}
} // file is disposed here
return skippedLines;
}
33. Asynchronous streams
• System.Index representa un índice en una secuencia.
• índice desde el operador final ^
• System.Range representa un subrango de una secuencia.
• El operador de intervalo ..
34. var 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
}; // 9 (or words.Le