Out with one pop quiz, in with another…

In a recent post I posed the challenge of coming up with a function—VariableEquals, I called it—that would return true if the two parameters passed in were represented by the same variable, not just the same object (or value).

It wasn’t exactly a fair challenge, for two reasons:

  1. The code I posted illustrating an example usage of this hypothetical function was in VB.NET; in reality, it would impossible to implement said function directly in VB.NET. (To be fair, I did explicitly point this out in the hints I provided with the original challenge.)
  2. It is only possible to implement such a method for some types of input; in other words, a generic VariableEquals<T> method is not possible*.

Now, that said, here’s how you could concoct such a function in C#:

unsafe static bool VariableEquals(ref int x, ref int y)
{
    fixed (int* xptr = &x, yptr = &y)
    {
        return xptr == yptr;
    }
}

I know, pretty sneaky, right? In particular this was a tricky one since in VB.NET, there is no syntactical difference between calling a method that takes parameters ByRef and calling one that takes them ByVal (there is a difference in defining such methods, but not in calling them). So I was definitely withholding a crucial piece of information from my original challenge. (But I still think it would’ve been possible for someone to get it, if he or she thought really hard about it.)

Anyway, the above method does indeed distinguish between distinct variables, even if those variables contain the same value or references to the same object. Try it yourself and see!

int x = 5;
int y = x;
Console.WriteLine(VariableEquals(ref x, ref y));
Console.WriteLine(VariableEquals(ref x, ref x));

Output:

False
True

Now here’s another pop quiz for you. This one I’m stealing from a Stack Overflow question; I don’t want to link to the question yet, though, because I don’t want you to see anyone else’s answer (yet—I’ll link to the question when I post an answer later!).

Can you define a function in C# that takes a different function as input, and returns an instance of a delegate that will execute the input function and return an instance of itself?

That description might be a little tough to comprehend, so here’s a code example illustrating how it could work (if it’s possible, of course)!

var weirdFunction = CreateWeirdFunction(s => Console.WriteLine(s));
weirdFunction("What")("is")("going")("on")("here?");

Desired output:

What
is
going
on
here?

I really like this one 😉

*That is, as far as I know it isn’t possible! If you can think of a way to accomplish that, I will be quite impressed!

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: