D G

351
reputation
1
11
string x = null!;


string y1 = x ?? "other";
string y2 = x is string ? x : "other";
string y3 = x is string __ ? __ : "other";
string y4 = x is null ? "other" : x;
if (x is not string y5) y5 = "other";
string y6 = x!; y6 ??= "other";


Console.WriteLine($"y1: {y1}, y2: {y2}, y3: {y3}, y4: {y4}, y5: {y5}, y6: {y6}");

stupid mistakes:

using System.Data.Entity; // Wrong namespace!
private static async Task<IResult> Gets(StudentEnrollmentDbContext db)
{
    return Results.Ok(await db.Courses.AsNoTracking().ToArrayAsync());
}
using Microsoft.EntityFrameworkCore; // Correct namespace!
private static async Task<IResult> Gets(StudentEnrollmentDbContext db)
{
    return Results.Ok(await db.Courses.AsNoTracking().ToArrayAsync());
}

Returning a new List<int> for every get access

public List<int> Data1 => new List<int>();
public List<int> Data1 { get => new List<int>(); }

Initialiazed read-only field or property.

public List<int> Data2 { get; } = new List<int>();
public readonly List<int> Data2 = new List<int>();
static async Task Main()
{
    var f = async (char c) =>
    {
        for (int i = 0; i < 5; ++i)
        {
            Console.WriteLine(c);
            await Task.Yield();
        }
    };
Console.WriteLine(&quot;Begin&quot;);
var a = f('*');
var b = f('-');
await Task.WhenAll(a, b);
Console.WriteLine(&quot;End&quot;);

}

and

async function Main() {
    let f = async (c) => {
        for (let i = 0; i < 5; ++i) {
            console.log(c)
            await Promise.resolve();
        }
    }
    console.log('Begin');
    const a = f('*');
    const b = f('-');
    await Promise.all([a, b]);
    console.log('End');
}