Gotcha’s! (gesundheit)

Imagine a class, a simple object…  Add a protected static property to it.  Now imagine have multiple classes extending it, what role does the static property take on?  It is shared, right — meaning that both sub-classes can interact with the shared property since it is static, there is only one.

If you’re familiar with C#, terms like inheritance and static are commonplace. Without querying your favorite search engine or pasting the code into your favorite C# IDE — do you know the answer to this questions?

Consider the following:

    class Program
    {
        static void Main(string[] args)
        {
            var one = new ExtendsOne();
            one.Put(Guid.Empty, new { OMG = "WTF" });
            var value = one.Get(Guid.Empty);
            Console.WriteLine(value);

            var two = new ExtendsTwo();
            two.Put(2, "Seriously, interesting...");
            var @string = two.Get(2);
            Console.WriteLine(@string);

            Console.ReadKey();
        }

        abstract class Generic<TKey, TValue>
        {
            protected static readonly IDictionary<TKey, TValue> 
                Cache = new Dictionary<TKey, TValue>();

            public abstract TValue Get(TKey key);

            public abstract TValue Put(TKey key, TValue value);
        }

        sealed class ExtendsOne : Generic<Guid, object>
        {
            public override object Get(Guid key)
            { 
                return Cache[key];
            }

            public override object Put(Guid key, object value) 
            {
                return Cache[key] = value;
            }
        }

        sealed class ExtendsTwo : Generic<int, string>
        {
            public override string Get(int key)
            { 
                return Cache[key];
            }

            public override object Put(int key, string value) 
            {
                return Cache[key] = value;
            }
        }

Does this compile?

Yes, it does — good you’re with me so far!

Does this throw a runtime exception?

No, it does not… What I found interesting is that the static property Cache is created anew for each subclass of a different type. Which actually makes perfect sense, but it kind of makes you rethink static a little.

It is almost like having an instance of a static which is not normally what you expect when you think of statics. Typically, one would expect it to be shared — but with the power of generics I guess anything is possible.

Advertisements

Greetings!

Welcome to CSharper!

This is a place where you can come to sharpen your programming skills with the worlds most powerful programming language in existence today, C# (pronounced “c sharp”).

Coining the phrase CSharper “see sharper”, was simply a play on words — eluding to an individuals desire to extend their knowledge and solidify their ability to analyze, comprehend, and ultimately implement a programming solution to solve advanced problems.  Visit the Sharpening page for the latest covered topics…

Happy blogging!