Skip to content

Instantly share code, notes, and snippets.

View nishanc's full-sized avatar
🚀
This is the way!

Nishan Chathuranga Wickramarathna nishanc

🚀
This is the way!
View GitHub Profile
await foreach (var item in GetItemsAsync())
{
Console.WriteLine(item.value);
}
var result = obj switch
{
> 0 => "Positive",
< 0 => "Negative",
_ => "Zero"
};
var result = obj switch
{
int i when i > 0 => "Positive",
int i when i < 0 => "Negative",
_ => "Zero"
};
foreach (var blog in blogs)
{
if (context.Entry(blog).Collection(e => e.Posts).IsLoaded)
{
Console.WriteLine($" Posts for blog '{blog.Name}' are loaded.");
}
}
var blogs = await context.Blogs.AsNoTracking().ToListAsync();
if (int.TryParse(ReadLine(), out var blogId))
{
Console.WriteLine("Posts:");
foreach (var post in blogs[blogId - 1].Posts)
{
Console.WriteLine($" {post.Title}");
}
}
AppContext.SetData("GCHeapHardLimit", (ulong)100 * 1024 * 1024);
MethodInfo refreshMemoryLimitMethod = typeof(GC).GetMethod(
"_RefreshMemoryLimit", BindingFlags.NonPublic | BindingFlags.Static);
refreshMemoryLimitMethod.Invoke(null, Array<object>.Empty);
// Hashing example
if (SHA3_256.IsSupported)
{
byte[] hash = SHA3_256.HashData(dataToHash);
}
else
{
// ...
}
YourType[] trainingData = LoadTrainingData();
Random.Shared.Shuffle(trainingData);
private static ReadOnlySpan<Button> s_allButtons = new[]
{
Button.Red,
Button.Green,
Button.Blue,
Button.Yellow,
};
// ...
// Get system time.
DateTimeOffset utcNow = TimeProvider.System.GetUtcNow();
DateTimeOffset localNow = TimeProvider.System.GetLocalNow();
// Create a time provider that works with a
// time zone that's different than the local time zone.
private class ZonedTimeProvider : TimeProvider
{
private TimeZoneInfo _zoneInfo;