Andrei Rinea

.NET Framework & SQL Server

Another C# riddle..

clock February 11, 2010 23:40 by author Andrei Rinea

Just like a little game of guessing went, I want to present you another C# riddle Cool :

using System;

namespace AnotherRiddle
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var w1 = new Wee { bar = new Foo() };
            if (w1.bar is Bar)
                Console.WriteLine("A");
            if (w1.bar.GetType() == typeof(Bar))
                Console.WriteLine("B");
        }
    }

    internal class Bar { }

    internal class Foo : Bar { }

    internal class Wee
    {
        public Bar bar;
    }
}

 

1. What do you think the code snippet will print at the console? 

2. What are all the differences between the two if constructs?



A little game of guessing...

clock October 14, 2009 20:23 by author Andrei Rinea

Let's say I have this block of C# code that compiles 

if (someVariable == true)
{
    // do something
} 

  but in a haste to optimize the code I change it into :   

if (someVariable)
{
    // do something
}

and suddenly my code does not compile anymore.

WHAT TYPE COULD someVariable  HAVE?! Please comment on what you think about this small riddle :)