Summary: in this tutorial, you’ll learn about the C# public and private keywords and the differences between them.
Introduction to the access modifiers in C#
When declaring a field or a method inside a class, you can specify an accessibility level. In the class
tutorial, you learned how to use the public
keyword to make a field or a method to be accessible from both inside and outside of a class.
The public
keyword is an access modifier. Besides the public access modifier, C# has other access modifiers as follows:
- private
- protected
- internal
- protected internal
- private protected
In this tutorial, you’ll focus on the public and private access modifiers.
The public access modifier
When using the public
keyword for a field or a method, you can access the field and method from both inside and outside of a class.
For example, the following defines the Person
class with two public fields FirstName
and LastName
and one public methods GetFullName()
:
// Person.cs
class Person
{
public string FirstName;
public string LastName;
public string GetFullName()
{
return $"{this.FirstName} {this.LastName}";
}
}
Code language: C# (cs)
Since the fields and methods of the Person
class are public, you can access them from the outside of the class like this:
// Program.cs
Person p1 = new();
p1.FirstName = "John";
p1.LastName = "Doe";
Console.WriteLine(p1.GetFullName());
Code language: C# (cs)
Output:
John Doe
Code language: C# (cs)
The private access modifier
The private
access modifier uses the private
keyword. When using the private
keyword for a field or method, you can only access the private field or method inside the same class. It means that you cannot access the private field or method outside of the class.
Typically, you use the private access modifier to prevent direct access to fields of a class. Also, you use the private method when you want to use that method only within the class.
If you don’t specify an access modifier for a member, that member will be private.
The following program prompts you to enter your first name and last name and assigns values to the FirstName
and LastName
fields of the Person
‘s object:
// Program.cs
Person p1 = new();
Console.WriteLine("Enter the first name and last name:");
string? firstName = Console.ReadLine();
string? lastName = Console.ReadLine();
if (!string.IsNullOrEmpty(firstName))
{
p1.FirstName = firstName;
}
if (!string.IsNullOrEmpty(lastName))
{
p1.LastName = lastName;
}
Console.WriteLine(p1.GetFullName());
Code language: C# (cs)
In this example, before assigning the firstName
and lastName
variables to the FirstName
and LastName
fields, we check if the input strings are not null or empty.
However, if we need to do this in multiple places in the program, we have to duplicate the code. Also, if we want to change the validation logic, we need to change it in various places. So the code becomes very difficult to maintain. And it violates the DRY (Don’t Repeat Yourself) principle in programming.
To fix this, you can follow these steps:
First, wrap the checking logic as a private method in the Person
class:
private bool isValidName(string? name)
{
return !string.IsNullOrEmpty(name);
}
Code language: C# (cs)
Second, make the firstName
and lastName
fields private:
private string firstName;
private string lastName;
Code language: C# (cs)
By convention, private fields are camelCase like firstName
and lastName
. Therefore, we rename the field names to follow the community convention.
Because the firstName
and lastName
fields are private, we cannot access them outside the Person
class. Therefore, to assign values to these fields, we can define two public methods SetFirstName()
and SetLastName()
.
Third, add the SetFirstName()
and SetLastName()
methods to the Person
class:
public Person SetFirstName(string? firstName)
{
if (isValidName(firstName))
{
this.firstName = firstName;
}
return this;
}
public Person SetLastName(string? lastName)
{
if (isValidName(lastName))
{
this.lastName = lastName;
}
return this;
}
Code language: C# (cs)
Inside these methods, we call the isValidName()
method to validate the name before assigning it to the corresponding private field.
Note that you’ll learn how to do it more elegantly by using a property with the getters and setters later.
The following shows the complete Person
class:
// Person.cs
class Person
{
private string firstName;
private string lastName;
public string GetFullName()
{
return $"{this.firstName} {this.lastName}";
}
private bool isValidName(string name)
{
return !string.IsNullOrEmpty(name);
}
public Person SetFirstName(string firstName)
{
if (isValidName(firstName))
{
this.firstName = firstName;
}
return this;
}
public Person SetLastName(string lastName)
{
if (isValidName(lastName))
{
this.lastName = lastName;
}
return this;
}
}
Code language: C# (cs)
Now, you can create the Person
‘s object and call the public methods SetFirstName()
and SetLastName()
to set the first name and last name private fields:
// Program.cs
Person p1 = new();
Console.WriteLine("Enter the first name and last name:");
string? firstName = Console.ReadLine();
string? lastName = Console.ReadLine();
string name = p1.SetFirstName(firstName)
.SetLastName(lastName)
.GetFullName();
Console.WriteLine(name);
Code language: C# (cs)
Summary
- Use the public access modifier for the fields and methods that are accessible from both inside and outside the class.
- Use the private access modifier for the fields and methods that are accessible only within the same class.