user606035
user606035

Reputation: 141

Do access modifiers affect performance?

Do access modifiers affect performance in C#?

Upvotes: 8

Views: 917

Answers (1)

Andrew Hare
Andrew Hare

Reputation: 351616

No, access modifiers are not considered by the runtime for execution. The only time they come into play after compilation is if you are using reflection and querying the assembly's metadata.

Think of access modifiers like concrete forms. They are put in place when the concrete is wet to provide form and boundaries for the wet concrete. Once the concrete is dry they are removed as they are no longer needed. Access modifiers are the concrete forms for your un-compiled code - once the code has been compiled the access modifiers are no longer a factor (even though they are part of the emitted IL).

Edit: Maybe "no longer a factor" is a little vague. What I mean is that it's the compiler's job to make sure that all access modifiers are properly honored and no violations occur. The runtime (at least Microsoft's CLR - other runtimes are free to implement this any way they see fit) trusts that the compiler has done its job and no further checks are necessary.

Upvotes: 9

Related Questions