0% found this document useful (0 votes)
156 views2 pages

Understanding Runtime Type Identification in C#

Runtime type identification (RTTI) allows determining an object's type at runtime. RTTI is useful when you want to discover the type of an object referred to by a base class reference or to test if a cast will succeed before attempting it. C# provides three keywords that support RTTI: the "is" operator checks if an expression's type is the same as or compatible with a specified type.

Uploaded by

Deepak Garg
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views2 pages

Understanding Runtime Type Identification in C#

Runtime type identification (RTTI) allows determining an object's type at runtime. RTTI is useful when you want to discover the type of an object referred to by a base class reference or to test if a cast will succeed before attempting it. C# provides three keywords that support RTTI: the "is" operator checks if an expression's type is the same as or compatible with a specified type.

Uploaded by

Deepak Garg
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Runtime Type Identification (RTTI):

Runtime type identification (RTTI) allows the type of an object to be determined during program execution.
RTTI useful when
 You want to discover what type of object is being referred to by a base class reference.
 To test in advance whether a cast will succeed, preventing an invalid cast exception.

C# provide three keywords that supports RTTI

1) is operator
expr is type
expr is an expression whose type is begin tested against type. If the type of expr is same as type or compatible
with type, then the outcome is true means expr can be cast to type otherwise, it is false.

Type is a set of values from which a variable, constant, function or other expression may take its value.

Strong Type is that in which you must declare the type of each object that you create and complier will
help you prevent bugs by enforcing that only data of right type is assigned to those objects.

Data Types:

1. Intrinsic (Built in)


2. User Defined
3. Value Type
4. Reference Type

Value Type holds its actual value in memory allocated on stack

The address of a Reference Type variable sits on the stack whereas actual object is stored the on heap

All intrinsic types are value type except object and string. All user-defined types are reference type
except struct

Stack
A stack is a data structure used to stored items on a last in first out basis.
The stack refers to area of memory supported by processor on which the local variables are stored. In c#,
value types are allocated on the stack – an area of memory is set aside for their value, and this area is
referred by the name of variable.

Reference types are allocated on the heap. When an object is allocated on the heap its address is returned
and that address is assigned to a reference.

The garbage collector destroys objects on the stack sometimes after the stack frame they are declared
within ends. Typically a stack frame is defined by a function. Thus if you declare a local variable within
a function the object will be marked for garbage collection when the function ends. Objects are garbage
collected sometime after the final reference to them is destroyed.

Constant is a variable whose value can’t be changed


1. Literal constant
Example x=32;
2. Symbolic constant assign a name to constant value.
Syntax const type identifier=value;
Example Const int FreezingPoint=23;
3. Enumeration
It provides powerful alternatives to constants. An enumeration is a distinct value type consisting of a set
of named constant called enumerator list

Syntax
[attributes][modifier] enum idenfier
[:base type ]{enumerator list}

The default value for base type is integer but we can use any integral type beside CHAR

You might also like