C # Data types
In C #, variables are divided into the following types:
Value type (types)
Reference type (Reference types)
Pointer type (Pointer types)
Value type (types)
Value type variables can be assigned directly to a value. They are derived from the class System.ValueType.
Value types directly contain data. such as int, char, float, respectively, they store numbers, letters, floating point number. When you declare an int type, the system allocates memory to store the value.
The following table lists the value types available in C # 2010:
To get the exact size of a type or a variable on a particular platform, you can use the sizeof method. The expression sizeof (type) produces storage dimensions that store objects or types in bytes. The following example gets the storage size of the int type on any machine:
Namespace Datatypeapplication
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Size of int: {0}", sizeof (int));
Console.ReadLine ();
}
}
}
When the above code is compiled and executed, it produces the following results:
Size of Int:4
Reference type (Reference types)
Reference types do not contain the actual data stored in variables, but they contain references to variables.
In other words, they refer to a memory location. When you use multiple variables, the reference type can point to a memory location. If the data in the memory location is changed by one variable, the other variables will automatically reflect this value change. The built-in reference types are: object, dynamic, and string.
Object type
The object type is the ultimate base class for all data types in the C # Common type system (Common type system-cts). Object is an alias for the System.Object class. Therefore, the object type can be assigned to any other type (value type, reference type, predefined type, or user-defined type) value. However, before assigning a value, you need to convert the type first.
When a value type is converted to an object type, it is called boxing; on the other hand, when an object type is converted to a value type, it is called unboxing.
Object obj;
obj = 100; This is a boxing
Dynamic type
You can store any type of value in a Dynamic Data type variable. The type checking of these variables occurs at run time.
Syntax for declaring a dynamic type:
Dynamic <variable_name> = value;
For example:
Dynamic d = 20;
Dynamic types are similar to object types, but type checking of object type variables occurs at compile time, while type checking for dynamic type variables occurs at run time.
String type
The string type allows you to assign any string value to a variable. The string type is an alias for the System.String class. It is derived from the object type. Values of string types can be assigned in two forms: quotation marks and @ quotes.
For example:
String str = "runoob.com";
An @ Quote string:
@ "runoob.com";
C # string strings can be preceded by an @ (called "verbatim string") to treat the escape character (\) as a normal character, such as:
String str = @ "C:\Windows";
Equivalent to:
String str = "C:\\Windows";
The @ string can be arbitrarily wrapped, line breaks, and indent spaces are counted within the string length.
String str = @ "<script type=" "Text/javascript" ">
<!--
-
</script> ";
User-defined reference types are: class, interface, or delegate. We will discuss these types in a later section.
Pointer type (Pointer types)
A pointer type variable stores another type of memory address. Pointers in C # have the same functionality as pointers in C or C + +.
syntax for declaring pointer types:
type* identifier;
For example:
char* cptr;
int* iptr;
C # Data types