Skip to content

Instantly share code, notes, and snippets.

@gurustron
gurustron / TaskWhenEachBenchMark.cs
Last active March 26, 2025 19:21
Stupid bench =)
// no significant difference found
[MemoryDiagnoser]
[ThreadingDiagnoser]
public class TaskWhenEachBenchMark
{
private IEnumerable<Task<(int Index, int Delay)>> GetTasks()
{
int[] delays = [11, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1]; // play with me!
@gurustron
gurustron / arrays.md
Last active September 23, 2024 08:46
Arrays

C# works on the base of types, Array's represent a non-readonly collection, so the following:

Now, the subset only needs to be read only.

Can't be implemented without changing type accepted by the method. Depending on the case you can consider using either ReadOnlySpan<T> or ReadOnlyMemory<T> which were designed for such cases.

Note that arrays in C# are not "just pointers" to some block of memory, they are objects with quite specific structure (see the Managed object internals, Part 3. The layout of a managed array article). If method signature can't be changed you can try leveraging this fact (on your own risk of course, since this structure is

@gurustron
gurustron / DictSearchWithConsumeBenchmarks.cs
Last active December 22, 2023 12:25
TryGetValue benchmark
public class DictSearchWithConsumeBenchmarks
{
public record Item(int Id, int Value);
private const int ItemCount = 50;
private Item[] _itemList = new Item[ItemCount];
private int[] _searchIds = new int[ItemCount];
private Dictionary<int, Item> _itemDictionary = new();
private FrozenDictionary<int, Item> _itemFrozenDictionary;
@gurustron
gurustron / Answer.md
Created May 9, 2023 19:08
Answer to closed question

So to answer your direct answer - you can't access SomeProperty on object, so you need to use parameter of object, so if you want the parameters to be of the object type you will need perform cast on the parameter:

var parameter1 = Expression.Parameter(typeof(object));
var parameter2 = Expression.Parameter(typeof(object));
var property1 = Expression.Property(Expression.Convert(parameter1, type), propertyInfo);
var property2 = Expression.Property(Expression.Convert(parameter2, type), propertyInfo);
var result = Expression.AndAlso(property1, property2);
var lambda = Expression.Lambda<Func<object, object, bool>>(result, new[] { parameter1, parameter2 });

Books

Don't have books I could recommend for C# newbie, so lets go over my personal favorites

  1. C# 10 in a Nutshell: The Definitive Reference 1st Edition by Joseph Albahari
  2. C# in Depth, Fourth Edition by Jon Skeet
  3. LINQ Pocket Reference: Learn and Implement LINQ for .NET Applications by Joseph Albahari (Author), Ben Albahari (Author)
    Some LINQ stuff
  4. Concurrency in C# Cookbook: Asynchronous, Parallel, and Multithreaded Programming by Stephen Cleary
    One of the best to ever do it - writing and explaining async-await in C#
  5. CLR via C# by Jeffrey Richter
@gurustron
gurustron / Program.cs
Created December 30, 2022 15:40
MakeGenericMethod
using System.Data;
using System.Reflection;
var methodInfo = typeof(Program)
.GetMethod(nameof(Program.GetList), BindingFlags.Public | BindingFlags.Static);
Type t = typeof(object); // your type
var method = methodInfo.MakeGenericMethod(t);
var invoke = method.Invoke(null, new []{ new DataTable()});
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddHttpClient<IRequestHandler<DummyRequest, string>, DummyHandler>((client) =>
{
@gurustron
gurustron / KindOfGeneric.cs
Created July 7, 2022 13:45
Testing generic base entity builder.
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace EFCoreTest;
public class KindOfGeneric
{
public static async Task Do()
{
await using var ctx = new KindOfGenericContext();
public class SpaceBattlesDatabase: ISpaceBattlesDataBase
{
public override DbSet<Building> Buildings => Set<Building>();
}
public abstract class ISpaceBattlesDataBase: DbContext
{
public virtual IQueryable<Building> Buildings { get; }
}
@gurustron
gurustron / Benchmark.cs
Created July 2, 2022 10:29
Benchmarking single access TryUpdate method (updates value only if present)
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace Bench;
[SimpleJob]
public class BenchTryUpdateDict
{
private static string[] PresentKeys = Enumerable.Range(1, 10).Select(k => $"Present{k}").ToArray();