The highlight of WinDev day 1 was the Ask The Experts (ATE) session, where attendees and speakers were able to mingle over appetizers and drinks. The food was good (especially those yummy bacon wrapsJ), the drinks were flowing (I swear, I rarely touch the stuff J), and the .NET trivia game and code brain-teasers (hosted by yours truly and my partner in crime Stephen Toub J) were both very engaging and tons of fun.

 

Congratulations to all the attendees who answered correct trivia questions and won a .NET book of their choice and congratulations to the two lucky winners of the ViewSonic V37 Pocket PCs for solving two of the four .NET code brain-teasers!

 

Thanks to all those who contributed questions to the .NET trivia game and special thanks to Stephen Toub, Adi Oltean and Mike Woodring for supplying the code brain-teasers!

 

Brain-teaser #1

Replace the TBD comment with a single line of code that causes this program to display "Win-Dev!"  You cannot modify any of the existing code, or add code above or below the existing code.  You can only write one line of code in place of the TBD comment and you cannot use the Console class.

        class App {
            static void Main() {
                // TBD
            }
        }

        sealed class Internal {
            sealed class Private {
                static void PrintMessage( string s1, string s2 ) {
                    System.Console.WriteLine(s1 + "-" + s2);
                }
            }
        }

 

Brain-teaser #2

What does the following code print?  There's a long answer, and there's a short answer, both of which were accepted.


        class C
        { 
        const string s = @" 
        class C 
        {{
        const string s = @{1}{0}{1};
        static void Main()
        {{
        System.Console.WriteLine(s,s,'\u0022');
        }}
        }}";
        static void Main()
        {
        System.Console.WriteLine(s,s,'\u0022');
        }
        }

 

Brain-teaser #3

What does the following code display when run on Everett

What does the following code display when run on Whidbey?


        using System;
        using System.Runtime.InteropServices;
        public class Program
        {
            static void Main()
            {
                SomeType s;
                unsafe
                {
                    Console.WriteLine("{0} {1} {2} {3} {4}",
                        (byte*)&s.Field1 - (byte*)&s,
                        (byte*)&s.Field2 - (byte*)&s,
                        (byte*)&s.Field3 - (byte*)&s,
                        sizeof(SomeType),
                        Marshal.SizeOf(typeof(SomeType)));
                }
            }
        }

        struct SomeType
        {
            public char Field1;
            public int Field2;
            public char Field3;
        }

 

Brain-teaser #4

What does the following code display? 


        using System;
        using System.Threading;
        class Class1
        {
            static void Main()
            {
                Console.WriteLine("A");
                new Class2();
                GC.Collect();
                new Class1();
                GC.Collect();
                Console.WriteLine("B");
            }

            ~Class1()
            {
                Console.WriteLine("C");
                Class2.Are.Set();
                Console.WriteLine("D");
            }
        }

        class Class2
        {
            public static ManualResetEvent Are = new ManualResetEvent(false);
            ~Class2()
            {
                Console.WriteLine("E");
                Class2.Are.WaitOne();
                Console.WriteLine("F");
            }
        }

 

Check out Chris Brumme’s blog for more on finalization.

 

The answer to brain-teaser #1 is:

 

typeof(Internal).GetNestedType("Private", System.Reflection.BindingFlags.NonPublic).GetMethod("PrintMessage",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).Invoke(null, new object[]{"Win","Dev!"});

 

The answers to the other brain-teasers can be computed by simply cutting, pasting and compiling the code above.

 

Congratulations again to all our winners and thanks to all the attendees who participated!