แก้ไข

แชร์ผ่าน


Usage rules

Usage rules support proper usage of .NET.

In this section

Rule Description
CA1801: Review unused parameters A method signature includes a parameter that is not used in the method body.
CA1816: Call GC.SuppressFinalize correctly A method that is an implementation of Dispose does not call GC.SuppressFinalize; or a method that is not an implementation of Dispose calls GC.SuppressFinalize; or a method calls GC.SuppressFinalize and passes something other than this (Me in Visual Basic).
CA2200: Rethrow to preserve stack details An exception is rethrown and the exception is explicitly specified in the throw statement. If an exception is rethrown by specifying the exception in the throw statement, the list of method calls between the original method that threw the exception and the current method is lost.
CA2201: Do not raise reserved exception types This makes the original error hard to detect and debug.
CA2207: Initialize value type static fields inline A value type declares an explicit static constructor. To fix a violation of this rule, initialize all static data when it is declared and remove the static constructor.
CA2208: Instantiate argument exceptions correctly A call is made to the default (parameterless) constructor of an exception type that is or derives from ArgumentException, or an incorrect string argument is passed to a parameterized constructor of an exception type that is or derives from ArgumentException.
CA2211: Non-constant fields should not be visible Static fields that are not constants or read-only are not thread-safe. Access to such a field must be carefully controlled and requires advanced programming techniques for synchronizing access to the class object.
CA2213: Disposable fields should be disposed A type that implements System.IDisposable declares fields that are of types that also implement IDisposable. The Dispose method of the field is not called by the Dispose method of the declaring type.
CA2214: Do not call overridable methods in constructors When a constructor calls a virtual method, it is possible that the constructor for the instance that invokes the method has not executed.
CA2215: Dispose methods should call base class dispose If a type inherits from a disposable type, it must call the Dispose method of the base type from its own Dispose method.
CA2216: Disposable types should declare finalizer A type that implements System.IDisposable, and has fields that suggest the use of unmanaged resources, does not implement a finalizer as described by Object.Finalize.
CA2217: Do not mark enums with FlagsAttribute An externally visible enumeration is marked with FlagsAttribute, and it has one or more values that are not powers of two or a combination of the other defined values on the enumeration.
CA2218: Override GetHashCode on overriding Equals A public type overrides System.Object.Equals but does not override System.Object.GetHashCode.
CA2219: Do not raise exceptions in exception clauses When an exception is raised in a finally or fault clause, the new exception hides the active exception. When an exception is raised in a filter clause, the runtime silently catches the exception. This makes the original error hard to detect and debug.
CA2224: Override equals on overloading operator equals A public type implements the equality operator but doesn't override System.Object.Equals.
CA2225: Operator overloads have named alternates An operator overload was detected, and the expected named alternative method was not found. The named alternative member provides access to the same functionality as the operator, and is provided for developers who program in languages that do not support overloaded operators.
CA2226: Operators should have symmetrical overloads A type implements the equality or inequality operator, and does not implement the opposite operator.
CA2227: Collection properties should be read only A writable collection property allows a user to replace the collection with a different collection. A read-only property stops the collection from being replaced but still allows the individual members to be set.
CA2229: Implement serialization constructors To fix a violation of this rule, implement the serialization constructor. For a sealed class, make the constructor private; otherwise, make it protected.
CA2231: Overload operator equals on overriding ValueType.Equals A value type overrides Object.Equals but does not implement the equality operator.
CA2234: Pass System.Uri objects instead of strings A call is made to a method that has a string parameter whose name contains "uri", "URI", "urn", "URN", "url", or "URL". The declaring type of the method contains a corresponding method overload that has a System.Uri parameter.
CA2235: Mark all non-serializable fields An instance field of a type that is not serializable is declared in a type that is serializable.
CA2237: Mark ISerializable types with SerializableAttribute To be recognized by the common language runtime as serializable, types must be marked with the SerializableAttribute attribute even if the type uses a custom serialization routine through implementation of the ISerializable interface.
CA2241: Provide correct arguments to formatting methods The format argument passed to String.Format does not contain a format item that corresponds to each object argument, or vice versa.
CA2242: Test for NaN correctly This expression tests a value against Single.Nan or Double.Nan. Use Single.IsNan(Single) or Double.IsNan(Double) to test the value.
CA2243: Attribute string literals should parse correctly An attribute's string literal parameter does not parse correctly for a URL, a GUID, or a version.
CA2244: Do not duplicate indexed element initializations An object initializer has more than one indexed element initializer with the same constant index. All but the last initializer are redundant.
CA2245: Do not assign a property to itself A property was accidentally assigned to itself.
CA2246: Do not assign a symbol and its member in the same statement Assigning a symbol and its member, that is, a field or a property, in the same statement is not recommended. It is not clear if the member access was intended to use the symbol's old value prior to the assignment or the new value from the assignment in this statement.
CA2247: Argument passed to TaskCompletionSource constructor should be TaskCreationOptions enum instead of TaskContinuationOptions enum TaskCompletionSource has constructors that take TaskCreationOptions that control the underlying Task, and constructors that take object state that's stored in the task. Accidentally passing a TaskContinuationOptions instead of a TaskCreationOptions will result in the call treating the options as state.
CA2248: Provide correct 'enum' argument to 'Enum.HasFlag' The enum type passed as an argument to the HasFlag method call is different from the calling enum type.
CA2249: Consider using String.Contains instead of String.IndexOf Calls to string.IndexOf where the result is used to check for the presence or absence of a substring can be replaced by string.Contains.
CA2250: Use ThrowIfCancellationRequested ThrowIfCancellationRequested automatically checks whether the token has been canceled, and throws an OperationCanceledException if it has.
CA2251: Use String.Equals over String.Compare It is both clearer and likely faster to use String.Equals instead of comparing the result of String.Compare to zero.
CA2252: Opt in to preview features Opt in to preview features before using preview APIs.
CA2253: Named placeholders should not be numeric values Named placeholders in the logging message template should not be comprised of only numeric characters.
CA2254: Template should be a static expression The logging message template should not vary between calls.
CA2255: The ModuleInitializer attribute should not be used in libraries Module initializers are intended to be used by application code to ensure an application's components are initialized before the application code begins executing.
CA2256: All members declared in parent interfaces must have an implementation in a DynamicInterfaceCastableImplementation-attributed interface Types attributed with DynamicInterfaceCastableImplementationAttribute act as an interface implementation for a type that implements the IDynamicInterfaceCastable type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implements IDynamicInterfaceCastable will not provide them otherwise.
CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static' Since a type that implements IDynamicInterfaceCastable may not implement a dynamic interface in metadata, calls to an instance interface member that is not an explicit implementation defined on this type are likely to fail at run time. Mark new interface members static to avoid run-time errors.
CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupported Providing a functional DynamicInterfaceCastableImplementationAttribute-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic.
CA2259: Ensure ThreadStatic is only used with static fields ThreadStaticAttribute only affects static (Shared in Visual Basic) fields. When applied to instance fields, the attribute has no impact on behavior.
CA2260: Implement generic math interfaces correctly Generic math interfaces require the derived type itself to be used for the self-recurring type parameter.
CA2261: Do not use ConfigureAwaitOptions.SuppressThrowing with Task<TResult> The ConfigureAwaitOptions.SuppressThrowing option isn't supported by the generic Task<TResult>, since that might lead to returning an invalid TResult.
CA2262: Set MaxResponseHeadersLength properly Make sure the MaxResponseHeadersLength value is provided correctly. This value is measured in kilobytes.
CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull' 'ArgumentNullException.ThrowIfNull' throws when the passed argument is 'null'. Certain constructs like non-nullable structs, and 'nameof()' and 'new' expressions are known to never be null, so 'ArgumentNullException.ThrowIfNull' will never throw.
CA2265: Do not compare Span<T> to null or default Comparing a span to null or default might not do what you intended. default and the null literal are implicitly converted to Span<T>.Empty.
CA2263: Prefer generic overload when type is known Using a generic overload is preferable to passing a System.Type argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks.