Quiz #3

8

Posted on : 27-02-2008 | By : matteosp | In : C#, Quiz

Take the following snippet:

public class Foo
{
  // add code
  public static int MethodOne() { return 0; }
  public static string MethodTwo() { return string.Empty; }
}

public class Program
{
  public static void Main()
  {
    try { Foo.MethodOne(); }
    catch (Exception ex) { Console.WriteLine(ex.Message); }

    try { Foo.MethodTwo(); }
    catch (Exception ex) { Console.WriteLine(ex.Message); }
  }
}

and add the code necessary (you cannot modify in any way MethodOne and MethodTwo) to make both method invocation throw an exception.

Quiz #2

2

Posted on : 11-04-2007 | By : matteosp | In : C#, Quiz

Look at the following snippet:

public class Foo
{
    protected Foo() {}
}

public class Var : Foo
{
    Var(): base() {}

    void Main()
    {
        Foo foo = new Foo();
    }
}

Now tell me if it can be successfully compiled:

  1. both in 1.1 and 2.0
  2. only in 1.1
  3. only in 2.0
  4. neither in 1.1 nor in 2.0

Quiz #1

0

Posted on : 15-03-2007 | By : matteosp | In : .Net Programming, C#, Quiz

Modify this apparently thread-safe event raiser in such a way it becomes truly thread-safe without changing signature nor implementation.

protected void OnMyEvent(MyEventArg e)
{
   MyEventHandler handler = MyEvent;

   if (handler != null)
      handler (this, e);
}

Inspired by this post by Pierre Greborio.

Quiz Sharp #0

1

Posted on : 09-03-2007 | By : matteosp | In : .Net Programming, C#, Quiz

This is my first quiz, and is the result of a beatiful and long chat with my good friend Adrian Florea. Here the Italian version on his blog.

Look at this snippet:

struct Foo
{
	public static implicit operator bool(Foo value)
	{
		return value != null;
	}
}

static void Main(string[] args)
{
	if (new Foo())
	{
		Console.WriteLine("Hello word!");
	}
}

What will you get? And, first of all, why?

a) nothing

b) “Hello word!”

c) a compiler error

d) a runtime error