This page is not yet available in Spanish. We are working on its translation. If you have any questions or feedback about our current translation project, feel free to reach out to us!
Metadata
ID:csharp-best-practices/static-class
Language: C#
Severity: Notice
Category: Best Practices
Description
This rule states that a class should be declared as static if it only contains static members. Declaring a class as static indicates that it cannot be instantiated or extended and that all its members are static. This provides a clear signal to other developers that this class is not meant to be used as an object.
What the rule detects
The rule detects if all members are static . If they are, the rule recommends to declare the class static.
How to fix the issue?
Declare the class static by adding the static keyword to its declaration.
Non-Compliant Code Examples
// all attributes are static, the class should be staticpublicclassFoo{privatestaticstring_f;publicstaticvoidBar(){}}publicclassFooBar{privatestaticreadonlyIList<MyObject>LargeCollection;FooBar(){stringjson=System.IO.File.ReadAllText(TestFixtureBase.ResolvePath("large.json"));LargeCollection=JsonConvert.DeserializeObject<IList<RootObject>>(json);}publicstaticvoidPlop(){}}