Based on this discussion, closures in C# promote private variables to public variables at compile time.

Suppose a closure was implemented on a Username or Password string. Would there be any security issue with this at run time? Is this an issue in any other language?

If so, is there any way I can prevent this (via attributes)?

1 Answer 1


Despite the visibility promotion, Closures do not present any new security issue.

This is because of the simple reason, that visibility (i.e. private/public) is not a security mechanism in the first place.
Just because you marked your password field as private, doesnt mean it is not accessible. It's not accessible, but only to code that behaves nicely.
It's still accessible easily in code, e.g. via reflection. (Of course, consumers of your class shouldn't do that...)

And that's even without going into things like debugging, memory inspection, etc...

So - as I said, it doesnt affect the level of security. You'd need to protect that Password field, but you'd need to do that anyway.
(Suggestion: store passwords as SecureString instead of strings... )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .