Keywords in C# .Net

What is Keywords in C# .Net?

Keywords are predefined and reserved identifiers for .net compiler, which cannot be used while writing any program in C#.

Apart from Access Modifiers (public, private, protected) here are some Most Used Keywords in C#

Most Used Keywords in C#
  • using

    using keyword normally used for adding namespace reference, but also can be used for adding for defining a block for new instance, let’s look at the example below.

    using System.Text; 
    using WebTrainingRoom.BO
    
    
    //now creating a new object of any class
    using(Student stu=new  Student())
    {
    
    }
    
  • class

    Class is a keyword used for Creating a class in C# .Net, here is a simple example.

    using System;
    
    public class TrainingRoom
    {
        // define properties and methods here.
    }
    

    Learn more about C# Class

  • struct
  • enum
    Enum is a value type in C# .net data type.
    enum EventState
    {
        Gif = 1,
        Jpg = 2   
    }
    Read more about enums c# example.
  • readonly
  • sealed
    Sealed is another access modifier, sealed keyword can be used with class or class-members.
    sealed class SealedExample
    {
        public int getTotal(int x, int y)
        {
            return x + y;
        }  
    }
    
    Learn more about c# sealed example.
  • static
  • volatile
    volatile keyword tells the JIT compiler, that the value of the variable not to be cached, volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time.
    class MyAccont
    {
        public volatile int balance;
        
        public void Update(int i)
        {
                balance = i;
        }
    }
    
  • unsafe
    When we mark something with unsafe keyword, it tells compiler that this code is not verifiable by CLR (common language runtime).
    unsafe static void DOWork(int p)
        {
            
        }
    
    Learn more about unsafe keyword
  • protected internal
    Protected and internal are two separate keywords, sometimes used together.
    public class Car
    {
       protected internal int EngineNumber { get; set; }  
    }
    
    learn more about C# protected internal access modifier example.
  • event
  • extern
    We use extern modifier to declare a method that is implemented externally (outside assembly).
    class TestClass
    {
        [DllImport("User32.dll"]
        public static extern string MessageBox(string message);
    
        static void Main()
        {         
            Console.Write("Enter message : ");
            string myMsg = Console.ReadLine();
            MessageBox(myMsg);
        }
    }
    
  • virtual, override
    Virtual and override are two different keywords
    
    public abstract class Car
    {
            public abstract string color { get; set; }
            public virtual int Number { get; set; }
    }
    
    public class Maruti: Car
    {
            public override string color { get; set; }
            public override int Number { get; set; }
    }
    
    When any methods are declared with virtual keyword, at implementation class we use override keyword for that method.
  • new
    New keyword often used for creating a new object of any class, by using new keyword, we occupy a memory location for the reference of that class with assigned data value.
    Car myCar=New Car();
    
  • async, await
    Async keyword is used when we want to define any Asynchronous methods in c# programming.
    public static async void Execute()
    {
    // running this method asynchronously.
    int t = await Task.Run(() => Calculate());
        Console.WriteLine("Result: " + t);
    } 
    Learn more about c# async await example
  • partial
    Partial keyword is used when we want to declare same class in two different places, and after compilation that class becomes one, this helps two or more developers work one same class without disturbing others.
    partial class TrainingBase
    {
      public abstract string Title { get; set; }
    }
    
    partial class TrainingBase
    { 
        public abstract DateTime TrainingDate { get; set; }    
    }
    
    After compilation, we see only one class that has all properties defined in different partial class files.
  • const
  • abstract

    abstract keyword can be used for declaring abstract class, property, method

    abstract class TrainingBase
    {
        public abstract string Title { get; set; }
        public abstract DateTime TrainingDate { get; set; }    
    }
    Read here moew about c# abstract class example.
  • interface
    interface is the design of any component, that only contains the declaration of the methods.
    interface ITraining
        {
            string Title { get; set; }       
            string Trainer { get; set; }         
            string TrainingDate { get; set; }
        }
    

    Here are some c# Interface example.

  • null
    Null keyword is reference type, we can assign null value to any reference type object.
    Car myCar=null;
    
    In above example, assigning null value to car object.
  • try, catch, throw
    try 
    {
        // our code.
    }
    catch(Exception ex)
    {
        throw;
    }
    
    
    Above keywords are used for exception handling while we write c# code.
 
C# Keywords
C# Tutorials
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
.Net C# Examples | Join .Net C# Course