Skip to content

Instantly share code, notes, and snippets.

Avatar

Gérald Barré meziantou

View GitHub Profile
@meziantou
meziantou / Program.cs
Last active Sep 12, 2020
Audit table with Entity Framework Core
View Program.cs
// https://www.meziantou.net/entity-framework-core-history-audit-table.htm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Newtonsoft.Json;
@meziantou
meziantou / benchmark.cs
Created Feb 11, 2020
Struct - HashSet benchmark
View benchmark.cs
public class Hashset
{
[Benchmark]
public void HashSet_Int32()
{
var hashset = new HashSet<int>();
for (int i = 0; i < 1000; i++)
{
hashset.Add(i);
}
@meziantou
meziantou / Range.cs
Last active Sep 21, 2020
C# 8: Index and Range
View Range.cs
// https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Index.cs
// https://github.com/dotnet/corefx/blob/1597b894a2e9cac668ce6e484506eca778a85197/src/Common/src/CoreLib/System/Range.cs
using System.Runtime.CompilerServices;
namespace System
{
/// <summary>Represent a type can be used to index a collection either from the start or the end.</summary>
/// <remarks>
/// Index is used by the C# compiler to support the new index syntax
View NullableAttributes.cs
#pragma warning disable MA0048 // File name must match type name
#define INTERNAL_NULLABLE_ATTRIBUTES
#if NETSTANDARD2_0 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NET45 || NET451 || NET452 || NET6 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
// https://github.com/dotnet/corefx/blob/48363ac826ccf66fbe31a5dcb1dc2aab9a7dd768/src/Common/src/CoreLib/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
@meziantou
meziantou / program.cs
Created Mar 31, 2019
Collection performance
View program.cs
internal static class Program
{
private static void Main()
{
BenchmarkRunner.Run<Remove>();
}
}
[CoreJob]
[MemoryDiagnoser]
View benchmark.cs
[Benchmark]
public string V1()
{
return string.Create(13, _id, (buffer, id) =>
{
var encode32CharsArray = _encode32CharsArray;
buffer[0] = encode32CharsArray[(id >> 60) & 31];
buffer[1] = encode32CharsArray[(id >> 55) & 31];
buffer[2] = encode32CharsArray[(id >> 50) & 31];
View Results Bounds check assembly.txt
.NET Core 2.2.0 (CoreCLR 4.6.27110.04, CoreFX 4.6.27110.04), 64bit RyuJIT
BranchmarkString.Benchmark.V6()
return string.Create(13, _id, (buffer, id) =>
^^^
{
^^^
var encode32CharsArray = _encode32CharsArray;
^^^
buffer[12] = encode32CharsArray[id & 31];
^^^
View program.cs
static void Main(string[] args)
{
var xsl = XDocument.Parse(@"<xsl:stylesheet version=""1.0""
xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""
xmlns:msxsl=""urn:schemas-microsoft-com:xslt""
xmlns:user=""http://dummy/ns"">
<msxsl:script language=""C#"" implements-prefix=""user"">
<![CDATA[
public string Code()
{
View main.cs
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
HandleDataObject(e.DataObject);
e.Handled = true;
}
private void HandleDataObject(IDataObject data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
View RemoteDataCache.cs
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public abstract class RemoteDataCache<T> : IDisposable
{
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
private CacheValue<T> _value;
You can’t perform that action at this time.