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 6652115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:05:46+00:00 2026-05-26T01:05:46+00:00

I am developing a CLS-compliant types library and I have a class inside It,

  • 0

I am developing a CLS-compliant types library and I have a class inside It, which contains private, protected and public fields and properties. I use underscore symbol (_) as prefix for private or protected fields and little first letter to distinct their from properties with same names. It looks so:

class SomeClass
{
private int _age; //Here is OK

public int Age { get { return this._get; } }
}

But when I try to use protected fields I collide with a next problem:

class SomeClass
{
protected int _age; //Here is NOT CLS-compliant (because of _ can't be the first symbol of identifier)

public int Age { get { return this._get; } }
}

Then I tried to do in such way:

class SomeClass
{
protected int age; //Here is NOT CLS-compliant (because of age and Age differ only in one symbol)

public int Age { get { return this._get; } }
}

Please, tell me, what is right CLS-compliant notation or convention between developers for such cases? Have I to use prefixes in C-style like l_age?

  • 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-26T01:05:46+00:00Added an answer on May 26, 2026 at 1:05 am

    Here’s a more correct version, IMO:

    private int _age;
    public int Age {
        get { return this._age ; }
        protected set { this._age = value; }
    }
    

    or simply:

    public int Age { get; protected set; }
    

    If you properly encapsulate it, then it doesn’t matter what the field is called, as nothing outside that type can see it.


    In comments, the question of events is then raised, with the example:

    protected EventHandler<StateChangedEventArgs> _stateChanged;
    public event EventHandler<StateChangedEventArgs> StateChanged
    {
        add { lock (this.StateChanged) { this._stateChanged += value; } }
        remove { lock (this.StateChanged) { this._stateChanged -= value; } }
    }
    

    Here I again assert that there is no reason for this field to be protected; the event does not belong to the derived class. It has 2 sensible operations it can want to perform:

    1. invoke the event
    2. subscribe/unsubscribe to the event

    The former should be done via the On* pattern; the latter should just use the regular accessors (otherwise it violates the lock). Also, even if we assume that lock(this.StateChanged) is a typo (that would be a really, really bad thing to use as the lock object -it would not work at all), note that in C# 4.0 the compiler has a much more efficient lock strategy inbuilt (that uses Interlocked rather than Monitor) when you write a “field-like” event (i.e. no explicit add/remove). Consequently, the preferred approach here would be:

    public event EventHandler<StateChangedEventArgs> StateChanged;
    protected virtual void OnStateChanged(StateChangedEventArgs args) {
        var snapshot = StateChanged; // avoid thread-race condition
        if(snapshot != null) shapshot(this, args);
    }
    

    and… that’s it!

    • if the subclass wants to subscribe/unsubscribe (not ideal, but meh) it just uses StateChanged += and StateChanged -=
    • if the subclass wants to invoke the event, it calls OnStateChanged(...)
    • if the subclass wants to tweak the event logic, it adds an override to OnStateChanged

    no need for any non-private fields.

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

Sidebar

Related Questions

Developing ios app. I have an object class Product.h and .m respectively along with
I developing a plugin which reads from a .properties file for the database connection,
Developing a project of mine I realize I have a need for some level
When developing whether its Web or Desktop at which point should a developer switch
When developing a new web based application which version of html should you aim
Developing for iPhone, I have a collection of points that I need to make
Developing iphone application using makkit framework. I have got the map view integrated in
When developing iPhone apps with Xcode 3.2.1/Objective C, which unit test tools are recommended?
i developing an application in which i want to bind my own parameter with
It is safe to ignore this warning? Argument type 'uint' is not CLS-compliant I'm

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.