Are private fields really private?

As the saying goes, private fields and properties of a class are not available outside the class. But are they really private? Well, they are not; once they are in memory, anyone can tap in to them.

Let’s write some code in c#:

void Main()
{
	//create an instance and dump
	var demo = new PrivateDemo().Dump();
	
	FieldInfo[] privateFieldInfos = typeof(PrivateDemo)
				.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
	
	//set new value and dump
	foreach (var fieldInfo in privateFieldInfos)
	{
		fieldInfo.SetValue(demo, "I am no longer private...");
		fieldInfo.GetValue(demo).Dump(fieldInfo.Name);
	}
}

public class PrivateDemo
{
	public string PublicProperty { get; set; }
	private string PrivateProperty { get; set; }

	private string privateField;
	
	public PrivateDemo()
	{
		PublicProperty = "I am public...";
		PrivateProperty = "I am private...";
		privateField = "I am also Private...";
	}
}

And output from LinQPad:

comments powered by Disqus