Unger Technologies LLC


Type Basics

A type defines the blueprint for a value. A value is a storage location denoted by a variable (if it can change) or a constant (if it cannot).

All values in C# are an instance of a specific type. The meaning of a value, and the set of possible values a variable can have, is determined by its type.

The Type class represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.

Type is the root of the System.Reflection functionality and is the primary way to access metadata. 

Use the members of Type to get information about a type declaration, about the members of a type (such as the constructors, methods, fields, properties, and events of a class), as well as the module and the assembly in which the class is deployed.

No permissions are required for code to use reflection to get information about types and their members, regardless of their access levels. No permissions are required for code to use reflection to access public members, or other members whose access levels would make them visible during normal compilation. However, in order for your code to use reflection to access members that would normally be inaccessible, such as private or internal methods, or protected fields of a type your class does not inherit, your code must have ReflectionPermission. See Security Considerations for Reflection.

Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection, all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.


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 keywords are contextual, meaning they can also be used as identifiers—without an @ symbol.

These are:

addDefines a custom event accessor that is invoked when client code subscribes to the event.
asyncIndicates that the modified method, lambda expression, or anonymous method is asynchronous.
awaitSuspends an async method until an awaited task is completed.
dynamicDefines a reference type that enables operations in which it occurs to bypass compile-time type checking.
getDefines an accessor method for a property or an indexer.
globalSpecifies the default global namespace, which is otherwise unnamed.
partialDefines partial classes, structs, and interfaces throughout the same compilation unit.
removeDefines a custom event accessor that is invoked when client code unsubscribes from the event.
setDefines an accessor method for a property or an indexer.
valueUsed to set accessors and to add or remove event handlers.
varEnables the type of a variable declared at method scope to be determined by the compiler.
whereAdds constraints to a generic declaration. (See also where).
yieldUsed in an iterator block to return a value to the enumerator object or to signal the end of iteration.

With contextual keywords, ambiguity cannot arise within the context in which they are used.

The Future of C#