Skip to content

Instantly share code, notes, and snippets.

View admir-live's full-sized avatar

Admir Mujkic admir-live

View GitHub Profile
@admir-live
admir-live / Example.cs
Created July 9, 2024 17:34
Accessing the last element
public class Program
{
public static void Main()
{
List<string> personList = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };
// Accessing the last element
string lastPerson = personList[^1];
Console.WriteLine($"Last person: {lastPerson}"); // Output: Eve
@admir-live
admir-live / 𝗦𝘂𝗺 𝗠𝗲𝘁𝗵𝗼𝗱
Created June 25, 2024 19:40
𝗦𝘂𝗺 𝗠𝗲𝘁𝗵𝗼𝗱
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BenchmarkExample
{
public class Program
{
@admir-live
admir-live / LinqQueryPerformanceBenchmark.cs
Created June 20, 2024 20:35
Avoid Using the "let" Keyword in LINQ Queries
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class LinqQueryPerformanceBenchmark
{
private List<Person> _peopleList;
public LinqQueryPerformanceBenchmark()
{
_peopleList = new List<Person>();
@admir-live
admir-live / LinqPerformanceBenchmark.cs
Created June 19, 2024 21:41
Getting the last value of a collection
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Order;
// Represents a person with an ID and a Name
public class Person
{
public int Id { get; set; }
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly DbContext _dbContext;
@admir-live
admir-live / QueryExamplesEfCore9.cs
Created May 29, 2024 03:47
EF Core 9 performance with inlined uncorrelated subqueries
namespace NewInEfCore9;
public static class QueryExamples
{
public static Task RunUserQuerySample()
{
return ExecuteUserQueryTest<UserManagementDbContext>();
}
private static async Task ExecuteUserQueryTest<TContext>()
@admir-live
admir-live / ObjectPoolBenchmarks.cs
Created May 21, 2024 18:21
Object Pool Benchmark Results
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
@admir-live
admir-live / ClassBenchmark.cs
Created May 20, 2024 13:05
Understanding Class Performance in C#
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ClassBenchmark
{
public class StandardClass
{
public int StandardProperty { get; set; }
public int GetStandardProperty() => StandardProperty;
@admir-live
admir-live / Number.cs
Created May 15, 2024 08:15
Number.cs
public interface INumber
{
object NumericValue { get; set; }
}
public class Number : INumber
{
public object NumericValue { get; set; }
}
@admir-live
admir-live / GenericHolderNull.cs
Created May 15, 2024 07:44
GenericHolderNull.cs
var intHolder = new Number<int?>(null); // T is int?, which can be null
var stringHolder = new Number<string>("Hello"); // T is string
var objectHolder = new Number<object>(new object()); // T is object
var nullableHolder = new Number<int?>(5); // T is int?, with a value
Console.WriteLine(intHolder.Value); // Output: null
Console.WriteLine(stringHolder.Value); // Output: Hello
Console.WriteLine(objectHolder.Value); // Output: System.Object
Console.WriteLine(nullableHolder.Value); // Output: 5