C# .NET - Access private members directly
Found out something interesting this morning while writing a copy constructor in C# .NET 2.0 code. Similar to Java, within a class, if you use another reference of the same class within a method of that class, you can directly access private members of that other reference.
An example should show this more clearly. The following function is a copy constructor. I like to use leading underscores for private class members.
As you can see, it is accessing the reference's private members directly.
Why would you want to do this?
What if your public get/set accessor methods return altered values? Being forced to use the get/set methods would not allow a copy constructor to work properly in this case.
This isn't only limited to constructors either, you can do this in any method of the class as long as the reference is the same type. Even static methods. Like I said, this also works in Java.
An example should show this more clearly. The following function is a copy constructor. I like to use leading underscores for private class members.
public Category(Category cat) { _id = cat._id; _name = cat._name; _description = cat._description; }
As you can see, it is accessing the reference's private members directly.
Why would you want to do this?
What if your public get/set accessor methods return altered values? Being forced to use the get/set methods would not allow a copy constructor to work properly in this case.
This isn't only limited to constructors either, you can do this in any method of the class as long as the reference is the same type. Even static methods. Like I said, this also works in Java.
Comments: (1)
 
Sean
I just came across this bizarre OOP artifact myself. I think the below definition can help to explain
private accessor - can only be reached by members from the same class.
You'll notice it doesn't say same 'instance' of the class, just the same class. This serendipitously results in allowing scenarios where if class A has a member variable also of class A (not descendants of A, just A itself); it allows the outer class A to directly access the private variables of the inner class A. Bizarre, disturbing, inethical, yet most convenient for what I'm trying to accomplish. Please don't fix this, Microsoft.
private accessor - can only be reached by members from the same class.
You'll notice it doesn't say same 'instance' of the class, just the same class. This serendipitously results in allowing scenarios where if class A has a member variable also of class A (not descendants of A, just A itself); it allows the outer class A to directly access the private variables of the inner class A. Bizarre, disturbing, inethical, yet most convenient for what I'm trying to accomplish. Please don't fix this, Microsoft.
alphatrak
Bringing you the coding smackdown since '95
Bringing you the coding smackdown since '95
Now Playing
- StarCraft - PC
Now Reading
- Death Note
- Ikigami: The Ultimate Limit
- Infinite Game Universe: Mathematical Techniques
- Microserfs by Douglas Coupland
- Pro Android 2