Skip to content

Instantly share code, notes, and snippets.

@SergeyTeplyakov
SergeyTeplyakov / MemberAccessor.cs
Created February 6, 2024 00:25
MemberAccessor
namespace MemberAccessors
{
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
#nullable enable
/// <summary>
/// A helper class for accessing fields and properties of an object in a performant way.
/// </summary>
public class MemberAccessor<T>
@SergeyTeplyakov
SergeyTeplyakov / gist:054760dbb1af1b1ab8aa99d048e50293
Created December 9, 2020 18:10
Comments about 'The .NET World - C# Source Generator'
This is a comment about the following blog post: https://hamedfathi.me/the-dotnet-world-csharp-source-generator/
I just skimmed through the post because its quite long tbh, so this is not a review but rather an impression.
When I was blogging I was asking myself some questions:
- Who's my audience?
- What's the goal and the story of the blogpost?
- Is there any redundant infromation that I've added to the post that can be removoed without making the main point less clear? etc.
Even though I think soomeone will love the posts like the one we're talking about (and the content is great, btw), this is not the post I'll be reading carefully.
public class TypeTuple : IEquatable<TypeTuple>
{
public bool Equals(TypeTuple other)
{
if (ReferenceEquals(null, other)) return false;
return EqualityComparer<Type>.Default.Equals(this.Source, other.Source) &&
EqualityComparer<Type>.Default.Equals(this.Destination, other.Destination);
}
public override bool Equals(object obj)
// Infinite loop
var x = new { Items = new List<int> { 1, 2, 3 }.GetEnumerator() };
while (x.Items.MoveNext())
{
Console.WriteLine(x.Items.Current);
}
// Mutable struct used in all the following samples
struct Mutable
{
using System;
interface IBase {}
interface IFoo : IBase
{
int A {get;}
}
class Foo : IFoo
{
public int A {get;set;}
}
@SergeyTeplyakov
SergeyTeplyakov / gist:a4d4df67748d27497848
Last active August 29, 2015 14:06
Async Programming Guidelines
using System.Threading.Tasks;
using System;
class NamingConventions
{
public static async Task DoStuffAsync()
{
await Task.Delay();
}
public static Task DoAnotherStuffAsync()
@SergeyTeplyakov
SergeyTeplyakov / NastyCCBug
Last active August 29, 2015 14:04
Nasty Code Contract bug
class CornerCaseSample
{
private readonly Lazy<int> _lazy;
public CornerCaseSample(string s)
{
// Any issues with this code?
Contract.Requires(s != null);
_lazy = new Lazy<int>(() => s.Length);
}
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;
using FunctionalWeapon.Monads;
using NUnit.Framework;
namespace NoThrow.Tests
{
public struct NoThrow<T>
{
var list = new List<int>{42};
var lIter = list.GetEnumerator();
lIter.MoveNext();
list.Add(12);
Console.WriteLine(lIter.Current); // 1
Console.WriteLine(lIter.MoveNext()); // 2
// Тоже самое но с LinkedList
var linked = new LinkedList<int>();
@SergeyTeplyakov
SergeyTeplyakov / gist:8104967
Created December 23, 2013 21:25
TreeNode sample from the PPP book by Robert C. Martin
using System;
public class TreeMap
{
private TreeMapNode topNode = null;
public void Add(IComparable key, object value)
{
if (topNode == null)
topNode = new TreeMapNode(key, value);
else