As we all know that private data members are not accessible from outside of class, but suppose if we need to assign any value to private data member then how we can do that without creating object of class or using any kind of reflection
Class ABC
{
private int x;
ABC(int temp_x){this.x = temp_x;}
}
Class XYZ : ABC
{
//class XYZ declaration
}
by constructor we can assign the value when we creating the object of class. But is there any way to assign value to variable x without creating any object of ABC.

VulpesPosted Jul 24, 2014, 6:08 AM
class XYZ : ABC
{
public XYZ(int x) : base (x)
{}
}
VulpesPosted Jul 24, 2014, 7:07 AM
Normally, the private field 'i' couldn't exist at all unless an object of type ABC had been created.The method I gave earlier gets around this by creating an object of the derived class XYZ which incorporates the field inherited from its base class even though it's private and not therefore directly accessible from ABC.
This field is then set indirectly by calling the base class constructor from XYZ's constructor.
Akhil KharePosted Jul 24, 2014, 6:12 AM
Anupam SinghPosted Jul 24, 2014, 6:10 AM
this could be helpful : http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/