Skip to content

Instantly share code, notes, and snippets.

View nguerrera's full-sized avatar
🌴
I no longer work at Microsoft

Nick Guerrera nguerrera

🌴
I no longer work at Microsoft
View GitHub Profile
@nguerrera
nguerrera / PartialPropertyOverride.cs
Created March 11, 2016 22:28
Partial property override
class X { }
class Foo {
public virtual X Y { get; set; }
public virtual X Z { get; set; }
}
class Bar : Foo {
public override X Y { set { } }
public override X Z { get { return null; } }
@nguerrera
nguerrera / GetMethodBodyViaSRM.cs
Created December 5, 2015 18:59
GetMethodBodyViaSRM
/*
"dependencies": {
"System.Console": "4.0.0-beta-23516",
"System.IO.FileSystem": "4.0.1-beta-23516",
"System.Reflection": "4.1.0-beta-23516",
"System.Reflection.Metadata": "1.1.0",
"System.Reflection.TypeExtensions": "4.1.0-beta-23516"
}
*/
II.23.2.12 Type
Type ::=
BOOLEAN | CHAR | I1 | U1 | I2 | U2 | I4 | U4 | I8 | U8 | R4 | R8 | I | U
| ARRAY ModifiableType ArrayShape (general array, see §II.23.2.13)
| CLASS TypeDefOrRefOrSpecEncoded
| FNPTR MethodDefSig
| FNPTR MethodRefSig
| GENERICINST (CLASS | VALUETYPE) TypeDefOrRefOrSpecEncoded GenArgCount ModifiableType*
| MVAR number
@nguerrera
nguerrera / UnsafeWithoutKeyword.cs
Last active October 15, 2019 13:38
Absence of 'unsafe' C# keyword/switch does not guarantee type or memory safety.
/*
The absence of the C# unsafe keyword and switch does not guarantee code
is type or memory safe. It merely prevents the use of pointer types and
fixed size buffers.
The only thing that can guarantee type and memory safety is
verification and CAS/transparency enforcement.
In particular, the absence of unsafe C# blocks does NOT:

@davkean's thoughts on "contract breaking" exceptions:

You do not want to derive from InvalidOperationException. It and ArgumentException (and derivatives) are considered "contract breaking" exceptions.

Contract breaking exceptions should only be thrown to indicate that the caller has a bug. I pretend that every time I go to throw these exceptions that the process will be torn down immediately. If I’m okay with that, then I throw it, if not, then I should be throwing a

@nguerrera
nguerrera / ConfusableIdentifiers.cs
Last active August 29, 2015 14:05
This is valid and prints False then 42. (Hint: Unicode)
using System;
static class Program
{
static bool A()
{
return false;
}
static int А()
@nguerrera
nguerrera / DelegateToStaticWithFirstArg.cs
Last active August 29, 2015 14:05
Creating a delegate bound to a static method with first argument in verifiable IL
// And this program can be writen in C# after all if you use an extension method!
// (And the codegen is exactly as the optimal hand-written IL below.)
//
// Thanks to Vladimir Sadov on the compiler team for teaching me about it!
static class Program
{
static void Main()
{
var f = new Func<string>("World".StaticMethod);