| TypeName | SA1024ColonsMustBeSpacedCorrectly |
| CheckId | SA1024 |
| Category | Spacing Rules |
A colon within a C# element is not spaced correctly.
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 should 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 should 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;
}A colon that appears as part of a string interpolation formatting component should not have leading whitespace characters. For example:
var s = $"{x:N}";When a colon appears within a property pattern, including nested members referenced through extended property pattern syntax, it is treated like a named element separator. The colon should not be preceded by whitespace, and it should normally be followed by a single space.
if (item is { Outer.Inner: value })
{
}Finally, when a colon is used within a conditional statement, it should 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;To fix a violation of this rule, ensure that the spacing around the colon follows the rule described above.
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1024:ColonsMustBeSpacedCorrectly", Justification = "Reviewed.")]#pragma warning disable SA1024 // ColonsMustBeSpacedCorrectly
#pragma warning restore SA1024 // ColonsMustBeSpacedCorrectly