You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The latest version of this topic can be found at [CA1000: Do not declare static members on generic types](https://docs.microsoft.com/visualstudio/code-quality/ca1000-do-not-declare-static-members-on-generic-types).
28
-
29
-
TypeName|DoNotDeclareStaticMembersOnGenericTypes|
30
-
|CheckId|CA1000|
31
-
|Category|Microsoft.Design|
32
-
|Breaking Change|Breaking|
33
-
34
-
## Cause
35
-
An externally visible generic type contains a `static` (`Shared` in Visual Basic) member.
36
-
37
-
## Rule Description
38
-
When a `static` member of a generic type is called, the type argument must be specified for the type. When a generic instance member that does not support inference is called, the type argument must be specified for the member. The syntax for specifying the type argument in these two cases is different and easily confused, as the following calls demonstrate:
39
-
40
-
```vb
41
-
' Shared method in a generic type.
42
-
GenericType(OfInteger).SharedMethod()
43
-
44
-
' Generic instance method that does not support inference.
45
-
someObject.GenericMethod(OfInteger)()
46
-
```
47
-
48
-
```csharp
49
-
// Static method in a generic type.
50
-
GenericType<int>.StaticMethod();
51
-
52
-
// Generic instance method that does not support inference.
53
-
someObject.GenericMethod<int>();
54
-
```
55
-
56
-
Generally, both of the prior declarations should be avoided so that the type argument does not have to be specified when the member is called. This results in a syntax for calling members in generics that is no different from the syntax for non-generics. For more information, see [CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md).
57
-
58
-
## How to Fix Violations
59
-
To fix a violation of this rule, remove the static member or change it to an instance member.
60
-
61
-
## When to Suppress Warnings
62
-
Do not suppress a warning from this rule. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.
63
-
64
-
## Related Rules
65
-
[CA1005: Avoid excessive parameters on generic types](../code-quality/ca1005-avoid-excessive-parameters-on-generic-types.md)
66
-
67
-
[CA1010: Collections should implement generic interface](../code-quality/ca1010-collections-should-implement-generic-interface.md)
68
-
69
-
[CA1002: Do not expose generic lists](../code-quality/ca1002-do-not-expose-generic-lists.md)
70
-
71
-
[CA1006: Do not nest generic types in member signatures](../code-quality/ca1006-do-not-nest-generic-types-in-member-signatures.md)
72
-
73
-
[CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md)
74
-
75
-
[CA1003: Use generic event handler instances](../code-quality/ca1003-use-generic-event-handler-instances.md)
76
-
77
-
[CA1007: Use generics where appropriate](../code-quality/ca1007-use-generics-where-appropriate.md)
78
-
79
-
## See Also
27
+
The latest version of this topic can be found at [CA1000: Do not declare static members on generic types](https://docs.microsoft.com/visualstudio/code-quality/ca1000-do-not-declare-static-members-on-generic-types).
An externally visible generic type contains a `static` (`Shared` in Visual Basic) member.
36
+
37
+
## Rule Description
38
+
When a `static` member of a generic type is called, the type argument must be specified for the type. When a generic instance member that does not support inference is called, the type argument must be specified for the member. The syntax for specifying the type argument in these two cases is different and easily confused, as the following calls demonstrate:
39
+
40
+
```vb
41
+
' Shared method in a generic type.
42
+
GenericType(OfInteger).SharedMethod()
43
+
44
+
' Generic instance method that does not support inference.
45
+
someObject.GenericMethod(OfInteger)()
46
+
```
47
+
48
+
```csharp
49
+
// Static method in a generic type.
50
+
GenericType<int>.StaticMethod();
51
+
52
+
// Generic instance method that does not support inference.
53
+
someObject.GenericMethod<int>();
54
+
```
55
+
56
+
Generally, both of the prior declarations should be avoided so that the type argument does not have to be specified when the member is called. This results in a syntax for calling members in generics that is no different from the syntax for non-generics. For more information, see [CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md).
57
+
58
+
## How to Fix Violations
59
+
To fix a violation of this rule, remove the static member or change it to an instance member.
60
+
61
+
## When to Suppress Warnings
62
+
Do not suppress a warning from this rule. Providing generics in a syntax that is easy to understand and use reduces the time that is required to learn and increases the adoption rate of new libraries.
63
+
64
+
## Related Rules
65
+
[CA1005: Avoid excessive parameters on generic types](../code-quality/ca1005-avoid-excessive-parameters-on-generic-types.md)
66
+
67
+
[CA1010: Collections should implement generic interface](../code-quality/ca1010-collections-should-implement-generic-interface.md)
68
+
69
+
[CA1002: Do not expose generic lists](../code-quality/ca1002-do-not-expose-generic-lists.md)
70
+
71
+
[CA1006: Do not nest generic types in member signatures](../code-quality/ca1006-do-not-nest-generic-types-in-member-signatures.md)
72
+
73
+
[CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md)
74
+
75
+
[CA1003: Use generic event handler instances](../code-quality/ca1003-use-generic-event-handler-instances.md)
76
+
77
+
[CA1007: Use generics where appropriate](../code-quality/ca1007-use-generics-where-appropriate.md)
The latest version of this topic can be found at [CA1002: Do not expose generic lists](https://docs.microsoft.com/visualstudio/code-quality/ca1002-do-not-expose-generic-lists).
28
-
29
-
TypeName|DoNotExposeGenericLists|
30
-
|CheckId|CA1002|
31
-
|Category|Microsoft.Design|
32
-
|Breaking Change|Breaking|
33
-
34
-
## Cause
35
-
A type contains an externally visible member that is a <xref:System.Collections.Generic.List%601?displayProperty=fullName> type, returns a <xref:System.Collections.Generic.List%601?displayProperty=fullName> type, or whose signature includes a <xref:System.Collections.Generic.List%601?displayProperty=fullName> parameter.
36
-
37
-
## Rule Description
38
-
<xref:System.Collections.Generic.List%601?displayProperty=fullName> is a generic collection that is designed for performance and not inheritance. <xref:System.Collections.Generic.List%601?displayProperty=fullName> does not contain virtual members that make it easier to change the behavior of an inherited class. The following generic collections are designed for inheritance and should be exposed instead of <xref:System.Collections.Generic.List%601?displayProperty=fullName>.
To fix a violation of this rule, change the <xref:System.Collections.Generic.List%601?displayProperty=fullName> type to one of the generic collections that is designed for inheritance.
48
-
49
-
## When to Suppress Warnings
50
-
Do not suppress a warning from this rule unless the assembly that raises this warning is not meant to be a reusable library. For example, it would be safe to suppress this warning in a performance tuned application where a performance benefit was gained from the use of generic lists.
51
-
52
-
## Related Rules
53
-
[CA1005: Avoid excessive parameters on generic types](../code-quality/ca1005-avoid-excessive-parameters-on-generic-types.md)
54
-
55
-
[CA1010: Collections should implement generic interface](../code-quality/ca1010-collections-should-implement-generic-interface.md)
56
-
57
-
[CA1000: Do not declare static members on generic types](../code-quality/ca1000-do-not-declare-static-members-on-generic-types.md)
58
-
59
-
[CA1006: Do not nest generic types in member signatures](../code-quality/ca1006-do-not-nest-generic-types-in-member-signatures.md)
60
-
61
-
[CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md)
62
-
63
-
[CA1003: Use generic event handler instances](../code-quality/ca1003-use-generic-event-handler-instances.md)
64
-
65
-
[CA1007: Use generics where appropriate](../code-quality/ca1007-use-generics-where-appropriate.md)
66
-
67
-
## See Also
27
+
The latest version of this topic can be found at [CA1002: Do not expose generic lists](https://docs.microsoft.com/visualstudio/code-quality/ca1002-do-not-expose-generic-lists).
28
+
29
+
|TypeName|DoNotExposeGenericLists|
30
+
|CheckId|CA1002|
31
+
|Category|Microsoft.Design|
32
+
|Breaking Change|Breaking|
33
+
34
+
## Cause
35
+
A type contains an externally visible member that is a <xref:System.Collections.Generic.List%601?displayProperty=fullName> type, returns a <xref:System.Collections.Generic.List%601?displayProperty=fullName> type, or whose signature includes a <xref:System.Collections.Generic.List%601?displayProperty=fullName> parameter.
36
+
37
+
## Rule Description
38
+
<xref:System.Collections.Generic.List%601?displayProperty=fullName> is a generic collection that is designed for performance and not inheritance. <xref:System.Collections.Generic.List%601?displayProperty=fullName> does not contain virtual members that make it easier to change the behavior of an inherited class. The following generic collections are designed for inheritance and should be exposed instead of <xref:System.Collections.Generic.List%601?displayProperty=fullName>.
To fix a violation of this rule, change the <xref:System.Collections.Generic.List%601?displayProperty=fullName> type to one of the generic collections that is designed for inheritance.
48
+
49
+
## When to Suppress Warnings
50
+
Do not suppress a warning from this rule unless the assembly that raises this warning is not meant to be a reusable library. For example, it would be safe to suppress this warning in a performance tuned application where a performance benefit was gained from the use of generic lists.
51
+
52
+
## Related Rules
53
+
[CA1005: Avoid excessive parameters on generic types](../code-quality/ca1005-avoid-excessive-parameters-on-generic-types.md)
54
+
55
+
[CA1010: Collections should implement generic interface](../code-quality/ca1010-collections-should-implement-generic-interface.md)
56
+
57
+
[CA1000: Do not declare static members on generic types](../code-quality/ca1000-do-not-declare-static-members-on-generic-types.md)
58
+
59
+
[CA1006: Do not nest generic types in member signatures](../code-quality/ca1006-do-not-nest-generic-types-in-member-signatures.md)
60
+
61
+
[CA1004: Generic methods should provide type parameter](../code-quality/ca1004-generic-methods-should-provide-type-parameter.md)
62
+
63
+
[CA1003: Use generic event handler instances](../code-quality/ca1003-use-generic-event-handler-instances.md)
64
+
65
+
[CA1007: Use generics where appropriate](../code-quality/ca1007-use-generics-where-appropriate.md)
0 commit comments