C# data type
In C#, variables are divided into the following types:
Value type
Reference type
Pointer type
Value type
A value type variable can be assigned directly to a value. They are derived from the class System.ValueType
.
The value type contains data directly, such as int
、 char
、 float
that store numbers, characters, and floating-point numbers, respectively when you declare a int
type, the system allocates memory to store values.
The following table lists the value types available in C# 2010:
To get the exact size of a type or variable on a particular platform, you can use the sizeof
method. Expression of sizeof(type)
produces a storage size of an object or type stored in bytes. The following example is used to get any machine int
storage size of the type:
Example
using System;
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 do not contain the actual data stored in the variable, but they do contain references to the variable.
In other words, they refer to a memory location. When using multiple variables, the reference type can point to a memory location. If the data in the memory location is changed by one variable, other variables will automatically reflect this change in value. 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 (CTS). Object is System.Object
alias for the class. So object types can be assigned values of any other type (value type, reference type, predefined type, or user-defined type). However, you need todo a type conversion before assigning a value.
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 packing
Dynamic type
You can store values of any type in dynamic data type variables. The type checking of these variables occurs at run time.
The syntax for declaring dynamic types:
dynamic <variable_name> = value;
For example:
dynamic d = 20;
Dynamic types are similar to object types, but type checking for object type variables occurs at compile time, while type checking for dynamic type variables occurs at run time.
String type
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 type String can be assigned in two forms: quotation marks and @ quotation marks.
For example:
String str = "runoob.com";
An @ quotation mark string:
@"runoob.com";
The C# string string can be preceded by @ (called “verbatim string”) to treat the escape character () as a normal character, such as:
string str = @"C:\Windows";
Equivalent to:
string str = "C:\\Windows";
@
can wrap any line in a string, and newline characters and indented spaces are counted within the length of the string.
string str = @"<script type=""text/javascript"">
<!--
-->
</script>";
User-defined reference types are: class
、 interface
or delegate
. We will discuss these types in later chapters.
Pointer type
Pointer type variables store another type of memory address. Pointers in C# have the same function as pointers in C or C++.
Syntax for declaring pointer types:
type* identifier;
For example:
char* cptr;
int* iptr;
We will discuss pointer types in the section “unsafe Code”.