.NET 6 Examples
Note: See .NET 5 examples
const string Bar = "Bar"; | |
const string DoubleBar = $"{Bar}_{Bar}"; | |
WriteLine(DoubleBar); |
Note: See .NET 5 examples
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
</Project> |
Battery battery = new("CR2032", 0.235, 100); | |
WriteLine(battery); | |
while (battery.RemainingCapacityPercentage > 0) | |
{ | |
Battery updatedBattery = battery with {RemainingCapacityPercentage = battery.RemainingCapacityPercentage - 1}; | |
battery = updatedBattery; | |
} |
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
</Project> |
Battery battery = new("CR2032", 0.235, 100); | |
WriteLine(battery); | |
while (battery.RemainingCapacityPercentage > 0) | |
{ | |
battery.RemainingCapacityPercentage--; | |
} | |
WriteLine(battery); |
namespace XUnitTestProject | |
{ | |
using Xunit; | |
public static class SwitchStatementMapper | |
{ | |
// Don't judge me, it's only for educational purposes :) | |
public static bool Map(string str) => str switch | |
{ | |
"true" => true, |
There are lots of cases that you can improve. The examples use nullable reference types, but only the WhenNotNull
example requires it.
IsNullorEmpty
Consider adopting the new property pattern, wherever you use IsNullOrEmpty
.
string? hello = "hello world";
The following are examples of various features.
using System; | |
using System.Net.Http; | |
using System.Net.Http.Json; | |
string serviceURL = "https://localhost:5001/WeatherForecast"; | |
HttpClient client = new(); | |
Forecast[] forecasts = await client.GetFromJsonAsync<Forecast[]>(serviceURL); | |
foreach(Forecast forecast in forecasts) | |
{ |