TypeName

ColonsMustBeSpacedCorrectly

CheckId

SA1024

Category

Spacing Rules

Cause

A colon within a C# element is not spaced correctly.

Rule Description

A violation of this rule occurs when the spacing around a colon is not correct.

The spacing around a colon depends upon the type of colon and how it is used within the code. A colon appearing within an element declaration must always have a single space on either side, unless it is the first or last character on the line. For example all of the colons below follow this rule:

    public class Class2<T> : Class1 where T : MyType

    {

        public Class2(int x) : base(x)

        {

        }

    }

When the colon comes at the end of a label or case statement, it must always be followed by whitespace or be the last character on the line, but should never be preceded by whitespace. For example:

    _label:

    switch (x)

    {

        case 2:

            return x;

    }

Finally, when a colon is used within a conditional statement, it must always contain a single space on either side, unless the colon is the first or last character on the line. For example:

    int x = y ? 2 : 3;

How to Fix Violations

To fix a violation of this rule, ensure that the spacing around the colon follows the rule described above.