Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Keywords in C#
3.
Modifier Keywords
3.1.
Access Modifiers Keywords
4.
Statement Keywords
5.
Access Keywords
6.
Namespace Keywords
7.
Operator Keywords
8.
Type keywords
9.
Contextual keywords
10.
Query keywords
11.
FAQs
12.
Key Takeaways
Last Updated: Mar 27, 2024

Keywords in C#

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Keywords are reserved words in a programming language used for internal processes or represent predefined actions. These keywords have special meanings to the compiler. Therefore, these predefined words cannot be used as variable names or objects. Doing so will result in a compile-time error.
 

Now we will learn about keywords in C# and the various categories like modifier keywords, statement keywords, access keywords, namespace keywords, operator keywords, type keywords, contextual keywords, and query keywords.

Also see, c# interview questions

Keywords in C#

C# is rich in keywords, which help make the language strong and versatile. These keywords have special significance and cannot be used as identifier names for variables, classes, interfaces, etc. 

 

Some of the keywords in C# are:- bytecharcheckedclassconstcontinuedecimal, etc.

 

Some Keywords in C#

Source: https://ecomputernotes.com/csharp/cs/c-sharp-keywords

 

The keywords in C# are categorized under various categories. Let's explore each category of keywords in detail.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Modifier Keywords

Modifier keywords in C# are specific keywords that indicate who can modify types and type members. It allows or prevents certain parts of programs from being modified by other parts.

For example:- internalabstract, const, etc.

Access Modifiers Keywords

Access modifiers keywords are applied to the declaration of a method, class, fields, properties, and other members. They are used to define the accessibility of the class and its members.
 

The access modifiers are as follows:-

Access Modifiers

Use

public It allows a part of the program in the same assembly or another assembly to access the type and its members. It is the most permissive access level as there are no restrictions on accessing public members.
private It restricts other program parts from accessing the type and its members. It is the least permissive access level as private members are accessible only within the body of the struct or the class in which they are declared.
internal It allows other program codes in the same assembly to access the type or its members. Internal members or types are accessible only within files in the same assembly.
protected It allows codes in the same class or a derived class to access the type or its members.

 

Example

using System; 

public class Program {
  class Example{       
        // public access modifier
        public string name;
  }

  public static void Main(string[] args) {
    Example obj = new Example();
  
    // Access to public members
    obj.name = "Sapna";
  
    Console.WriteLine("Name: " + obj.name);
  }
}


Output

Name: Sapna

 

To learn more about them, visit the Access Modifiers section in the documentation.

Statement Keywords

Statement keywords are the keywords related to the program flow. They include keywords for selection statements, iteration statements, jump statements, exception handling statements, etc. There are a total of 18 keywords that are used in program instructions. 

 

Some of them are: ifforbreakgotoreturnthrow, etc.


Example

using System; 

public class Program {
  public static void Main(string[] args) {
    int age = 21;
    
    // Statement keyword
    if(age >= 18) {
     Console.WriteLine("Eligible to vote.");    
    } else {
      Console.WriteLine("Not eligible to vote.");     
    }   
  }
}

 

Output

Eligible to vote.

 

To learn more about them, visit the Statement keywords section in the documentation.

Access Keywords

Access keywords are the keywords used to access the containing class or the base class of an object or class.
 

The keywords in this category are as follows:-

  • base: Used to access members of the base class from within a derived class.
  • this: Refers to the class's current instance and is used as a modifier of the first parameter of an extension method.

 

Example

public class Student{
    private string name;
    private int rollNo;

    public Employee(string name, int rollNo) {
        // Refers to the class's current instance 
        this.name = name;
        this.rollNo = rollNo;
    }
}

Namespace Keywords

The namespace keywords are used to declare a scope that contains a set of related objects. We can use a namespace to organize code elements and create globally unique types. They are applied with namespace and related operators.

 

The various namespace keywords in C# are . operator, :: operator, using, and extern alias.
 

Example

using System.Text;

 

In this example, the using directive is used to import all the types from a single namespace.

 

To learn more about them, visit the namespace keywords section in the documentation.

Operator Keywords

The operator keyword is used for different purposes like creating objects, getting the size of an object, etc. The keywords are: isasnewtypeofsizeoftruefalsestackalloc.

 

Example

using System; 

public class Program {
  public static void Main(string[] args) {
    // Displays the size of int
    Console.WriteLine(sizeof(int));
  }
}

 

Output

4

Type keywords

Type keywords are those keywords used to define various data types. There are 15 type keywords in C#. Some of them are enumstructushort, etc.


Example

using System; 

public class Program {
  public static void Main(string[] args) {
    // int keyword
    int number = 10;
    Console.WriteLine("Number =  " + number);
  }
}

 

Output

Number =  10

Contextual keywords

Contextual keywords give a specific meaning to the program, but it is not a reserved word. It can be used as an identifier outside the context. Whenever a new keyword comes in C#, it is added to the contextual keywords to avoid crashing programs written in earlier versions.

 

They can have different meanings in two or more contexts. For example, where and partial, have special meanings in two or more contexts.

 

Some of the contextual keywords are letglobaladdawait, etc.

 

Example

var i = 10; 

Query keywords

Query keywords are contextual keywords used in LINQ(Language Integrated Query) queries. Some of the keywords are selectascendingequals, etc.

 

Example

using System; 
using System.Linq;

public class Program {
  public static void Main(string[] args) {
    // Array of numbers
    int[] numbers = { 5, 8, 6, 3, 9, 7, 9, 7, 1, 0 };
 
    // Query to select numbers higher than 5
    var higherNumbers = from num in numbers
      where num > 5
      select num;

    // Execute the query to display numbers higher than 5
    foreach (int i in higherNumbers) {
      Console.Write(i + " ");
    }
  }
}

 

Output

8 6 9 7 9 7 

 

Must Read IEnumerable vs IQueryable, singleton design pattern in c#

FAQs

  1. How can we use keywords in C# as identifiers?
    If we want to use the keywords in C# as identifiers, we may prefix the keyword with the "@" character.
    For example- @if
     
  2. What is meant by the reference type keywords in C#?
    The keywords used to store references of the data or objects are called reference type keywords. The six reference type keywords in C# are: class, interface, object, delegate, void, and string.
     
  3. Which is the default access modifier?
    internal is the default access modifier when no other modifier is specified.
     
  4. Explain the use of literal keywords in C#?
    Literal keywords are those keywords that are used as a literal or a constant. The keywords are null and default.
     
  5. What is the use of the conversion keywords in C#?
    Conversion keywords are used in type conversions. The keywords are explicit, implicit, and operator.
     
  6. What is the difference between keywords and identifiers in C#?
    Keywords are predefined and reserved words that hold special meanings. On the other hand, an identifier is a name given to a variable, class label in the program, or function.

Key Takeaways

In this article, we have extensively discussed the keywords in C#, and the various categories like modifier keywords, statement keywords, access keywords, namespace keywords, operator keywords, type keywords, contextual keywords, and query keywords.
 

We hope that this blog has helped you enhance your knowledge regarding the keywords in C# and if you would like to learn more, check out our articles on Functions in C#. If you are an experienced professional, you can check out C# interview questions for 5 years Experience. Do upvote our blog to help other ninjas grow. 

Recommended Readings:

Singleton Design Pattern C#.

Happy Coding!

Live masterclass