C# Puzzlers: The Joy of Lists

My quest to take a “deep dive” into C# has led me to this variation on the ShortSet Java Puzzler. I translated the ShortSet class, described in Java Puzzlers, to C#. C# does not have a Set class, so I used List.

public class ShortList{    public static void Main(string[] args)    {        IList<short> l = new List<short>();        for(short i=0; i<100; i++)        {            l.Add(i);            l.Remove(i - 1);        }        Console.WriteLine(l.Count);        Console.Write("Press any key to continue . . . ");        Console.ReadKey(true);    }}

What gets printed to the console?Well… this code does not even compile. List defines the Remove method as Remove(T value). The compiler correctly tells us that i - 1 is an int. We cannot remove an int from a List<short>.The Java compiler correctly allows this to compile, because Set’s remove method is defined with Object as a parameter. Remove is defined this way for backward compatibility.Does C# always behave as expected?My second attempt at a “deep dive” results in another boring/pointless example. Does C# not have puzzler material? Is it really that well designed?Where are the C# edge cases?


C# Puzzlers: Elvis Lives

I was a Java instructor earlier in my career. The best thing about being a Java instructor is you really learn Java. I knew Java as well or better than anybody while I was teaching. I quickly learned that students ask the best questions and they forced me to truly understand Java.A group of developers, at work, started exploring Java Puzzlers. Java Puzzlers reminded me about how well I knew Java and how I don’t have the same depth of knowledge in C#. I want/need the same depth of knowledge in C# as I do in Java.I am currently working my way through the Java puzzlers in C#. This post is the result of the first puzzler. This puzzler was written by Jeff Grigg.Given the following: What will be printed to the console?

public class Elvis : King{    public bool IsAlive = true;    protected override void Kill()    {        IsAlive = false;        Console.WriteLine("The king is dead.");    }    public static void Main(String[] args)    {        Elvis elvis = new Elvis();        if (elvis.IsAlive)            Console.WriteLine("Long live the king!");        Console.WriteLine("Hit enter to continue....");        Console.ReadLine();    }}public abstract class King{    public King()    {        Kill();    }    protected abstract void Kill();}

It turns out it only prints

The king is dead.

This went against my instinct. Why? The same puzzler prints

The king is dead.Long live the king!

in Java. Java does not execute public bool IsAlive = true; until after the super constructor executes.This obviously does not happen in C#. C# must initialize all member variables before executing the constructor. This behavior seems to be more obvious than the Java behavior. I guess I should try not to think so hard when I am working through these puzzles ;)The code for the puzzlers can be found in my svn repository.https://esterline.devguard.com/svn/code/csharp_puzzlers