Category Archives:Development

An interesting property of C# inheritance rules

By

Today I was playing with the robot and was thinking about a few possible ways of solving a particular problem I was having so that the internal API is nice for developers to attach to.

I wondered if a method from a base class could be used as an implementation of an interface, even though the base class does not implement explicitly that interface (but the inheriting class does).

I will admit to not fully understanding the little details of C#’s inheritance system, but I and the people I asked about this problem couldn’t give a definitive answer without trying it out.

This is the simplest code I could write that explains the concept of what I’m doing. ChildClass extends BaseClass and inherits Interface, but does not implement the testMethod method defined in Interface (however BaseClass does). For ChildClass2, the method is overridden. The output is what you would expect – ChildClass.testMethod() writes “BaseClass”, and ChildClass2.testMethod() writes “ChildClass2″.

Note it does not matter in this case whether BaseClass is abstract or not, nor does it matter whether testMethod in BaseClass is declared virtual or not (but you can’t use ChildClass2 if it’s not virtual of course).

using System;

namespace InheritanceTest
{
public interface Interface
{
void testMethod();
}

abstract public class BaseClass
{
public virtual void testMethod()
{
Console.WriteLine("BaseClass");
}
}

public class ChildClass : BaseClass, Interface { }

public class ChildClass2 : BaseClass, Interface
{
public override void testMethod()
{
Console.WriteLine("ChildClass2");
}
}

class Program
{
static void Main(string[] args)
{
Interface cc = new ChildClass();
cc.testMethod();

Interface cc2 = new ChildClass2();
cc2.testMethod();
}
}
}

Suppress warning “Field is never assigned to, and will always have its default value null” when fields are populated by MEF

By

MEF is a great framework for doing all kinds of cool things in .NET. However one thing that gets me quite often is that fields in classes that are composed by MEF using the [Import] label often generate warnings like the following:

Field 'field name' is never assigned to, and will always have its default value null

This is a warning that is safe to ignore but looks bad on your automated builds when warnings come up. So it is easy to get rid of with the SuppressMessage decoration on the field, but it is nearly impossible to find the right documentation for it (Visual Studio helpfully does not include the identifying code in the error message when rendered in the UI, thanks guys). To suppress this particular message, use the decoration:

[SuppressMessage("Microsoft.Performance", "CA1823")]

I also discovered a wonderful resource on MSDN (after literally hours of searching) which describes all the warnings and their codes so you can use them in such suppressions later on. Here is the whole list of warning identifiers, and here is the specific one for CA1823.

The unused event one is equally hard to find:

[SuppressMessage("Microsoft.Performance", "CS0219")]

Hope this saves someone a few hours looking for the docs and the correct code.

Abusing PHP to your whim – private methods and properties

By

I have recently been writing a lot of code (and open-sourcing it – see here, here, here, here, here, here / here, here, here) and today came across a particular problem and a rather convenient solution provided by PHP’s visibility rules for methods and properties of classes.

Get the low-down on WebMatrix, for creating simple websites

By

The full release of WebMatrix is now available. It’s a great tool designed to be easy to use to build great websites. It comes with a load of built in project templates including DotNetNuke, Joomla, WordPress and more. You may have noticed this list includes PHP software – WebMatrix and the servers that support it can handle both .NET and PHP based websites, which is super cool.

If you’re new to WebMatrix, the video below will give you a solid introduction – well worth checking out.

Twiq: a quick-tweet applet for Windows

By

It is with great pleasure that I announce my latest open-source adventure in the form of Twiq.

Twiq is a little applet for Windows that allows you to quickly tweet a message you come up with and then get straight back to work.

Too many times I have forgotten something funny, useful or generally tweetable while I wait for a web browser and Twitter.com or a client to load. No longer! Twiq is always listening just a keypress away (Ctrl+Win+Space in fact) to get that tweet dispatched efficiently.

You can’t read tweets with Twiq, but then you’d get distracted from whatever you’re working on. The flow for creating a tweet with Twiq has been designed to be super-smooth. Ctrl+Win+Space brings up the program, and puts your cursor in the tweet box; 140 taps gets you a witty, thoughtful, or useful tweet; press Return and your tweet is dispatched; Ctrl+Win+Space gets rid of Twiq and you back to work.

By popular demand – a screenshot of Twiq in action:

Twiq is free and open-source. The code is on Github, and you can get the latest installer from there too.