C# Easter Egg?

How have I never heard of the TypedReference type in C# until today?!

Seriously, did you know this was possible?

static T AddIfIntegers<T>(T x, T y)
{
    if (typeof(T) == typeof(int))
    {
        int sum = __refvalue(__makeref(x), int) + __refvalue(__makeref(y), int);
        return __refvalue(__makeref(sum), T);
    }
    else
    {
        return default(T);
    }
}

The above code doesn’t box the arguments, by the way (apparently).

Seriously… what the heck.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: