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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:15:37+00:00 2026-05-24T16:15:37+00:00

In C# (and many other languages) it’s perfectly legitimate to access private fields of

  • 0

In C# (and many other languages) it’s perfectly legitimate to access private fields of other instances of the same type. For example:

public class Foo
{
    private bool aBool;

    public void DoBar(Foo anotherFoo)
    {
        if (anotherFoo.aBool) ...
    }
}

As the C# specification (sections 3.5.1, 3.5.2) states access to private fields is on a type, not an instance. I’ve been discussing this with a colleague and we’re trying to come up with a reason why it works like this (rather than restricting access to the same instance).

The best argument we could come up with is for equality checks where the class may want to access private fields to determine equality with another instance. Are there any other reasons? Or some golden reason that absolutely means it must work like this or something would be completely impossible?

  • 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-24T16:15:38+00:00Added an answer on May 24, 2026 at 4:15 pm

    I think one reason it works this way is because access modifiers work at compile time. As such, determining whether or not a given object is also the current object isn’t easy to do. For example, consider this code:

    public class Foo
    {
        private int bar;
    
        public void Baz(Foo other)
        {
            other.bar = 2;
        }
    
        public void Boo()
        {
            Baz(this);
        }
    }
    

    Can the compiler necessarily figure out that other is actually this? Not in all cases. One could argue that this just shouldn’t compile then, but that means we have a code path where a private instance member of the correct instance isn’t accessible, which I think is even worse.

    Only requiring type-level rather than object-level visibility ensures that the problem is tractable, as well as making a situation that seems like it should work actually work.

    EDIT: Danilel Hilgarth’s point that this reasoning is backwards does have merit. Language designers can create the language they want, and compiler writers must conform to it. That being said, language designers do have some incentive to make it easier for compiler writers to do their job. (Though in this case, it’s easy enough to argue that private members could then only be accessed via this (either implicitly or explicitly)).

    However, I believe that makes the issue more confusing than it needs to be. Most users (myself included) would find it unneccessarily limiting if the above code didn’t work: after all, that’s my data I’m trying to access! Why should I have to go through this?

    In short, I think I may have overstated the case for it being “difficult” for the compiler. What I really meant to get across is that above situation seems like one that the designers would like to have work.

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

Sidebar

Related Questions

In Flex (and many other languages) a function/method of a class can be declared
C++ has std::vector and Java has ArrayList , and many other languages have their
In C# , Ruby , and many other languages you can denote a string
I'm using boost::program_options and it suffers from the same as many other c++ libs,
C and many other languages have a conditional (AKA ternary) operator. This allows you
Stack Overflow has this question answered in many other languages, but not C. So
In C, like many other languages, there is the if statement. You can use
The question is simple, there are many API specific methods in other languages, but
I have accepted from many other languages that underscores have as much freedom as
I'm learning Objective-C and already know many other languages, but every time that i

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.