Skip to content

Instantly share code, notes, and snippets.

@jcdickinson
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcdickinson/9051956 to your computer and use it in GitHub Desktop.
Save jcdickinson/9051956 to your computer and use it in GitHub Desktop.
Accessing Structs Methods via Interfaces
.method private hidebysig static
bool Bar<(class [mscorlib]System.IEquatable`1<valuetype ConsoleApplication4.MyEquatableThing>) -T> (
!!T obj1,
!!T obj2
) cil managed
{
// Method begins at RVA 0x20a4
// Code size 22 (0x16)
.maxstack 2
.locals init (
[0] bool areEqual
)
IL_0000: ldarga.s obj1
IL_0002: ldarg.1
IL_0003: box !!T
IL_0008: constrained. !!T
IL_000e: callvirt instance bool [mscorlib]System.Object::Equals(object)
IL_0013: stloc.0
IL_0014: ldloc.0
IL_0015: ret
} // end of method Program::Bar
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
struct MyEquatableThing : IEquatable<MyEquatableThing>
{
public int IntField;
public bool Equals(MyEquatableThing other)
{
return IntField == other.IntField;
}
}
class Program
{
static void Main(string[] args)
{
var a = new MyEquatableThing() { IntField = 1 };
var b = new MyEquatableThing() { IntField = 2 };
Console.WriteLine(Bar(a, b));
}
static bool Bar<T>(T obj1, T obj2) where T : IEquatable<MyEquatableThing>
{
var areEqual = obj1.Equals(obj2);
return areEqual;
}
}
}
static bool Bar<T>(T obj1, T obj2) where T : IEquatable<T>
{
var areEqual = obj1.Equals(obj2);
return areEqual;
}
.method private hidebysig static
bool Bar<(class [mscorlib]System.IEquatable`1<!!T>) -T> (
!!T obj1,
!!T obj2
) cil managed
{
// Method begins at RVA 0x20a4
// Code size 17 (0x11)
.maxstack 2
.locals init (
[0] bool areEqual
)
IL_0000: ldarga.s obj1
IL_0002: ldarg.1
IL_0003: constrained. !!T
IL_0009: callvirt instance bool class [mscorlib]System.IEquatable`1<!!T>::Equals(!0)
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: ret
} // end of method Program::Bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment