A value type variable directly contains data in the memory.
Value type variables can be assigned a value directly. They are derived from the class System.ValueType
Reference Type:
A Reference type variable contains memory address of value.
In other words, they refer to a memory location. Using more than one variable, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
Example of built-in reference types are:object, dynamic and string.
Consider the following graph to make better sense of value type and reference type.
Value type variables can be assigned a value directly. They are derived from the class System.ValueType
Reference Type:
A Reference type variable contains memory address of value.
In other words, they refer to a memory location. Using more than one variable, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.
Example of built-in reference types are:object, dynamic and string.
Consider the following graph to make better sense of value type and reference type.
int Result;
result=200;
result=200;
In the preceding example, the value type variable contains the value whereas a reference type variable contains the address of Result variable.
Object type
The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. So object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.
object obj;
obj = 100; // this is boxing
When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.
Dynamic type
You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time.
Syntax for declaring a dynamic type is:
dynamic <variable_name> = value;
For example,
dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables take place at run time.
string type
The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class. It is derived from object type. The value for a string type can be assigned using string literals in two forms: quoted and @quoted.
Type conversion is basically type casting or converting one type of data to another type.
For example,
String str = "Tutorials Point";
A @quoted string literal looks like:
@"Tutorials Point";
The user-defined reference types are: class, interface, or delegate. We will discuss these types in later.
Type conversion is basically type casting or converting one type of data to another type.
In C#, type casting has two forms:
Implicit type conversion - these conversions are performed by C# in a type-safe manner. Examples are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion - these conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
Implicit type conversion - these conversions are performed by C# in a type-safe manner. Examples are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion - these conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
C# Type Conversion Methods:
C# provides the following built-in type conversion methods:
| S.N | Methods & Description |
|---|---|
| 1 | ToBoolean Converts a type to a Boolean value, where possible. |
| 2 | ToByte Converts a type to a byte. |
| 3 | ToChar Converts a type to a single Unicode character, where possible. |
| 4 | ToDateTime Converts a type (integer or string type) to date-time structures. |
| 5 | ToDecimal Converts a floating point or integer type to a decimal type. |
| 6 | ToDouble Converts a type to a double type. |
| 7 | ToInt16 Converts a type to a 16-bit integer. |
| 8 | ToInt32 Converts a type to a 32-bit integer. |
| 9 | ToInt64 Converts a type to a 64-bit integer. |
| 10 | ToSbyte Converts a type to a signed byte type. |
| 11 | ToSingle Converts a type to a small floating point number. |
| 12 | ToString Converts a type to a string. |
| 13 | ToType Converts a type to a specified type. |
| 14 | ToUInt16 Converts a type to an unsigned int type. |
| 15 | ToUInt32 Converts a type to an unsigned long type. |
| 16 | ToUInt64 Converts a type to an unsigned big integer. |
No comments:
Post a Comment