Inherited Static Members

For my first blog post, lets start with a programming tidbit.  For folks who know C# / C++, what do you think will be the output when the Start() method is called?

80 % of the engineers I’ve shown this to answered with:
ParentClass
ChildClass
ParentClass

Instead what prints out will be:
ParentClass
ChildClass
ChildClass

When childObject gets instantiated it changes MyString for ParentClass and any other class deriving from it. One solution, to get the expected result, would be to remove the static keyword in the declaration of MyString. But if you create hundreds of instances of ParentClass or ChildClass, that’s hundreds of instances of strings that you’re creating. One way to get the desired result, would be to use properties. Like so:

One thought on “Inherited Static Members

Leave a Reply