Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:53:08+00:00 2026-05-24T21:53:08+00:00

I have a class like this one: public class Foo { public readonly int

  • 0

I have a class like this one:

public class Foo
{
    public readonly int A = 1;
    public readonly int B = 2;
}

When I run VS2010 built in Code Analysis tool, I get 2 identical warnings:
that ‘field ‘…’ is visible outside of its declaring type, change its accessibility to private and add a property, with the same accessibility as the field has currently, to provide access to it‘.

I want to suppress this warning for all fields in my class Foo, but I don’t want to mark every field with SuppressMessage attribute like this:

public class Foo
{
    [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
    public readonly int A = 1;
    [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
    public readonly int B = 2;
}

I want to mark all class members, using code like this:

[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
public class Foo
{
    public readonly int A = 1;
    public readonly int B = 2;
}

But this code doesn’t work, I still get a code analysis warning.
How can I do it correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:53:09+00:00Added an answer on May 24, 2026 at 9:53 pm

    There is no way to suppress more than 1 message at a time using SuppressMessageAttribute.

    As discussion can be found here, but the relevant part is:

    You are running into a common misunderstanding about SuppressMessage.

    Each time you put a SuppressMessage in a source file, you suppress
    exactly one problem (one “row” in the grid). Period.

    A SuppressMessage may be placed either “near” the violation or at the
    module-level. Module-level, assembly-level, and global suppression
    all mean the same thing. By placing at the module-level, you do not
    suppress multiple instances of the problem at once. You merely get to
    locate the SuppressMessage in a different place of the code. The main
    benefit is that you can, for example, collect all the suppressions
    related to the assembly in a single file (for example,
    GlobalSuppressions.cs).

    When you use a module-level SuppressMessage, you must specify the
    Target. The Target must match exactly what is reported in the GUI for
    a violation of the rule.

    There is no way to use SuppressMessage to suppress a rule for the
    entire scope of a class or the entire scope of a namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: public final class Foo { public native int
I have a class that contains a boolean field like this one: public class
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class like this public class foo { private void getThread() {
I have a object model a bit like this: public class Foo { public
Take this sample code: Class Foo ReadOnly name As String Public Sub New(name As
I have this class: public class TestClass { public TestClass(int? foo, string bar) {
So I have a class of overloaded methods like this: class Foo { public
Assume I have a class like this: public class Foo { public Bar RequiredProperty
I have a class like this: class Foo private shared default_ = DEFAULT public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.