Modifiers in C#

This article describes about the modifiers used in C# programming language. Summary of the article:

  • What is Modifiers?
  • Modifiersin C#

What is Modifiers?
In programming language modifiers are used to modify declarations of types and type members. C# used the following modifiers.

  • Access Modifiers
  • abstract
  • async
  • const
  • event
  • extern
  • new
  • override
  • partial
  • readonly
  • sealed
  • static
  • unsafe
  • virtual
  • volatile

Access Modifiers
It determine the scope of the method or variables that can be accessed from other various objects or classes. It has five types:

  1. public
  2. private
  3. internal
  4. protected
  5. protected internal

abstract
It determines that a class is intended only to be a base class of other classes. That means a class need to be inherited. The abstract modifier indicates that it has a missing or incomplete implementation

async
async modifiers indicates that the modified method, lambda expression, or anonymous method is asynchronous.

const
const modifiers specifies that the value of the field or the local variable cannot be modified.

event
event modifiers declares an event in a class.

extern
extern modifiers indicates that the method is implemented externally. We commonly use it with DllImport attribute to call unmanaged code written in DLL.An extern method has no method body. Because it is implemented in the Dll.

[DllImport(“callinfo.dll”)]
private static extern void MyInfo();

new
new modifiers determines that a class hides an inherited member of the same name. In programming the new keyword is used to hide a method, property, event of base class into derived class.

override
override modifiers provides a new implementation of a virtual member inherited from a base class. The overridden method in the derived class must have the same signature as the base class. We can’t override a non-virtual or static method and the overridden base method must be virtual, abstract, or override.

partial
partial modifiers is used to split the definition of a class or struct or interface across multiple source files. Each source file contain a section of the class definition. When the application is compiled all parts are combined.This type of Splitting files can be useful for large projects or with automatically generated code. Microsoft Visual Studio uses this functionality.

//File1.cs
public partial class Order
{
private GetOrder() { ... }
}

//File2.cs
public partial class Order
{
public RetrieveOrder()
{
GetOrder();
.....
}
}

readonly
readonly keywords declares a field that can only be assigned values as part of the declaration or in a constructor in the same class. It can only be applied to class fields. The readonly keyword is different from the const keyword. A const field is a compile-time constant. A readonly field can be used for runtime constants.

public readonly int y = 25;
public readonly int z;

sealed
sealed modifieres determines that a class cannot be inherited. sealed is typically used to prevent accidental inheritance of the class. struct are implicitly sealed and thus cannot be inherited.

static
Static keyword is used to declare a member of a type so that it is specific to that type. In computer memory there are lots of locations. Static means stands alone or just one location. The static modifier is used on class, methods, field, property, and constructor. Static modifiers are used when we want to access a class without creating its instance. It improves code efficiency and performance but make codes less flexible.

unsafe
Unsafe modifiers Declares an unsafe context. The unsafe keyword is used for any block of code that uses pointer. To compile unsafe code we must need /unsafe compiler. Because unsafe code is not verifiable by the CLR or common language runtime. The unsafe modifiers is used on class member (methods, property, constructor and so on – but not for static constructors)

virtual
The virtual modifier declares a method or an accessor whose implementation can be changed by an overriding member in a derived class. In OOP the virtual keyword is used to modify a method, property, indexer, or event which is declared in the base class and allows it to be overridden in the derived class. In C#, in order to override the base class method in derived class, we have to declare base class method as virtual and derived class method as override.
The virtual modifiers are used on methods, properties, events, and indexers.

volatile
The virtual keyword indicates that a field can be modified in the program by something such as hardware, operating system, or a concurrently executing thread. This field is usually used for fields that will be accessed by multiple threads without using the lock statement to synchronize access. This modifier ensures that one thread retrieves the most-up-to-date value written by another field.

volatile
Indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

That’s all about C# modifiers.

Comments

  1. By Kerman Smith

    Reply

  2. Reply

Leave a Reply

Your email address will not be published. Required fields are marked *