Skip to content

Instantly share code, notes, and snippets.

@joncham
joncham / Program.cs
Created February 28, 2023 19:21
Writing Value to Managed Object at an Offset
using System.Runtime.CompilerServices;
class Program
{
static void Main(string[] args)
{
TestSetIntAtOffset();
}
public static void TestSetIntAtOffset()
@joncham
joncham / NullableExample.cs
Created July 21, 2020 15:57
Nullable Example
using System;
public class C {
public void TakeStruct<T>(T t) where T: struct
{
}
public void Test()
{
var someStruct = new SomeStruct();
SomeStruct? someStructNullable = new SomeStruct();
@joncham
joncham / Test.cs
Last active April 24, 2019 20:27
Why do these warnings and errors happen?
// case 1
public struct A : System.IDisposable
{
public void Test()
{
var a = new A();
using (a)
a[0] = 0; // warning CS0728: Possibly incorrect assignment to local 'a' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
}
@joncham
joncham / Program.cs
Created February 19, 2019 15:24
C# Pop Quiz
using System;
class Program
{
enum MyEnum { Zero }
static void Main(string[] args) {
Print(0.0f);
Print(0.1f);
}
static void Print(object a) {
000002600F1020C0 push rbp
000002600F1020C1 mov rbp,rsp
000002600F1020C4 sub rsp,110h
000002600F1020CB mov qword ptr [rbp-78h],rsp
000002600F1020CF mov qword ptr [rbp-80h],rbp
000002600F1020D3 mov qword ptr [rbp-38h],rbx
000002600F1020D7 mov qword ptr [rbp-30h],rsi
000002600F1020DB mov qword ptr [rbp-28h],rdi
000002600F1020DF mov qword ptr [rbp-20h],r12
000002600F1020E3 mov qword ptr [rbp-18h],r13
@joncham
joncham / Program.cs
Last active July 25, 2017 22:04
Delegate GetHashCode Performance
//#define USE_CUSTOM_COMPARER
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class Program
{
private const int Iterations = 8000;
public static void Main (string[] args)
{
@joncham
joncham / gist:dee87d5ce78797cc3df8
Created February 6, 2015 15:11
Double Check Locking Style
if (!value)
{
AutoLock lock (&theLock);
if (!value)
{
//...
value = ///
}
}
@joncham
joncham / gist:1900ab969cc34def30bb
Last active August 29, 2015 14:14
Accidental usage of IEnumerable.Contains rather than HashSet<T>.Contains
using System;
using System.Collections.Generic;
using System.Linq;
namespace GenericConsoleTest
{
class Base {}
class Derived : Base {}
using System;
using System.Collections.Generic;
using System.Linq;
class Experiment
{
//ErrorsFor takes a hayStack, a bunch of needles, and is supposed to invoke errorMessage generator for everything
//in the haystack that matches a certain needle. As an excersice, i've been trying out all the different ways
//to write it, and evaluate for clarity, conciceness, readability by others, readability by me.
//
@joncham
joncham / gist:d3bf68b5be05a20114e6
Created October 24, 2014 15:41
Floating Point Awesomeness
Console.WriteLine (Math.Round (1.5));
Console.WriteLine (Math.Round (2.0));
Console.WriteLine (Math.Round (2.5));
Console.WriteLine();
Console.WriteLine (Math.Round (2.5));
Console.WriteLine (Math.Round (3.0));
Console.WriteLine (Math.Round (3.5));
// Output
// 2