Modifiers in C#
For fields in classes, the access level determines who can get or set the value of the field. For methods, the access level determines who can call the method. The access level of a class member is determined by an access modifier.
We’ve encountered access modifiers so far in our code. In our examples, you
frequently see the keyword, public.
public makes the field or method to
be accessible by anyone working with our code. Another common access modifier
is private, which restricts access to fields or methods so they can only be
used within the class. Two additional access modifiers are available in C#,
though they are used much less often than
Let’s take another look at our
HelloWorld class from the last section,
but with one small change.
HelloWorld class, we omit the
public access modifier in lines
4 and 6. Doing this implicitly gives the message field and the
method default access.
We should avoid giving everything default access when creating classes in C# and instead think carefully about what level of access each field and method should have.
The table below details whether or not information can be accessed at different
levels based on the access modifier. For example, a field with the
access modifier can be accessed within the class, but cannot be accessed
outside the class at the assembly or world-level. In C#, an assembly refers
to a grouping of classes and other resources that form a particular unit of an application.
World-level is the level of the whole application and contains all of the packages and
classes. While we will discuss later how to decide which access modifier to use for different
scenarios, you should save this table now as reference for those conversations.
If you would like to learn more about access modifiers, you should check out the documentation on the subject.
Let’s take a look at our
HelloWorld class again and add some access
message only needs to be used by
SayHello(), we declare it to be
private. Since we want
SayHello() to be usable by anybody else, we
declare it to be
In C#, you should always use the most restrictive access modifier possible. Minimizing access to class members allows code to be refactored more easily in the future, and hides details of how you implement your classes from others.
This makes your code more modular and modifiable. Each public member that you expose is another field or property that can be referenced directly elsewhere in any program using your class. Thus, changing any such field in your code could potentially break any code referencing such members. The fewer public members, the more you can change your code without breaking stuff elsewhere.
Check Your Understanding
For this question, refer to the code block below.
What access modifier would you give
- no access modifier