← Foundations path
.NETFoundations 4 / 37

Access modifiers

“What are access modifiers, and why make a property's setter private?”

Words in front of a class member that say who may use it. They let a class hide its data and open one checked door.

See it

1/8Before. is with a public setter: any code anywhere can read it and write it.

In plain words

An access modifier is a word like or in front of a field, property or method. It says which code may use it. The compiler (the tool that turns your code into a program when you build) checks it: break the rule and the build fails. Make the data hard to reach () and give outside code one method that checks before it changes anything.

Say it in the interview

In the lending API

The same class before and after, plus the report code that broke it. Press Next line to walk through them.

Diagram · step 1Before. is with a public setter: any code anywhere can read it and write it.
Before · Loan.cs · line 5 · 1 of 1Both halves are . Any code that holds a Loan can set any number here, with no check.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Microsoft Learn: access modifiers · Restricting accessor accessibility