In C#, access modifiers are keywords used to control the accessibility of class members (properties, methods, fields, etc.) from other classes or code blocks. There are five access modifiers in C#:
By using access modifiers, you can control the visibility and accessibility of your class members, which helps you create more secure and maintainable code. Access modifiers also provide a way to hide implementation details and prevent unintended modification of internal state by external code.
Example:
using System;
namespace AccessModifiersExample
{
public class Animal
{
private string species; // private field
protected int age; // protected field
public string name; // public field
internal string habitat; // internal field
public Animal(string species, int age, string name, string habitat)
{
this.species = species;
this.age = age;
this.name = name;
this.habitat = habitat;
}
public void Eat()
{
Console.WriteLine($"{name} is eating.");
}
protected void Sleep()
{
Console.WriteLine($"{name} is sleeping.");
}
private void Move()
{
Console.WriteLine($"{name} is moving.");
}
internal void DisplayHabitat()
{
Console.WriteLine($"{name} lives in {habitat}.");
}
}
public class Dog : Animal
{
public Dog(string name, int age, string habitat) : base("Canine", age, name, habitat)
{
}
public void Bark()
{
Console.WriteLine($"{name} is barking.");
}
}
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal("Feline", 3, "Lion", "Savannah");
Dog dog = new Dog("Rufus", 2, "Suburb");
Console.WriteLine(animal.name); // public field is accessible from outside the class
animal.Eat(); // public method is accessible from outside the class
// animal.Move(); // private method is not accessible from outside the class
// animal.Sleep(); // protected method is not accessible from outside the class
Console.WriteLine(dog.name); // public field inherited from base class is accessible from derived class
dog.Bark(); // public method defined in derived class is accessible from derived class
dog.Eat(); // public method inherited from base class is accessible from derived class
// dog.Move(); // private method defined in base class is not accessible from derived class
// dog.Sleep(); // protected method defined in base class is not accessible from derived class
Console.WriteLine(animal.habitat); // internal field is accessible from the same assembly
animal.DisplayHabitat(); // internal method is accessible from the same assembly
}
}
}
In this example, we have a base class called “Animal” that has private, protected, public, and internal fields and methods. We also have a derived class called “Dog” that inherits from the “Animal” class.
Here’s a breakdown of the access modifiers used in this example:
By using these access modifiers, we can control the visibility and accessibility of our class members and ensure that our code is secure and maintainable.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.