Keywords in C#:
Keywords are predefined reserved Identifiers that have special meanings. They Can't be used as identifiers in our Program unless they include @ as a prefix. For Example: @if is a valid Identifier but only if is not valid, because it is an Keyword.
C# Reserve a Number of Words for Special use, also know as reserved words. Down, there is an table of Keywords, we can't use any of these keywords in a program as a variable or Identifiers. If we accidentally attempt to use any keywords in a Program as a variable, the compiler will generate an error.
“
If you want to use any of the Reserved word / keyword as an Variable, or you're not sure , whether its keyword or not. you can use "at" symbol @ with your keyword to make it variable.
For Example: case is the Keyword, and we can't use it, but we can use it like this way: @case
Keywords | Description |
---|---|
Abstract | This keyword indicates that the thing being modified has a missing or incomplete implementation. We can use Abstract Modifier with Classes, Methods, Properties, indexers and Events. |
as | this is an operator to perform certain types of conversion between compatible reference types or Null able types. |
base | We use this keyword to access members of the Base class from within a derived class. ( read more... ) |
bool | We use This keyword to declare variables to store the Boolean Values. (i.e. true or false). ( read more... ) |
break | We use this keyword to terminates the loop or switch statement. ( read more... ) |
byte | This keyword denotes an integral type that store values from Range 0 - 255. ( read more... ) |
case | We use this keyword in "switch" Statement to control the execution of statements from a list. ( read more... ) |
catch | We use this keyword to Handle the Exception in C#. ( read more... ) |
char | We used this keyword to declare variables to store the Unicode character. ( read more... ) |
checked | We use this keyword to Explicitly enable overflow checking for integral-type arithmetic operations and Conversions. |
class | We use this keyword to declare a Class. Class is the blue print for any functional entity which defines its properties and its function. ( read more... ) |
const | We use this keyword to declare a constant. constants can be number, Boolean values, string or any other. |
continue | We use this keyword to pass the statement control to the next iteration of the enclosing loops statement in which it appears. ( read more... ) |
decimal | We use this keyword to declare a variable to store decimal values upto 28-29 significant digits. ( read more... ) |
default | We use this keyword in Switch statement or in Generic code to set an default value. ( read more... ) |
delegate | It is the reference type variable to hold the reference to a method. This keyword are similar to pointers to functions, in C or C++. ( read more... ) |
do | We use this keyword to execute a statement or a block of statements repeatedly until a specified expression evaluates to false. ( read more... ) |
double | We use this keyword to declare an variable to store an -bit floating-point values. ( read more... ) |
else | We use this keyword to make decision that which statement need to run based on the value of a Boolean expression. ( read more... ) |
enum | We use this keyword to declare an enumeration, a Distinct type that consist of a set of named constants called the enumerator list. ( read more... ) |
event | We used this keyword to declare an Event in a Publisher class. ( read more... ) |
explicit | We use this keyword to declares a user-defined type conversion operator that must be invoked with a cast. ( read more... ) |
extern | We used this keyword to declare a method that is implemented externally. |
false | We used this keyword to returns the bool value true to indicate that an operand is False and return false otherwise |
finally | We use this keyword in Exception handling to finalize the process. ( read more... ) |
fixed | We use this statement to prevents the Garbage collector from relocating a movable variable. |
float | We used this keyword to declare an Variable that can store 32-bit floating point values. ( read more... ) |
for | We use this keyword to run a statement or a block of statements repeatedly until a specified expression evaluates to false. ( read more... ) |
foreach | We use this keyword to iterate through the collection to get the information that we want. This statement repeats a group of embedded statements for each element in an array or an object collection. |
goto | We use this keyword to transfers the Program control directly to a labeled statement. |
if | We use this statement to make decision based on the value of a Boolean expression. ( read more... ) |
implicit | We use this keyword to declare an implicit user-defined type conversion operator. ( read more... ) |
in | We use this keyword to identifies the collection to enumerate in a foreach loop. |
in | (Generic Modifier) We use this keyword to specifies that the parameter is contravariant. we can use this keyword in generic interfaces and delegates. |
int | We used this keyword to decalre an variable to store an integral type value. ( read more... ) |
interface | We use this keyword to declare an Interface. interface provide a construct for a programmer to create types that can have methods, properties, delegates, events, and indexers declared, but not implemented. ( read more... ) |
internal | We use this keyword as an Access Modifier for types and type members. Internal types or members are accessible only within files in the same assembly. |
is | We use this keyword to checks, if an object is compatible with a given type. |
lock | We use this keyword to marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object. |
long | We use this keyword to declare an variable to store an 64-bit integers. ( read more... ) |
namespace | We use this keyword to declare a scope that can organize the code element and create Globally unique types. ( read more... ) We |
new | We can use this keyword as an Operator (Used to create objects and invoke constructors.) , Modifier ( Used to hide an inherited member from a base class member) and a constraint (used to restrict types that might be used as arguments for a type parameter in a Generic declaration). |
null | We use this keyword to represents a Null reference, one that does not refer to any object. |
object | We use this keyword in field, method, property, and variable declarations and in cast and typeof operations. |
operator | We use this keyword to overload the built-in Operator or to provide a user-defined conversion in class or struct declaration. |
out | We use this keyword to explicitly specifies that a variable should be passed by reference to a method, and set in that method. |
out | (Generic Modifier) We use this keyword to specifies that the type parameter is covariant. We can use this keyword in Generic interfaces and delegates. |
override | We use this keyword to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. |
params | We use this keyword to specify a method parameter that takes a variable number of arguments. |
private | We use this keyword to make the method, or property private to its enclosing class. The Private members or properties are accessible only within the body of the class or struct in which they are declared. ( read more... ) |
protected | We use this keyword to protect the method or properties from the external world. The protected members is accessible within its class and by derived class instances. ( read more... ) |
public | We use this keyword to make the methods or properties public to its enclosing class. the public methods are accessible from outside the class. ( read more... ) |
readonly | We use this keyword as an modifier that we can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. |
ref | We use this keyword to pass an arguments by reference, not by value. ( read more... ) |
return | We use this statement to terminates execution of the method in which it appears and returns control to the calling method. |
sbyte | We use this keyword to declare an variable to store 8-bit integers value. ( read more... ) |
sealed | We use this keyword in class to prevents other classes from inheriting from it. ( read more... ) |
short | We use this keyword to declare a variable to store an 16-bit integers. ( read more... ) |
sizeof | We use this operator to know the exact size of Built-in types in bytes. ( read more... ) |
stackalloc | We use this keyword in an unsafe code context to allocate a block of memory on the stack. |
static | We use this keyword to declare a static member, which belongs to the type itself rather than to a specific object. |
string | We use this keyword to represents a zero or more Unicode characters. ( read more... ) |
struct | We use this keyword to encapsulate small groups of related variables, such as the coordinates of a rectangle. ( read more... ) |
switch | We use this keyword to control the execution of statements from a list. ( read more... ) |
this | We use this keyword to refers to the current instance of the class. and we also used it as a modifier of the first parameter of an extension method. ( read more... ) |
throw | We use this keyword to handle the exceptions during the program execution. ( read more... ) |
true | We use this keyword to returns the bool value true, to indicate that an operand is true, and return false otherwise. |
try | We use this keyword to handle exception during the program execution. ( read more... ) |
typeof | We use this keyword to returns an instance of the type class when passed a name of a class. It is similar to the sizeof() Keyword in that it returns a value instead of starting a section of code. |
uint | We use this keyword to declare a variable to store an unsigned 32-bit integer. ( read more... ) |
ulong | We use this keyword to declare an variable to store an unsigned 64-bit integers. ( read more... ) |
uchecked | We use this keyword to suppress overflow-checking for integral-type arithmetic operations and conversions. |
unsafe | We use this keyword to modify a procedure or define a block of code which uses unsafe code. Code is unsafe if it uses the "Address of" (&) or "Pointer Operator" (*) |
ushort | We use this keyword to declare an variable to store an unsigned 16-bit integer. ( read more... ) |
using | We use this keyword as a Directive ( When it is used to create an alias for a namespace or to import types define in other namespace.) And As a Statement (When it defined a scope at the end of which an object will be disposed) |
virtual | We use this keyword to modify a Method, property, indexer, or event declaration and allow for it to be overridden in a derived class. |
void | We use this keyword as an return type for a method. Void specifies that the method doesn't return a value. |
volatile | We use this keyword to indicates that a filed might be modified by Multiple thread that are executing at the same time. |
while | We use this keyword to to executes a statement or block of statements until a specified expression evaluates to false. ( read more... ) |
Contextual Keywords:
A contextual keyword is used to Provide a specific Meaning in the code, but it is not a reserved word in C#. some contextual keyword, such as Partial and where have special meanings in two or more contexts.
Keyword | Description. |
---|---|
add | We use this contextual keyword to define a custom event accessor that is invoked when client code subscribes to our event. |
Alias | We use this keyword to indicate an external alias. When we need to use several versions of the same assembly or assemblies with the same full Qualified typenames, we need to use the alias keywords to give different alias names for each version. |
ascending | We use this keyword in orderby clause in Query expressions to specify that the sort order is from smallest to largest. |
async | We use this keyword to specify that a method is asynchronous. We we use this keyword on a method or expression, it;s referred to as an async method. |
await | We use this keyword to a task in an asynchronous method to suspend the execution of the Method until the awaited task completes. |
descending | We use this keyword in the orderby clause in Query expressions to specify that the sort order is from largest to smallest. |
dynamic | We use this keyword to enables the Operations in which it Occurs to bypass compile-time type checking, instead these operations are resolved at run time. |
from | We use this keyword to specifies the data source on which the Query will be run. and a local range variable that represents each element in the source sequence. |
get | We use this keyword to defines an Accessor method in a property or indexer that retrieves the value of the Property or the indexer element. |
global | We use this keyword to resolve ambiguity between identifiers. if we have a conflict between a class name and a namespace. |
group | We use this keyword to returns a sequence of objects that contain zero or more items that match the key value for the group. |
into | We use this keyword to create a temporary identifier to store the results of a Group, join, or select clause into a new Identifier. |
join | This keyword is useful for associating elements from different source sequences that have no direct relationship in the object model. |
let | We use this keyword to store the result of a sub-expression in order to use it in subsequent clauses. |
orderby | We use this keyword to returned sequence or groups to be sorted in either ascending or descending order. |
partial(type) | We use this keyword to define class, struct, or interface to be split into multiple files. |
partial(method) | We use this keywords to provide methods hooks in class. |
remove | We use this keyword to define a custom event accessor that is invoked when client code unsubscribes from our event. |
select | We use this keyword in Query expression to specifies the type of values that will be produced when the Query is executed. |
set | We use this keyword to define an accessor method in a property or indexer that assigns the value of the property or the indexer element. |
value | We use this keyword in the set accessor in ordinary property declarations. |
var | In the Beginning in Visual C# 3.0, Variables that are declared at method scope can have an implicit type var. |
where | (Generic type constraint) We use this keyword in a generic type definitions to specify constraints on the types that can be used as arguments for a type parameter defined in a Generic declaration. |
where | (Query clause) We use this keyword in a Query expression to specify which elements from the data source will be returned in the Query expression. |
yield | We use this keyword in statement to indicate that the method, operator, or get accessor in which it appears is an iterator. |
C# Identifiers:
Identifier is a name to use identify a variable, function, class, module, or any other user-defined item. an identifiers start with a letter A to Z or a to z, an underscore (_) followed by zero or more letters, and at (@) symbol.
“
C# is case-sensitive programming language. Thus, "InfoBrother" and "infobrother" are two different identifiers in C#.
Identifiers can have Nearly any name, bu a few restrictions apply. here are some rules to follow when creating identifiers.
//Following names are valid identifiers in C#:
char infoBrother; //start with lower case letter
char InfoBrother; //start with upper case letter.
char _InfoBrother; //start with underscore:
int @int; //use keyword with "@" at symbol.
double \u098info; //a Unicode character.
//Following names are not valid as an identifiers in C#:
char 5infobrother; //Error: start with number;
char public; //Error: public is an keyword:
double \u0027format //Error: Unicode its an Unicode formatting character.