Keyword
C# keywords are predefined and reserved for a language usage.Each keyword have its own meaning and instruction to the compiler. If you use the same name identifier, it will conflict and throw error. You can use the same name of a keyword adding @ at the start of a identifier name.
Here is the example of how error occurred if we use same name identifier and keyword.
Example 1
There are two types of Keyword, reserve and contextual.
Contextual keyword : keyword are valid for certain context only. This keyword are not reserved, are reserved for certain context only.
C# keywords are predefined and reserved for a language usage.Each keyword have its own meaning and instruction to the compiler. If you use the same name identifier, it will conflict and throw error. You can use the same name of a keyword adding @ at the start of a identifier name.
Here is the example of how error occurred if we use same name identifier and keyword.
Example 1
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
student
case
= new
student();
}
}
class
student
{
string
Name;
int
Roll_Number;
}
}
Output :Error
Invalid expression term 'case'
Here 'case' is reserve keyword , we used as identifier , causes error.
Here is the example how we can use same name identifier and keyword without error.
Example 1
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
student
@case = new
student();
}
}
class
student
{
string
Name;
int
Roll_Number;
}
}There are two types of Keyword, reserve and contextual.
Here is the list
of reserved keyword commonly used.
abstract | explicit | const | event |
---|---|---|---|
return | new | override | extern |
readonly | sealed | static | unsafe |
virtual | volatile | public | private |
internal | protected | if | else |
switch | case | do | foreach |
in | while | break | continue |
default | goto | throw | try |
catch | finally | unchecked | checked |
fixed | lock | ref | out |
using | awit | true | enum |
void | bull | bite | case |
class | decimal | int | double |
float | string | unit | sbyte |
struc | long | double |
Contextual keyword : keyword are valid for certain context only. This keyword are not reserved, are reserved for certain context only.
add | async | await | dynamic |
---|---|---|---|
get | global | partial | remove |
set | value | var | when |
where | yield |
Here is the example of contextual keyword , 'global' keyword here used for current context only.
Example 1
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
global::System.Console.WriteLine("Your
Specific Text");
Console.ReadKey();
}
}
class
System
{
}
}
No comments:
Post a Comment