Stum­bled upon an inter­est­ing ques­tion on Stack­Over­flow today regard­ing what goes on under the hood when you use the switch state­ment in C#.

Based on the answer by Brian Gideon (kudos for doing all that leg­work!), for the fol­low­ing code:

public static int Main(string[] args)
{
    switch (args[0])
    {
        case "1": return 1;
        case "2": return 2;
        case "3": return 3;
    }
    return 0;
}

the C# com­piler will:

  • if the num­ber of case state­ments is small then emit a sequen­tial equal­ity comparison;
  • if the num­ber of case state­ments is large then emit a Dic­tio­nary lookup.

Also, on a related ques­tion regard­ing if-else vs. switch state­ment, the updated answer also seems to con­firm this find­ing (regard­ing the use of a string in the switch state­ment at least) and Jon Skeet’s com­ment to the answer by ‘ima’ sug­gests that the thresh­old is 6 before the com­piler decides to build a hash table, i.e. case state­ment count >= 7 then build dictionary.

Share

imageYet another major new fea­ture Super­Fun­Town, you can now rotate your entire town!

To rotate your town left or right, sim­ply click on one of the two new but­tons (see left), but remem­ber, you can only rotate your own town!

Strug­gling with all the tow­er­ing build­ings and would like to see those nice lit­tle dec­o­ra­tion work you’ve done a while back? Well, rotate your town and bring them back into the view! Here’s how my town looks in all 4 angles :-)

image

image

image

image

Share

= true!

And here’s the proof.

Share

We’ve all been there before, write a sim­ple ser­vice with a sim­ple method:

[ServiceContract]
public interface IService
{
    [OperationContract]
    int SimpleMethod(object param1);
}

As time goes by, the sim­ple method gets more com­pli­cated, and the list of para­me­ters grows and even­tu­ally sim­ple method is over­loaded to pro­vide more vari­ety and sim­ple method is sim­ple no more!

A sim­ple solu­tion to this is the Request-Response pat­tern, by encap­su­lat­ing all the input and out­put val­ues into request and response objects you will be able to:

  • solve the prob­lem with grow­ing parameters
  • have an easy way of pro­vid­ing mul­ti­ple results
  • add input/output val­ues incrementally

And you’ll be able to do all this with­out even chang­ing the ser­vice contract!

[ServiceContract]
public interface IService
{
    [OperationContract]
    SimpleMethodResponse SimpleMethod(SimpleMethodRequest request);
}

[DataContract]
public void SimpleMethodRequest
{
    [DataMember]
    public object Param1 { get; set; }

    [DataMember]
    public string Param2 { get; set; }

    [DataMember]
    public int Param3 { get; set; }

    …
}

[DataContract]
public void SimpleMethodResponse
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public int? ErrorCode { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }

    …
}

In addi­tion, you can also cre­ate a hier­ar­chy of request/response objects and con­sol­i­date your val­i­da­tion logic in val­ida­tor classes or cus­tom val­i­da­tion attrac­tions (you can use Post­Sharp to write attrib­utes that take care of the val­i­da­tion ‘aspect’ of your application).

Ref­er­ences:

API Design Pat­terns – Request/Response

Share

By now, Win­dows 7 is almost old news and most of you would have been using or at least seen Win­dows 7 and one of the first things that should have caught your atten­tion is the redesigned task bar.

The new task bar are visu­ally much more pleas­ant and func­tion­ally it has also intro­duced a num­ber of rather use­ful new fea­tures such as the ‘jump list’ and the abil­ity to dis­play the progress bar on the task bar icon itself! In addi­tion, mul­ti­ple run­ning instances of the same appli­ca­tion is grouped together and when you hover over the icon you get a pre­view of each instance (see below).

image

The task bar works out which win­dows to group together by using the Appli­ca­tion User Model IDs (referred to as AppIDs from here on). In most cases you can rely on the AppID assigned to a process by the sys­tem. How­ever, an appli­ca­tion that owns mul­ti­ple processes or an appli­ca­tion that is run­ning in a host process might need to explic­itly iden­tify itself so that it can group its oth­er­wise dis­parate win­dows under a sin­gle taskbar but­ton and con­trol the con­tents of that application’s jump list.

Sup­port for the new task bar fea­tures is not included in the core .Net frame­work, you need to down­load the lat­est Win­dows API Code Pack (see Ref­er­ences sec­tion), build the solu­tion and ref­er­ence these two dlls in your project:

Microsoft.WindowsAPICodePack.dll

Microsoft.WindowsAPICodePack.Shell.dll

It takes only one line of code to set the AppID of a new win­dow, how­ever, it pays to write safe code that won’t break out­side Win­dows 7 so your code will usu­ally look more like this:

// Check whether the taskbar feature is supported (Win 7 or Win Server 2008 R2)
if (TaskbarManager.IsPlatformSupported)
{
    // Set the AppID for the current process TaskbarManager.Instance represents an 
    // instance of the Windows Taskbar
    TaskbarManager.Instance.ApplicationId = "NewApplicationID";
}

Ref­er­ences:

Win­dows­API Code Pack

Windows7 Train­ing Kit for Developers

Share