What are Access Modifiers in C#
In C#, access modifiers are keywords that specify the accessibility of a class, method, property, or field. There are four access modifiers in C#: public
, private
, protected
, and internal
.
public
: A class, method, property, or field that is marked aspublic
is accessible from anywhere within the project.private
: A class, method, property, or field that is marked asprivate
is only accessible within the class in which it is defined.protected
: A class, method, property, or field that is marked asprotected
is only accessible within the class in which it is defined, and any derived classes.internal
: A class, method, property, or field that is marked asinternal
is only accessible within the project in which it is defined.
By default, classes, methods, properties, and fields are private
. If no access modifier is specified, it is assumed that the member is private
.
Here is an example of how to use access modifiers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Student { // Private field private string name; // Public property public string Name { get { return name; } set { name = value; } } // Private method private void DoSomething() { // ... } // Public method public void DoSomethingElse() { // ... } } |
In this example, the name
field is private, so it can only be accessed within the Student
class. The Name
property is public, so it can be accessed from anywhere within the project. The DoSomething
method is private, so it can only be called within the Student
class. The DoSomethingElse
method is public, so it can be called from anywhere within the project.
Example : Here is an example of a C# program that illustrates the use of access modifiers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using System; namespace AccessModifiersExample { class Program { static void Main(string[] args) { // Create a new instance of the Student class Student student = new Student(); // Set the student's name and age student.Name = "John Smith"; student.Age = 18; // Get the student's name and age Console.WriteLine("Name: {0}", student.Name); Console.WriteLine("Age: {0}", student.Age); } } class Student { // Private field private string name; // Public property public string Name { get { return name; } set { name = value; } } // Private field private int age; // Public property public int Age { get { return age; } set { age = value; } } } } |
In this example, the Student
class has two private fields: name
and age
. These fields can only be accessed within the Student
class. To allow external access to these fields, the Student
class has two public properties: Name
and Age
. These properties provide getter and setter methods for the private fields.
In the Main
method, we create a new instance of the Student
class and set the Name
and Age
properties. We then use the getter methods of the Name
and Age
properties to retrieve the student’s name and age and print them to the console.
This program will output the following:
1 2 3 4 | Name: John Smith Age: 18 |