C# brain teasers

Recently I’ve stumbled upon a few brain teasers on C# and it was fun trying to figure out the answers to the problems. So here are a few simple brain teasers from me.

Teaser 1

What will be displayed in the console?

void foo(int a, int b, int c)
{
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
}

.....

int i = 0;

foo(i++, i++, i++);

Teaser 2

Will that compile?

enum E { A, B, C, }

Teaser 3

How about that:

void foo(string s) {...}

void foo(StringBuilder sb) {...}

....

foo(null);

No answers provided. You want to know the answers, you have to try it yourself, no fun otherwise.

More teasers here and here. Got a brain teaser? Drop a comment.

1 Response to “C# brain teasers”


  1. 1 AlexanderMP September 5, 2011 at 2:01 pm
    public partial class MyClass
    {
        IEnumerable Power(int number, int exponent)
        {
            int counter = 0;
            int result = 1;
            while (counter++ < exponent)
            {
                result = result * number;
                Console.Write("x ");
                yield return result;
            }
        }
    }
    public partial class MyClass
    {
        public void foo()
        {
            foreach (int i in Power(2, 4))
            {
                Console.Write("{0}", i);
            }
        }
    }
    

    what will foo() display?


Leave a Reply