Keywords in C#

In programming languages, including C#, a keyword is a reserved word that has a predefined meaning and purpose in the language's syntax. Keywords cannot be used as identifiers (such as variable names or function names) because they are already assigned a specific role in the language.

Here are some examples of keywords in C#:


using System;

class Program
{
    static void Main()
    {
        int number = 10;
        string text = "Hello, world!";
        
        if (number > 5)
        {
            Console.WriteLine(text);
        }
        else
        {
            Console.WriteLine("Number is not greater than 5.");
        }
    }
}

In the example above, we have used several keywords:

  • 'using': This keyword is used to include a namespace in the current code file. In this case, we include the 'System' namespace, which provides access to standard types and input/output functionalities.
  • 'class': This keyword is used to declare a class. In the example, we define a class called 'Program'.
  • 'static': This keyword is used to define a static member of a class. In the example, the 'Main' method is declared as static.
  • 'void': This keyword is used to indicate that a method does not return a value. In the example, the Main method has a void return type.
  • 'int' and 'string': These keywords are used to define the data types of variables. In the example, we declare an 'int' variable called number and a 'string' variable called 'text'.
  • 'if' and 'else': These keywords are used to define conditional statements. In the example, we use them to check if the value of 'number' is greater than 5 and execute the corresponding code block.
  • 'Console': This keyword is part of the 'System' namespace and is used to interact with the console for input and output operations. In the example, we use 'Console.WriteLine()' to display messages on the console.

These are just a few examples of the keywords used in C#. Each keyword has a specific purpose and syntax associated with it, contributing to the structure and behavior of the code.