Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
The keywords in C# are categorized under various categories. Let's explore each category of keywords in detail.
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.
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.
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.
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.
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.
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.
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: is, as, new, typeof, sizeof, true, false, stackalloc.
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 enum, struct, ushort, 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.
Query keywords are contextual keywords used in LINQ(Language Integrated Query) queries. Some of the keywords are select, ascending, equals, 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 + " ");
}
}
}
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
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.
Which is the default access modifier? internal is the default access modifier when no other modifier is specified.
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.
What is the use of the conversion keywords in C#? Conversion keywords are used in type conversions. The keywords are explicit, implicit, and operator.
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.