Do not declare visible instance fields warning in sequential struct

The documentation of CA1051: Do not declare visible instance fields says:

Cause

An externally visible type has an externally visible instance field.

The key point for both type and field is external. Hence the fix (since this is supposed to be used only inside your application) is to make the struct (and the class that exposes it) internal:

[StructLayout(LayoutKind.Sequential)]
internal struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}    

internal static class NativeMethods
{
    // ...
}

Please note that CA1051 warning is not generated by the C# compiler, but Code Analysis, hence can be excluded or ignored from the CA rule set (although the documentation suggests to not suppress it).


You can suppress warnings in a file like this:

#pragma warning disable CA1051, CA1815

or disable it in csproj file for the whole project

<NoWarn>CA1051, CA1815</NoWarn>

EDIT If you want to fix the warning instead of suppress it, you should follow the warning message.

I never compare it though and definitely don't need to, I just want to fix the warning.

The warning will appear unless you add operators like that suggested by the message. The warning means that "it probably works for you now, but not the best practice". Overriding equal operators for structs improves the readability and performance, also structs are supposed to be immutable, public fields break the immutability and hide potential bugs.