So how about truly private fields in C#?
UPDATE: Jim pointed out that you can access the field via reflecting over the delegate. (See comment) Damn this is a bit like how java does anonymous access to private fields of the parent class. I wonder if you could use this for some nasty security violations as people tend to think local variables are safe from reflection.
After the crazy !@$% with JavaScript yesterday I said to Christian, I bet we can do this with C# lambda. So the challenge was set….
[csharp]
class Purse
{
public Func
public Purse(int money)
{
get = () => { return money; };
set = (newMoney) => { money = newMoney ; };
}
}
[/csharp]
And here is the test …
[csharp]
var p = new Purse(2); p.set(p.get() + 1); Assert.AreEqual(3, p.get());
[/csharp]
If you tried to use reflection, as expected there is no field to inspect.