The best C # data types Tutorial In 2024, In this tutorial you can learn Value Type (Value types),Reference type (Reference types),Pointer type (Pointer types),
In C #, variables are divided into the following types:
Value type variables can be assigned a value. They are derived from the classSystem.ValueType.
Value types directly contain data. Such asint, char, float, they are stored numbers, letters, floating-point number.When you declare anint type, the system allocates memory to store the value.
The following table lists the values available Type C # 2010:
类型 | 描述 | 范围 | 默认值 |
---|---|---|---|
bool | 布尔值 | True 或 False | False |
byte | 8 位无符号整数 | 0 到 255 | 0 |
char | 16 位 Unicode 字符 | U +0000 到 U +ffff | '\0' |
decimal | 128 位精确的十进制值,28-29 有效位数 | (-7.9 x 10 28 到 7.9 x 10 28 ) / 10 0 到 28 | 0.0M |
double | 64 位双精度浮点型 | (+/-)5.0 x 10 -324 到 (+/-)1.7 x 10 308 | 0.0D |
float | 32 位单精度浮点型 | -3.4 x 10 38 到 + 3.4 x 10 38 | 0.0F |
int | 32 位有符号整数类型 | -2,147,483,648 到 2,147,483,647 | 0 |
long | 64 位有符号整数类型 | -923,372,036,854,775,808 到 9,223,372,036,854,775,807 | 0L |
sbyte | 8 位有符号整数类型 | -128 到 127 | 0 |
short | 16 位有符号整数类型 | -32,768 到 32,767 | 0 |
uint | 32 位无符号整数类型 | 0 到 4,294,967,295 | 0 |
ulong | 64 位无符号整数类型 | 0 到 18,446,744,073,709,551,615 | 0 |
ushort | 16 位无符号整数类型 | 0 到 65,535 | 0 |
To get an exact type or a variable size on a particular platform, you can usesizeof method.Expressionsizeof (type)produce storage size in bytes of storage object or type. Here's an example to obtain any type of storage on the machineintSize:
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 does not contain the actual data is stored in a variable, but they contain references to variables.
In other words, they refer to a memory location. When using multiple variables, reference type can point to a memory location. If the data memory location is determined by a change of variable, other variables will automatically reflect this change in values.Built-in reference types:object, dynamic andstring.
Object (Object) type is ageneric C # type system (Common Type System - CTS) in the ultimate base class for all data types. Object is an alias for System.Object class. So the object (Object) type can be assigned to any other type (value types, reference types, pre-defined types or user-defined type) values. However, before you assign a value, you need to type conversion.
When a value type to object type is calledboxing; on the other hand, when an object type to a value type, it is called unboxing.
object obj; obj = 100; // this is boxing
You can store any type of value in the dynamic data type of the variable. These variables are type checking occurs at run time.
Dynamic type declaration syntax:
dynamic <variable_name> = value;
E.g:
dynamic d = 20;
Similar dynamic type of an object type, but the type of inspection object type variable occurs at compile time, dynamic type checking and type of the variable occurs at runtime.
String (String) type allows you toassign any string value to a variable. String (String) type is an alias for System.String class. It is from the object (Object) type derived. Values String (String) type can be distributed in two forms: @ quotes and quotes.
E.g:
String str = "w3cschool.cc";
@ A quoted string:
@ "W3cschool.cc";
C # string in front of the string can be added @ (referred to as "verbatim string") will escape character (\) be treated as ordinary characters, such as:
string str = @"C:\Windows";
Equivalent to:
string str = "C:\\Windows";
@ String can be used in any row, line breaks and indentation spaces are counted in the length of the string.
string str = @"<script type=""text/javascript""> <!-- --> </script>";
User-defined reference types: class, interface or delegate. We will discuss these types in later chapters.
Pointer type variable to store another type of memory addresses. C # and pointers in C or C ++ pointers have the same functionality.
Pointer type declaration syntax:
type * identifier;
E.g:
char * cptr; int * iptr;
We will discuss the type of the pointer in the section "Unsafe Code".