Andrei Rinea

.NET Framework & SQL Server

Another C# riddle..

clock February 11, 2010 14: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?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


A little game of guessing...

clock October 14, 2009 11: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 :)

Currently rated 2.6 by 5 people

  • Currently 2.6/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


About me

A passionated .NET Developer working closely with Microsoft technologies. View my public site.

Page List

Sign in