A Guide to Reserved Keywords in C#

Category > CSHARP || Published on : Friday, March 3, 2023 || Views: 160 || C# reserved keywords syntax programming language guide sample code examples syntax and structure.


C# is a powerful programming language that comes with a set of reserved keywords. These keywords are reserved for specific functions in the language and cannot be used as identifiers such as variable names or method names. It's essential to have a good understanding of these reserved keywords to write effective and efficient C# code. In this article, we will discuss the reserved keywords in C# and provide sample code examples to illustrate their use.

Introduction:

C# is a programming language that has a set of keywords that are reserved for specific functions in the language. These keywords have a predefined meaning and can't be used as identifiers such as variable names or method names. These keywords are known as reserved keywords in C#. In this article, we will discuss the reserved keywords in C# and how they are used.

Reserved Keywords in C#:

C# has a total of 78 reserved keywords, which are used to define the language's syntax and structure. Here is a list of all the reserved keywords in C#:

abstract, as, base, bool, break, byte, case, catch, char, checked, class, const, continue, decimal, default, delegate, do, double, else, enum, event, explicit, extern, false, finally, fixed, float, for, foreach, goto, if, implicit, in, int, interface, internal, is, lock, long, namespace, new, null, object, operator, out, override, params, private, protected, public, readonly, ref, return, sbyte, sealed, short, sizeof, stackalloc, static, string, struct, switch, this, throw, true, try, typeof, uint, ulong, unchecked, unsafe, ushort, using, virtual, void, volatile, while.

Explanation of Reserved Keywords: Let's discuss some of the reserved keywords in C# and how they are used:

  1. abstract: The abstract keyword is used to define an abstract class or an abstract method. An abstract class cannot be instantiated and must be inherited by another class. An abstract method must be implemented by any non-abstract class that inherits from the abstract class.

Example:

public abstract class Shape
{
   public abstract double GetArea();
}

public class Rectangle : Shape
{
   double width;
   double height;

   public Rectangle(double w, double h)
   {
      width = w;
      height = h;
   }

   public override double GetArea()
   {
      return width * height;
   }
}
  1. const: The const keyword is used to define a constant variable. Once a constant variable is defined, its value cannot be changed.

Example:

public const int MaxScore = 100;
  1. delegate: The delegate keyword is used to define a delegate type, which is a type that represents a method signature. A delegate can be used to encapsulate a method call and pass it around as an object.

Example:

public delegate int Calculate(int x, int y);

public class Calculator
{
   public static int Add(int x, int y)
   {
      return x + y;
   }
}

public class Program
{
   public static void Main()
   {
      Calculate calc = new Calculate(Calculator.Add);
      int result = calc(3, 4);
      Console.WriteLine(result); // Output: 7
   }
}
  1. interface: The interface keyword is used to define an interface, which is a contract that defines a set of methods and properties that a class must implement. An interface can be implemented by any class, and a class can implement multiple interfaces.

Example:

public interface IShape
{
   double GetArea();
}

public class Rectangle : IShape
{
   double width;
   double height;

   public Rectangle(double w, double h)
   {
      width = w;
      height = h;
   }

   public double GetArea()
   {
      return width * height;
   }
}
  1. namespace: The namespace keyword is used to define a namespace, which is a container for a set of related classes, interfaces, and other types. Namespaces help to organize code and avoid naming conflicts.

Example

 
namespace MyProject.Geometry
{
   public class Rectangle
   {
      double width;
      double height;

      public Rectangle(double w, double h)
      {
         width = w;
         height = h;
      }

      public double GetArea()
      {
         return width * height;
      }
   }
}
  1. static: The static keyword is used to define a static class, a static constructor, or a static method. A static class cannot be instantiated and can only contain static members. A static constructor is used to initialize static data and is called once when the class is loaded. A static method can be called without creating an instance of the class.

Example:

public static class Calculator
{
   public static int Add(int x, int y)
   {
      return x + y;
   }
}

public class Program
{
   public static void Main()
   {
      int result = Calculator.Add(3, 4);
      Console.WriteLine(result); // Output: 7
   }
}

Conclusion:

In this article, we have discussed the reserved keywords in C# and how they are used. Understanding these reserved keywords is essential for writing effective and efficient C# code. It's important to remember that these keywords cannot be used as identifiers and have a predefined meaning in the language.