Accessing private variables

In certain scenarios private variables can be accessed externally

Home DailyDrop

Daily Knowledge Drop

Private variables of a class cannot be accessed from outside the class - except for in one specific case!
Private variables of one instance of a class, can be accessed from within another instance of the same class


Example

Consider the Person example below:

public class Person
{
    public DateOnly BirthDay { get; set; }

    private int Age;
    
    public Person() 
    { }

    public Person(DateOnly birthday)
    {
        BirthDay = birthday;
        Age = DateOnly.FromDateTime(DateTime.Now).Year - birthday.Year;
    }
}

The Age variable is private, and as such cannot be accessed externally. In this example the value of Age set in the constructor and never used again.

However, if we update the class, adding two new methods:

  • One method which takes a Person instance as an input parameter
  • One method which returns a Person instance
public class Person
{
    public DateOnly BirthDay { get; set; }

    private int Age;
    
    public Person() 
    { }

    public Person(DateOnly birthday)
    {
        BirthDay = birthday;
        Age = DateOnly.FromDateTime(DateTime.Now).Year - birthday.Year;
    }

    public Person Clone()
    {
        return new Person
        {
            Age = this.Age, // this variable is private!
            BirthDay = this.BirthDay
        };
    }

    public bool IsOlderThan(Person otherPerson)
    {
        return this.Age > otherPerson.Age; //otherPerson.Age is private!
    }
}
  • Clone method: In this method, a new instance of Person is declared, and the private Age variable is accessible and manually set. This is only possible as the new Person instance is being declared within the Person class.

  • IsOlderThan method: In this method, an instance of Person is passed in as a parameter. The private Age value of the parameter is compared against the Age of this Person. This is only possible as the Person parameter is being used within the Person class.


Notes

There is a very narrow scope in which private variables are accessible "outside" the instance of the class - so this is not something to worry about happening on a broad scale. However when the use case arises, it is useful to know about this feature - it means the private variable doesn't unnecessarily have to be changed to public (or internal)


References

Five C# Features You Might Not Know

Daily Drop 53: 15-04-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net private accessing