Why this matters
Utility classes are not meant to be instantiated. Mark them as static or provide a private constructor to prevent unintended instantiation.
Utility classes are not meant to be instantiated. Mark them as static or provide a private constructor to prevent unintended instantiation.
Utility classes are not meant to be instantiated. Mark them as static or provide a private constructor to prevent unintended instantiation.
Side-by-side examples engineers can pattern-match during review.
public class StringUtils // Noncompliant: implicit public constructor
{
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}public static class StringUtils // Compliant: the class is static
{
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}
public class StringUtils // Compliant: the constructor is not public
{
private StringUtils()
{
}
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}public class StringUtils // Noncompliant: implicit public constructor
{
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}public static class StringUtils // Compliant: the class is static
{
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}
public class StringUtils // Compliant: the constructor is not public
{
private StringUtils()
{
}
public static string Concatenate(string s1, string s2)
{
return s1 + s2;
}
}From the same buckets as this rule.