Monday 3 April 2023

Access modifiers in C#

 Access modifiers in C# are keywords used to control the accessibility of types and members in a class. There are five access modifiers available in C#:

  1. Public: A public member can be accessed from anywhere, inside or outside the class.

  2. Private: A private member can only be accessed within the class in which it is declared.

  3. Protected: A protected member can be accessed within the class in which it is declared, as well as by any derived class.

  4. Internal: An internal member can be accessed only within the same assembly (i.e., the same .exe or .dll file) in which it is declared.

  5. Protected internal: A protected internal member can be accessed within the same assembly in which it is declared, as well as by any derived class, whether it is in the same assembly or not.

To specify an access modifier for a type or member, the modifier keyword is placed before the type or member declaration. For example, to declare a public method, you would use the "public" keyword followed by the method signature:

csharp
public void MyMethod() { // method body }

It is important to choose the appropriate access modifier for each type and member to ensure that your code is secure and well-organized.


here are some examples of how access modifiers work in C#:

csharp
public class MyClass { public int publicField; private int privateField; protected int protectedField; internal int internalField; protected internal int protectedInternalField; public void MyMethod() { // code here can access all fields publicField = 1; privateField = 2; protectedField = 3; internalField = 4; protectedInternalField = 5; } } public class MyDerivedClass : MyClass { public void MyDerivedMethod() { // code here can access public and protected fields publicField = 1; protectedField = 2; // cannot access private or internal fields // privateField = 3; // error // internalField = 4; // error // can access protected internal fields protectedInternalField = 5; } } public class AnotherClass { public void AnotherMethod() { MyClass myClass = new MyClass(); // code here can access only public fields and methods myClass.publicField = 1; myClass.MyMethod(); // cannot access private, protected, internal or protected internal fields or methods // myClass.privateField = 2; // error // myClass.protectedField = 3; // error // myClass.internalField = 4; // error // myClass.protectedInternalField = 5; // error } }

In this example, we have a class called MyClass with five fields and a method. Each field has a different access modifier, and the method is public.

Then we have a derived class called MyDerivedClass that inherits from MyClass. In MyDerivedMethod, we can access public and protected fields, but not private or internal fields.

Finally, we have a class called AnotherClass that creates an instance of MyClass. In AnotherMethod, we can only access public fields and methods of MyClass. We cannot access any of the other fields or methods because they have a different access modifier.

No comments:

Post a Comment

Draw Circle in C#

 Here's an example program in C# that uses the `System.Drawing` namespace to draw a circle on a Windows Form: // Mohit Kumar Tyagi using...