Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.
Example:
C#
// C# Program to illustrate the keywords
using System;
class Geeks {
// Here static, public, void
// are keywords
static public void Main () {
// here int is keyword
// a is identifier
int a = 10;
Console.WriteLine("The value of a is: {0}",a);
// this is not a valid identifier
// removing comment will give compile time error
// double int = 10;
}
}
OutputThe value of a is: 10
Keywords in C#
There are total 78 keywords in C# as follows:
abstract | do | in | protected | throw |
as | double | int | public | true |
base | else | interface | readonly | try |
bool | enum | internal | ref | typeof |
break | event | is | return | uint |
byte | explicit | lock | sbyte | ulong |
case | extern | long | sealed | unchecked |
catch | false | namespace | short | unsafe |
char | finally | new | sizeof | ushort |
checked | fixed | null | stackalloc | using |
class | float | object | static | using static |
const | for | operator | string | virtual |
continue | foreach | out | struct | void |
decimal | goto | override | switch | volatile |
default | if | params | this | while |
delegate | implicit | private |
|
|
Keywords in C# is mainly divided into 10 categories as follows:
1. Value Type Keywords: There are 15 keywords in value types which are used to define various data types.
bool | byte | char | decimal |
double | enum | float | int |
long | sbyte | short | struct |
uint | ulong | ushort |
|
Example:
C#
// C# Program to illustrate the
// value type keywords
using System;
class Geeks {
// Here static, public, void
// are keywords
static public void Main () {
// here byte is keyword
// a is identifier
byte a = 47;
Console.WriteLine("The value of a is: {0}",a);
// here bool is keyword
// b is identifier
// true is a keyword
bool b = true;
Console.WriteLine("The value of b is: {0}",b);
}
}
OutputThe value of a is: 47
The value of b is: True
2. Reference Type Keywords: There are 6 keywords in reference types which are used to store references of the data or objects. The keywords in this category are: class, delegate, interface, object, string, void.
3. Modifiers Keywords: There are 17 keywords in modifiers which are used to modify the declarations of type member.
public | private | internal | protected | abstract |
const | event | extern | new | override |
partial | readonly | sealed | static | unsafe |
virtual | volatile |
|
|
|
Example:
C#
// C# Program to illustrate the
// modifiers keywords
using System;
class Geeks {
class Mod
{
// using public modifier
// keyword
public int n1;
}
// Main Method
static void Main(string[] args) {
Mod obj1 = new Mod();
// access to public members
obj1.n1 = 77;
Console.WriteLine("Value of n1: {0}", obj1.n1);
}
}
4. Statements Keywords: There are total 18 keywords which are used in program instructions.
if | else | switch | do | for |
foreach | in | while | break | continue |
goto | return | throw | try | catch |
finally | checked | unchecked |
|
|
Example:
C#
// C# program to illustrate the statement keywords
using System;
class Geeks
{
public static void Main()
{
// using for as statement keyword
// GeeksforGeeks is printed only 2 times
// because of continue statement
for(int i = 1; i < 3; i++)
{
// here if and continue are keywords
if(i == 2)
continue;
Console.WriteLine("GeeksforGeeks");
}
}
}
5. Method Parameters Keywords: There are total 4 keywords which are used to change the behavior of the parameters that passed to a method. The keyword includes in this category are: params, in, ref, out.
6. Namespace Keywords: There are total 3 keywords in this category which are used in namespaces. The keywords are: namespace, using, extern.
7. Operator Keywords: There are total 8 keywords which are used for different purposes like creating objects, getting a size of object etc. The keywords are: as, is, new, sizeof, typeof, true, false, stackalloc.
8. Conversion Keywords: There are 3 keywords which are used in type conversions. The keywords are: explicit, implicit, operator.
9. Access Keywords: There are 2 keywords which are used in accessing and referencing the class or instance of the class. The keywords are base, this.
10. Literal Keywords: There are 2 keywords which are used as literal or constant. The keywords are null, default.
Important Points:
- Keywords are not used as an identifier or name of a class, variable, etc.
- If you want to use a keyword as an identifier then you must use @ as a prefix. For example, @abstract is valid identifier but not abstract because it is a keyword.
Example:
int a = 10; // Here int is a valid keyword
double int = 10.67; // invalid because int is a keyword
double @int = 10.67; // valid identifier, prefixed with @
int @null = 0; // valid
Illustration:
C#
// C# Program to illustrate the use of
// prefixing @ in keywords
using System;
class Geeks {
// Here static, public, void
// are keywords
static public void Main () {
// here int is keyword
// a is identifier
int a = 10;
Console.WriteLine("The value of a is: {0}",a);
// prefix @ in keyword int which
// makes it a valid identifier
int @int = 11;
Console.WriteLine("The value of a is: {0}",@int);
}
}
OutputThe value of a is: 10
The value of a is: 11
Contextual Keywords
These are used to give a specific meaning in the program. Whenever a new keyword comes in C#, it is added to the contextual keywords, not in the keyword category. This helps to avoid the crashing of programs which are written in earlier versions.
Important Points:
- These are not reserved words.
- It can be used as identifiers outside the context that’s why it named contextual keywords.
- These can have different meanings in two or more contexts.
- There are total 30 contextual keywords in C#.
add | equals | nameof | value |
alias | from | on | var |
ascending | get | orderby | when |
async | global | partial(type) | where |
await | group | partial(method) | yield |
by | into | remove |
|
descending | join | select |
|
dynamic | let | set |
|
Example:
C#
// C# program to illustrate contextual keywords
using System;
public class Student
{
// Declare name field
private string name = "GeeksforGeeks";
// Declare Name property
public string Name
{
// 'get' is a contextual keyword
get
{
return name;
}
// 'set' is a contextual keyword
set
{
name = value;
}
}
}
class TestStudent
{
// Main Method
public static void Main(string[] args)
{
Student s = new Student();
// Calls set accessor of the property Name
s.Name = "GFG";
// Calls get accessor of the property Name
Console.WriteLine("Name: " + s.Name);
// Using 'get' and 'set' as identifiers (contextual usage)
int get = 50;
int set = 70;
Console.WriteLine("Value of get is: {0}", get);
Console.WriteLine("Value of set is: {0}", set);
}
}
OutputName: GFG
Value of get is: 50
Value of set is: 70
Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers