Example 2 Align by string - cpmcgrath/codealignment GitHub Wiki

For this part we will be working with the following example

using System;
namespace AlignmentTest
{
    public static class StateExtensions
    {
        public static string GetCapital(this State instance)
        {
            switch (instance)
            {
                case State.Queensland: return "Brisbane";
                case State.NewSouthWales: return "Sydney";
                case State.Victoria: return "Melbourne";
                case State.WesternAustralia: return "Perth";
                case State.SouthAustralia: return "Adelaide";
                case State.Tasmania: return "Hobart";
                case State.NorthernTerritory: return "Darwin";
                case State.AustralianCapitalTerritory: return "Canberra";
                default: return "Unknown";
            }
        }
    }
}

This is a simple method which returns the capital of each Australian state. The format you see here some people might disagree with and would prefer it to look more like…

using System;
namespace AlignmentTest
{
    public static class StateExtensions
    {
        public static string GetCapital(this State instance)
        {
            switch (instance)
            {
                case State.Queensland:
                    return "Brisbane";
                case State.NewSouthWales:
                    return "Sydney";
                case State.Victoria:
                    return "Melbourne";
                case State.WesternAustralia:
                    return "Perth";
                case State.SouthAustralia:
                    return "Adelaide";
                case State.Tasmania:
                    return "Hobart";
                case State.NorthernTerritory:
                    return "Darwin";
                case State.AustralianCapitalTerritory:
                    return "Canberra";
                default:
                    return "Unknown";
            }
        }
    }
}

But there's not too much difference, the only real advantage is it's more consistent with code where you want more complicated logic in it.

But with code alignment, there are advantages in keeping it on a single line too.

We could select the lines and align by string (Shortcut: Ctrl Shift =) on the word return, and that does a pretty good job.

Before

Before

After

After

It's almost like we're looking at a spread sheet, we have two columns – on with States and the other with cities.

But instead of aligning by “return” I prefer to align by colon. Why? Well the colon is the separator between the key and logic in a switch statement. It just makes more sense.

So we get back to the original state, select the lines and align by string (Shortcut: Ctrl Shift =) on :

Best Alignment

Tip: After aligning by column you might want to add another space and do the alignment again. This just looks a bit neater. Tip 2: If there’s a particular string you align by often you can assign it to a shortcut. Read:___