May 11, 2021 C#
There are several types of variables in C#:
Value type variables can be assigned directly to a value. They are derived from the class System.ValueType.
The value type contains the data directly. F or example, int, char, float, they store numbers, letters, floats. When you declare an int type, the system allocates memory to store values.
The following table lists the types of values available in C# 2010:
type | describe | Scope | Defaults |
---|---|---|---|
bool | Boolean value | True or False | False |
byte | 8-bit unsigned integer | 0 to 255 | 0 |
char | 16 Unicode characters | U +0000 to U + FFFF | '' |
decimal | 128 accurate decimal values, 28-29 effective digits | (-7.9 x 10 28 To 7.9 x 10 28 ) / 10 0 to 28 | 0.0M |
double | 64-bit double precision floating point type | (+/-)5.0 x 10 -324 To (+/-) 1.7 x 10 308 | 0.0D |
float | 32-bit single precision floating point type | -3.4 x 10 38 To + 3.4 x 10 38 | 0.0F |
int | 32-bit symbol integer type | -2, 147, 483, 648 to 2, 147, 483, 647 | 0 |
long | 64-bit symbol integer type | -923, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807 | 0L |
sbyte | 8-bit symbol integer type | -128 to 127 | 0 |
short | 16-bit symbol integer type | -32, 768 to 32,767 | 0 |
uint | 32-bit unsigned integer type | 0 to 4,294,967,295 | 0 |
ulong | 64-bit unsigned integer type | 0 to 18, 446, 744, 073, 709, 551, 615 | 0 |
ushort | 16-bit unsigned integer type | 0 to 65, 535 | 0 |
To get the exact size of a type or variable on a particular platform, you can use the sizeof method. T he expression sizeof(type) produces storage dimensions that store objects or types in bytes. Here are some examples to get storage dimensions of type int 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 types do not contain actual data stored in variables, but they do contain references to variables.
In other words, they refer to a memory location. W hen using multiple variables, the reference type can point to a memory location. I f the data for the memory location is changed by one variable, the other variables automatically reflect the change in this value. The built-in reference types are: object, dynamic, and string.
Object type is the ultimate base class for all data types in the Common Type System - CTS. O bject is an alias for the System.Object class. S o object types can be assigned values for any other type (value type, reference type, predefined type, or user-defined type). However, before you can assign a value, you need to make a type conversion.
When a value type is converted to an object type, it is called boxing, and when an object type is converted to a value type, it is called unboxing.
object obj;
obj = 100; // 这是装箱
Boxing: Value types are converted to object types, for example:
int val = 8;
object obj = val;//整型数据转换为了对象类型(装箱)
Unboxing: The object type that was previously converted from the value type is then turned back to the value type, for example:
int val = 8;
object obj = val;//先装箱
int nval = (int)obj;//再拆箱
Note: Only boxed data can be unboxed.
You can store any type of value in a dynamic data type variable. The type check for these variables occurs at runtime.
Syntax for declaring dynamic types:
dynamic <variable_name> = value;
For example:
dynamic d = 20;
Dynamic types are similar to object types, but type checks for object type variables occur at compile time, and type checks for dynamic type variables occur at runtime.
String type allows you to assign any string value to a variable. T he String type is an alias for the System.String class. I t is derived from the Object type. Values of the String type can be assigned in two forms: quotation marks and .
For example:
String str = "w3cschool.cn";
A string of quotes:
@"w3cschool.cn";
You can treat the escaped character as a normal character before the C#string with a hashtag (called a verbat fewer than a word string), such as:
string str = @"C:\Windows";
Equivalent to:
string str = "C:\\Windows";
Any line break in the string, line breaks, and indented spaces are calculated within the length of the string.
string str = @"<script type=""text/javascript"">
<!-- -->
</script>";
User-defined reference types are class, interface, or delegate. We'll discuss these types in a later section.
Pointer type variables store another type of memory address. The pointer in C# has the same function as the pointer in C or C.
The syntax of declaring pointer types:
type* identifier;
For example:
char* cptr;
int* iptr;
We'll discuss pointer types in the section "Unsafe Code."