Monday, 5 January 2015

What is Value type? Reference type?


Value Type:
   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.


 int Result;
 result=200;
valuetype-parameter-flowchart
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.
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.
C# Type Conversion Methods:
C# provides the following built-in type conversion methods:

S.NMethods & Description
1ToBoolean
Converts a type to a Boolean value, where possible.
2ToByte
Converts a type to a byte.
3ToChar
Converts a type to a single Unicode character, where possible.
4ToDateTime
Converts a type (integer or string type) to date-time structures.
5ToDecimal
Converts a floating point or integer type to a decimal type.
6ToDouble
Converts a type to a double type.
7ToInt16
Converts a type to a 16-bit integer.
8ToInt32
Converts a type to a 32-bit integer.
9ToInt64
Converts a type to a 64-bit integer.
10ToSbyte
Converts a type to a signed byte type.
11ToSingle
Converts a type to a small floating point number.
12ToString
Converts a type to a string.
13ToType
Converts a type to a specified type.
14ToUInt16
Converts a type to an unsigned int type.
15ToUInt32
Converts a type to an unsigned long type.
16ToUInt64
Converts a type to an unsigned big integer.

No comments:

Post a Comment