« Csharp 8 » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(→Liens) |
|||
Ligne 19 : | Ligne 19 : | ||
_ => throw new Exception("Invalid value") | _ => throw new Exception("Invalid value") | ||
}; | }; | ||
</kode> | |||
= Using declaration = | |||
<kode lang='cs'> | |||
using (var file = new StreamWriter("MyFile.txt")) | |||
{ | |||
// some code | |||
} // file is disposed here | |||
void MyMethod() | |||
{ | |||
using var file = new StreamWriter("MyFile.txt"); | |||
// some code | |||
// file is disposed here, at the end of the enclosing scope | |||
} | |||
</kode> | </kode> | ||
Version du 4 mars 2020 à 15:45
Liens
Switch expression
switch (i) { case 1: return "One"; default: throw new Exception("Invalid value"); } i switch { 1 => "One", _ => throw new Exception("Invalid value") }; |
Using declaration
using (var file = new StreamWriter("MyFile.txt")) { // some code } // file is disposed here void MyMethod() { using var file = new StreamWriter("MyFile.txt"); // some code // file is disposed here, at the end of the enclosing scope } |
.NET Framework
The C# 8 / .NET Framework combination is not officially supported by Microsoft.
It can be forced by editing the csproj file.
MyProject.csproj |
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <LangVersion>8.0</LangVersion> |
- default interface members won't work
- ssynchronous streams and indices and ranges will need polyfill nuget package