Diagnostic ID: CA1051: `Do not declare visible instance fields
Declaring properties in struct is not good due to defensive copy problem.
Therefore it is common to declare public fields.
[StructLayout(LayoutKind.Sequential)]
struct SomeStruct
{
public int a;
public int b;
}
There should be separate rule for class and for struct.
There are also several kind of structs:
I don't know if it makes sense to _ALWAYS_ suppress this rule for structs. IMO we should not report for class and struct if they are marked with StructLayoutAttribute as the description is pretty clear that you _HAVE TO_ use fields (not possible to use properties even the auto ones).
Good idea.
@Evangelink I think we can do couple of things:
(not possible to use properties even the auto ones).
Untrue. You can use the field attribute specifier to set field offsets on auto-properties.
@Joe4evr You are right for the Explicit case but as far as I have seen that's not right for Sequential where there is no guarantee for the order of the auto-generated fields.
Most helpful comment
I don't know if it makes sense to _ALWAYS_ suppress this rule for
structs. IMO we should not report forclassandstructif they are marked withStructLayoutAttributeas the description is pretty clear that you _HAVE TO_ use fields (not possible to use properties even the auto ones).