Code |
Description |
---|---|
abstract | The abstract modifier can be used with classes, methods, properties, indexers, and events. |
as | The as operator is used to perform conversions between compatible types. |
base | The base keyword is used to access members of the base class from within a derived class |
bool | The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true and false. |
break | The break statement terminates the closest enclosing loop or switch statement in which it appears. |
byte | The byte keyword denotes an integral type that stores values as indicated in the following table. |
case | The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. |
catch | The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. |
char | The char keyword is used to declare a Unicode character in the range indicated in the following table. |
checked | The checked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. |
class | Classes are declared using the keyword class. |
const | The const keyword is used to modify a declaration of a field or local variable. |
continue | The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears. |
decimal | The decimal keyword denotes a 128-bit data type. |
default | The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. |
delegate | A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. |
do | The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. |
double | The double keyword denotes a simple type that stores 64-bit floating-point values. |
else | The if-else statement selects a statement for execution based on the value of a Boolean expression. |
enum | The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. |
event | Specifies an event. |
explicit | The explicit keyword is used to declare an explicit user-defined type conversion operator |
extern | Use the extern modifier in a method declaration to indicate that the method is implemented externally. |
false | In C#, the false keyword can be used as an overloaded operator or as a literal |
finally | The finally block is useful for cleaning up any resources allocated in the try block. |
fixed | Prevents relocation of a variable by the garbage collector. |
float | The float keyword denotes a simple type that stores 32-bit floating-point values. |
for | The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. |
foreach | The foreach statement repeats a group of embedded statements for each element in an array or an object collection. |
goto | The goto statement transfers the program control directly to a labeled statement. |
if | The if statement selects a statement for execution based on the value of a Boolean expression. |
implicit | The implicit keyword is used to declare an implicit user-defined type conversion operator. |
in | The foreach,in statement repeats a group of embedded statements for each element in an array or an object collection. |
int | The int keyword denotes an integral type that stores values according to the size and range shown in the following table. |
interface | An interface defines a contract. A class or struct that implements an interface must adhere to its contract. |
internal | The internal keyword is an access modifier for types and type members. |
is | The is operator is used to check whether the run-time type of an object is compatible with a given type. |
lock | The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. |
long | The long keyword denotes an integral type that stores values according to the size and range shown in the following table. |
namespace | The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally-unique types. |
new | In C#, the new keyword can be used as an operator or as a modifier. |
null | The null keyword is a literal that represents a null reference, one that does not refer to any object. |
object | The object type is an alias for System.Object in the .NET Framework. |
operator | The operator keyword is used to declare an operator in a class or struct declaration. |
out | The out method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method |
override | Use the override modifier to modify a method, a property, an indexer, or an event. |
params | The params keyword lets you specify a method parameter that takes an |
private | The private keyword is a member access modifier. |
protected | The protected keyword is a member access modifier. |
public | The public keyword is an access modifier for types and type members. |
readonly | The readonly keyword is a modifier that you can use on fields. |
ref | The ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. |
return | The return statement terminates execution of the method in which it appears and returns control to the calling method. |
sbyte | The sbyte keyword denotes an integral type that stores values according |
sealed | A sealed class cannot be inherited. |
short | The short keyword denotes an integral data type that stores values |
sizeof | The sizeof operator is used to obtain the size in bytes for a value type. |
stackalloc | Allocates a block of memory on the stack. |
static | Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. |
string | The string type represents a string of Unicode characters. |
struct | A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. |
switch | The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. |
this | The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. |
throw | The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution. |
true | In C#, the true keyword can be used as an overloaded operator or as a |
try | The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. |
typeof | The typeof operator is used to obtain the System.Type object for a type. |
uint | The uint keyword denotes an integral type that stores values according to |
ulong | The ulong keyword denotes an integral type that stores values according |
unchecked | The unchecked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. |
unsafe | The unsafe keyword denotes an unsafe context, which is required for any |
ushort | The ushort keyword denotes an integral data type that stores values |
using | The using keyword has two major uses. |
virtual | The virtual keyword is used to modify a method or property declaration, in which case the method or the property is called a virtual member. |
volatile | The volatile keyword indicates that a field can be modified in the |
void | When used as the return type for a method, void specifies that the method |
while | The while statement executes a statement or a block of statements until a specified expression evaluates to false. |
Monday, September 12, 2011
C# Keywords Table
Table of C# keywords and Descriptions. C# was a language developed by the Microsoft Corporation in the late 1990’s. It is an object oriented language that resembles both C and Java. The C# keywords reflect it’s origins as an off shoot of the C language. The following C# keywords are the building blocks of the programming language. A C# program is constructed by creating a syntactically and procedurally correct program file that can be compiled by a C# compiler.
ASCII Table
ASCII stands for American |
|
|
Reflection Examples [C#]
This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.
Create instance from assembly that is in your project References
The following examples create instances of DateTime class from the System assembly.
[C#]
// create instance of class DateTime
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));
[C#]
// create instance of DateTime, use constructor with parameters (year, month, day)
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),
new object[] { 2008, 7, 4 });
Create instance from dynamically loaded assembly
All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.
[C#]
namespace Test
{
public class Calculator
{
public Calculator() { ... }
private double _number;
public double Number { get { ... } set { ... } }
public void Clear() { ... }
private void DoClear() { ... }
public double Add(double number) { ... }
public static double Pi { ... }
public static double GetPi() { ... }
}
}
Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).
[C#]
// dynamically load assembly from file Test.dll
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");
[C#]
// get type of class Calculator from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");
[C#]
// create instance of class Calculator
object calcInstance = Activator.CreateInstance(calcType);
[C#]
// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");
[C#]
// get value of property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);
[C#]
// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);
[C#]
// get info about static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");
[C#]
// get value of static property: public static double Pi
double piValue = (double)piPropertyInfo.GetValue(null, null);
[C#]
// invoke public instance method: public void Clear()
calcType.InvokeMember("Clear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, null);
[C#]
// invoke private instance method: private void DoClear()
calcType.InvokeMember("DoClear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
[C#]
// invoke public instance method: public double Add(double number)
double value = (double)calcType.InvokeMember("Add",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, new object[] { 20.0 });
[C#]
// invoke public static method: public static double GetPi()
double piValue = (double)calcType.InvokeMember("GetPi",
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, null);
[C#]
// get value of private field: private double _number
double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
Create instance from assembly that is in your project References
The following examples create instances of DateTime class from the System assembly.
[C#]
// create instance of class DateTime
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));
[C#]
// create instance of DateTime, use constructor with parameters (year, month, day)
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),
new object[] { 2008, 7, 4 });
Create instance from dynamically loaded assembly
All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.
[C#]
namespace Test
{
public class Calculator
{
public Calculator() { ... }
private double _number;
public double Number { get { ... } set { ... } }
public void Clear() { ... }
private void DoClear() { ... }
public double Add(double number) { ... }
public static double Pi { ... }
public static double GetPi() { ... }
}
}
Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).
[C#]
// dynamically load assembly from file Test.dll
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");
[C#]
// get type of class Calculator from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");
[C#]
// create instance of class Calculator
object calcInstance = Activator.CreateInstance(calcType);
[C#]
// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");
[C#]
// get value of property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);
[C#]
// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);
[C#]
// get info about static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");
[C#]
// get value of static property: public static double Pi
double piValue = (double)piPropertyInfo.GetValue(null, null);
[C#]
// invoke public instance method: public void Clear()
calcType.InvokeMember("Clear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, null);
[C#]
// invoke private instance method: private void DoClear()
calcType.InvokeMember("DoClear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
[C#]
// invoke public instance method: public double Add(double number)
double value = (double)calcType.InvokeMember("Add",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, new object[] { 20.0 });
[C#]
// invoke public static method: public static double GetPi()
double piValue = (double)calcType.InvokeMember("GetPi",
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, null);
[C#]
// get value of private field: private double _number
double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
Subscribe to:
Posts (Atom)