C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will discuss modifiers in C#. Modifiers are keywords that are used to define the accessibility or behavior of class members, such as properties, methods, and fields. Modifiers help you control the scope and visibility of class members and enforce object-oriented programming principles.
There are two main types of modifiers in C#:
Access modifiers control the visibility of class members. They define the scope of access for members within a class, as well as from derived classes and external classes. There are four access modifiers in C#:
The public
modifier allows access to the class member from any part of the code, including external classes and derived classes.
public class MyClass { public int MyProperty; }
The private
modifier restricts access to the class member to within the class itself. Members with the private
modifier cannot be accessed from derived classes or external classes.
public class MyClass { private int MyProperty; }
The protected
modifier allows access to the class member from within the class itself and from derived classes. Members with the protected
modifier cannot be accessed from external classes.
public class MyClass { protected int MyProperty; }
The internal
modifier allows access to the class member from within the same assembly. Members with the internal
modifier cannot be accessed from outside the assembly.
public class MyClass { internal int MyProperty; }
In addition to access modifiers, C# also provides several other modifiers that can be used to change the behavior of class members:
The static
modifier indicates that the class member belongs to the class itself rather than an instance of the class. Static members can be accessed using the class name rather than an object.
public class MyClass { public static int MyProperty; }
The readonly
modifier indicates that the value of a field can only be assigned during its declaration or within the constructor of the class.
public class MyClass { public readonly int MyProperty; public MyClass(int value) { MyProperty = value; } }
The sealed
modifier prevents a class from being inherited or a method from being overridden in a derived class.
public sealed class MyClass { //... } public class AnotherClass { public sealed override void MyMethod() { //... } }
The virtual
modifier indicates that a method or property can be overridden in a derived class.
public class MyClass { public virtual void MyMethod() { //... } }
The override
modifier is used to indicate that a method or property in a derived class overrides a virtual or abstract method or property in the base class.
public class DerivedClass : MyClass { public override void MyMethod() { //... } }
The abstract
modifier indicates that a method, property, or class does not have an implementation and must be implemented in a derived class. Abstract members can only be declared within abstract classes.
Different types of modifiers in C#
Modifiers in C# are keywords that specify the scope, accessibility, and behavior of classes, methods, and other members.
// Example: Access modifier (public) and method modifier (static) public static void DisplayMessage() { Console.WriteLine("Hello, World!"); }
Method modifiers in C#
Method modifiers include static
, virtual
, abstract
, override
, sealed
, and partial
.
// Example: Static method public static void PrintInfo() { Console.WriteLine("Printing info..."); }
C# static modifier usage
The static
modifier indicates that a member belongs to the class rather than an instance.
// Example: Static method public static void PrintInfo() { Console.WriteLine("Printing info..."); }
Modifiers and encapsulation in C#
Encapsulation involves using access modifiers (public
, private
, protected
, internal
) to control the visibility of members.
// Example: Private field private int age;
Using public and private modifiers in C#
Public members are accessible from outside the class, while private members are only accessible within the class.
// Example: Public method and private field public void DisplayInfo() { Console.WriteLine($"Name: {name}, Age: {age}"); } private int age;
Protected modifier in C#
The protected
modifier allows access to the member within the same class or its derived classes.
// Example: Protected method protected void UpdateAge(int newAge) { age = newAge; }
Internal modifier in C#
The internal
modifier restricts access to the current assembly.
// Example: Internal method internal void InternalMethod() { Console.WriteLine("Internal method"); }
Sealed modifier in C#
The sealed
modifier prevents a class from being inherited.
// Example: Sealed class sealed class FinalClass { // Class members... }
Abstract modifier in C#
The abstract
modifier indicates that a class or method has no implementation and must be implemented by derived classes.
// Example: Abstract class abstract class Shape { public abstract void Draw(); }
Virtual modifier in C#
The virtual
modifier allows a method to be overridden in derived classes.
// Example: Virtual method public virtual void Display() { Console.WriteLine("Displaying..."); }
Override modifier in C#
The override
modifier is used to provide a new implementation for a virtual or abstract method in a derived class.
// Example: Override method public override void Display() { Console.WriteLine("Overriding display..."); }
Partial class and method modifiers in C#
Partial classes allow a class to be defined in multiple files. Each part of the class can have different modifiers.
// File 1: Class definition part public partial class PartialClass { // Members... }
// File 2: Another part of the class public partial class PartialClass { // Members... }
C# const and readonly modifiers
const
: Constants are implicitly static
and must be initialized at compile time.readonly
: Read-only fields can be assigned at runtime but only within the constructor.// Example: Const and readonly public const int MaxValue = 100; public readonly string ReadOnlyValue;
Modifiers and inheritance in C#
Modifiers play a crucial role in controlling the inheritance and accessibility of members in derived classes.
// Example: Base class with protected member public class MyBaseClass { protected void ProtectedMethod() { Console.WriteLine("Protected method"); } }