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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:18:47+00:00 2026-06-05T07:18:47+00:00

Suppose that I have class Foo and class FooFrobber { private Foo _foo; FooFrobber(Foo

  • 0

Suppose that I have class Foo and

class FooFrobber
{
   private Foo _foo;

   FooFrobber(Foo foo)
   {
       _foo = foo;
   }

   Frob()
   {
      _foo.FrobCount += 1;
   }
}

It seems like _foo is a good name for a private field, but what if I want to make it an internal or protected variable? The convention (http://weblogs.asp.net/lhunt/archive/2004/08/17/CSharpCodingStandardsv113.aspx via Style guide for c#?) seems to be to use a StudlyCaps with no m_ or trailing _, which means that I will declare protected Foo Foo. This seems to work, but it seems a little bit odd to be shadowing a class name. The style guide says that all fields should be private, but that seems a bit excessive (but maybe that’s my inner python speaking). At any rate, it seems like you would have the same problem if we wanted to wrap _foo in a property.

Should I always come up with a different name for the field, as in MyFoo? Is it ok to leave this as it is, as the compiler doesn’t seem to mind?

  • 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-06-05T07:18:48+00:00Added an answer on June 5, 2026 at 7:18 am

    It’s allowed and common practice to have fields/properties that share the same name as their type. First example that comes to mind from the FCL is DispatcherObject.Dispatcher, which returns an instance of type Dispatcher.

    However, I personally prefer to avoid declaring fields as protected, and use properties instead. If you want to avoid the coding involved with declaring a backing field, you may use an auto-implemented property:

    protected Foo Foo { get; set; }
    

    The advantage of using properties is that you can apply different access modifiers for the getter and the setter:

    protected Foo Foo { get; private set; }
    

    Edit: The advantage of using protected properties instead of protected fields is that they allow you to change their implementation – for example, to introduce value validation or change notification – without breaking external libraries that might access it.

    Suppose, for example, that you want to extend your class to implement INotifyPropertyChanged. If you were using a protected field, there would be no straightforward way of detecting when the value of the field is changed by a consuming assembly (unless you change the implementation of the external assembly as well). If you were using a protected property, you could simply alter its implementation without requiring any changes in consuming assemblies:

    private Foo foo;
    
    protected Foo Foo 
    { 
        get
        {
            return foo;
        }
        set
        {
            if (foo != value)
            {
                foo = value;
                OnPropertyChanged("Foo");
            }
        }
    }
    

    Edit2: Several more advantages to using properties over fields are given in LBushkin’s answer.

    Changing a field to a property does appear to break the ABI. I haven’t yet found it stated in an authoritative source (I didn’t spend too much time looking); however, per pst’s comment:

    The code behind the property can be changed (to use a custom private backing field or whatever). However, changing a Public Member Variable to a Property is a breaking change in the ABI (Application Binary Interface).

    Per jstedfast’s answer:

    First thing to keep in mind is that property accessors are compiled into methods. This means that it has a different ABI from just reading/writing to a class member variable, even though it may syntactically look the same.

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

Sidebar

Related Questions

Suppose I have class like this (simplified): class Foo_p; class Foo { private: Foo_p
Suppose I have a stupid little case class like so: case class Foo(name: String,
Suppose that I have following class template<unsigned char B, unsigned char F> class Foo
Suppose that I have a Java class with a static method, like so: class
Suppose I have the following class: public class Foo { private List<Integer> list =
Suppose I have the following class Foo, that supports a function of any arity
Suppose you have a class Foo with private member of type Bar. You don't
Suppose I have a class like below: public static class Foo { public static
Suppose I have a resource like below: namespace OpenRastaApp.Resources { public class Foo {
Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&

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.