Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Usage rules

Feedback

In this article

Usage rules support proper usage of .NET.

In this section

RuleDescription
CA1801: Review unused parametersA method signature includes a parameter that is not used in the method body.
CA1816: Call GC.SuppressFinalize correctlyA method that is an implementation of Dispose does not callGC.SuppressFinalize; or a method that is not an implementation ofDispose callsGC.SuppressFinalize; or a method callsGC.SuppressFinalize and passes something other thanthis (Me in Visual Basic).
CA2200: Rethrow to preserve stack detailsAn 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 typesThis makes the original error hard to detect and debug.
CA2207: Initialize value type static fields inlineA 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 correctlyA 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 visibleStatic 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 disposedA type that implementsSystem.IDisposable declares fields that are of types that also implementIDisposable. TheDispose method of the field is not called by theDispose method of the declaring type.
CA2214: Do not call overridable methods in constructorsWhen 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 disposeIf a type inherits from a disposable type, it must call theDispose method of the base type from its ownDispose method.
CA2216: Disposable types should declare finalizerA type that implementsSystem.IDisposable, and has fields that suggest the use of unmanaged resources, does not implement a finalizer as described byObject.Finalize.
CA2217: Do not mark enums with FlagsAttributeAn externally visible enumeration is marked withFlagsAttribute, 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 EqualsA public type overridesSystem.Object.Equals but does not overrideSystem.Object.GetHashCode.
CA2219: Do not raise exceptions in exception clausesWhen 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 equalsA public type implements the equality operator but doesn't overrideSystem.Object.Equals.
CA2225: Operator overloads have named alternatesAn 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 overloadsA type implements the equality or inequality operator, and does not implement the opposite operator.
CA2227: Collection properties should be read onlyA 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 constructorsTo 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.EqualsA value type overridesObject.Equals but does not implement the equality operator.
CA2234: Pass System.Uri objects instead of stringsA 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 aSystem.Uri parameter.
CA2235: Mark all non-serializable fieldsAn instance field of a type that is not serializable is declared in a type that is serializable.
CA2237: Mark ISerializable types with SerializableAttributeTo 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 theISerializable interface.
CA2241: Provide correct arguments to formatting methodsThe format argument passed toString.Format does not contain a format item that corresponds to each object argument, or vice versa.
CA2242: Test for NaN correctlyThis expression tests a value againstSingle.Nan orDouble.Nan. UseSingle.IsNan(Single) orDouble.IsNan(Double) to test the value.
CA2243: Attribute string literals should parse correctlyAn attribute's string literal parameter does not parse correctly for a URL, a GUID, or a version.
CA2244: Do not duplicate indexed element initializationsAn 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 itselfA property was accidentally assigned to itself.
CA2246: Do not assign a symbol and its member in the same statementAssigning 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 enumTaskCompletionSource 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 theHasFlag method call is different from the calling enum type.
CA2249: Consider using String.Contains instead of String.IndexOfCalls tostring.IndexOf where the result is used to check for the presence or absence of a substring can be replaced bystring.Contains.
CA2250: UseThrowIfCancellationRequestedThrowIfCancellationRequested automatically checks whether the token has been canceled, and throws anOperationCanceledException if it has.
CA2251: UseString.Equals overString.CompareIt is both clearer and likely faster to useString.Equals instead of comparing the result ofString.Compare to zero.
CA2252: Opt in to preview featuresOpt in to preview features before using preview APIs.
CA2253: Named placeholders should not be numeric valuesNamed placeholders in the logging message template should not be comprised of only numeric characters.
CA2254: Template should be a static expressionThe logging message template should not vary between calls.
CA2255: TheModuleInitializer attribute should not be used in librariesModule 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 interfaceTypes attributed withDynamicInterfaceCastableImplementationAttribute act as an interface implementation for a type that implements theIDynamicInterfaceCastable type. As a result, it must provide an implementation of all of the members defined in the inherited interfaces, because the type that implementsIDynamicInterfaceCastable will not provide them otherwise.
CA2257: Members defined on an interface with 'DynamicInterfaceCastableImplementationAttribute' should be 'static'Since a type that implementsIDynamicInterfaceCastable 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 membersstatic to avoid run-time errors.
CA2258: Providing a 'DynamicInterfaceCastableImplementation' interface in Visual Basic is unsupportedProviding a functionalDynamicInterfaceCastableImplementationAttribute-attributed interface requires the Default Interface Members feature, which is unsupported in Visual Basic.
CA2259: EnsureThreadStatic is only used with static fieldsThreadStaticAttribute only affectsstatic (Shared in Visual Basic) fields. When applied to instance fields, the attribute has no impact on behavior.
CA2260: Implement generic math interfaces correctlyGeneric math interfaces require the derived type itself to be used for the self-recurring type parameter.
CA2261: Do not useConfigureAwaitOptions.SuppressThrowing withTask<TResult>TheConfigureAwaitOptions.SuppressThrowing option isn't supported by the genericTask<TResult>, since that might lead to returning an invalidTResult.
CA2262: SetMaxResponseHeadersLength properlyMake sure theMaxResponseHeadersLength 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 compareSpan<T> tonull ordefaultComparing a span tonull ordefault might not do what you intended.default and thenull literal are implicitly converted toSpan<T>.Empty.
CA2263: Prefer generic overload when type is knownUsing a generic overload is preferable to passing aSystem.Type argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?