Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
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(){}}