Csharp Data Types
# C# Data Types
In C#, variables are categorized into the following types:
* Value types
* Reference types
* Pointer types
## Value types
Value type variables can be assigned a value directly. They are derived from the class **System.ValueType**.
Value types directly contain data. For example, **int, char, and float**, which store numbers, characters, and floating-point numbers, respectively. When you declare an **int** type, the system allocates memory to store the value.
The following table lists the available value types in C# 2010:
| Type | Description | Range | Default Value |
| --- | --- | --- | --- |
| bool | Boolean value | True or False | False |
| byte | 8-bit unsigned integer | 0 to 255 | 0 |
| char | 16-bit Unicode character | U +0000 to U +ffff | '' |
| decimal | 128-bit precise decimal value, 28-29 significant 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 signed integer type | -2,147,483,648 to 2,147,483,647 | 0 |
| long | 64-bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
| sbyte | 8-bit signed integer type | -128 to 127 | 0 |
| short | 16-bit signed 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 a variable on a specific platform, you can use the **sizeof** method. The expression _sizeof(type)_ produces the storage size of the object or type in bytes. The following example retrieves the storage size of the _int_ type on any machine:
## Example
```csharp
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 result:
Size of int: 4
## Reference types
Reference types do not contain the actual data stored in a variable, but they contain a reference to the variable.
In other words, they refer to a memory location. When multiple variables are used, reference types can point to the same memory location. If the data in the memory location is changed by one variable, other variables 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 of all data types in C#'s Common Type System (CTS). Object is an alias for the System.Object class. Therefore, the Object type can be assigned a value of any other type (value types, reference types, predefined types, or user-defined types). However, before assigning a value, a type conversion is required.
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**.
```csharp
object obj;
obj = 100; // This is boxing
### Dynamic type
You can store any type of value in a dynamic data type variable. Type checking for these variables happens at runtime.
The syntax for declaring a dynamic type is:
```csharp
dynamic = value;
For example:
```csharp
dynamic d = 20;
Dynamic types are similar to object types, but type checking for object type variables happens at compile time, while type checking for dynamic type variables happens at runtime.
### 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. The value of a String type can be assigned in two forms: quoted and verbatim quoted.
For example:
```csharp
String str = ".com";
A verbatim quoted string:
```csharp
@".com";
A @ symbol can be placed before a C# string (called a "verbatim string") to treat escape characters () as ordinary characters, for example:
```csharp
string str = @"C:Windows";
is equivalent to:
```csharp
string str = "C:Windows";
In @ strings, you can have line breaks, and the line breaks and indentation spaces are counted as part of the string length.
```csharp
string str = @" ";
User-defined reference types include: class, interface, or delegate. We will discuss these types in later chapters.
## Pointer types
Pointer type variables store the memory address of another type. Pointers in C# have the same functionality as pointers in C or C++.
The syntax for declaring a pointer type is:
```csharp
type* identifier;
For example:
```csharp
char* cptr;
int* iptr;
We will discuss pointer types in the chapter "Unsafe Codes".
## 8 Notes Write a Note
1. #0 Jennis
jen***tian@gmail.com [](#)443 ## About Boxing and Unboxing
Boxing: Converting a value type to an object type, example:
```csharp
int val = 8;
object obj = val; // Integer data converted to object type (boxing)
```
Unboxing: Converting an object type (previously converted from a value type) back to a value type, example:
```csharp
int val = 8;
object obj = val; // First boxing
int nval = (int)obj; // Then unboxing
```
Only data that has been boxed can be unboxed.
(#)Jennis
jen***tian@gmail.com 9 years ago (2017-03-06)
2. #0 smoker
934***908@qq.com [](#)237 **Relationship between obj and int**
```csharp
using System;
namespace RectangleApplication
{
class ExecuteRectangle
{
static void Main(string[] args)
{
int a=9;
object obj;
obj = a;
obj =10;
Console.WriteLine("2: {0}", a); // Output: 2: 9
Console.WriteLine("1: {0}", obj); // Output: 1: 10
Console.ReadLine();
}
}
}
```
Setting the value `int a=9; obj=a;` When `obj` changes, it does not change `int a`. The object only copies the value of `int a` and operates on it. It does not affect the original value of `int`.
(javascript:;)smoker
934***908@qq.com 9 years ago (2017-11-12)
3. #0 assistant
404***626@qq.com
(https://www.cnblogs.com/ShaYeBlog/p/3210355.html) [](#)206 **Difference between String and string in C#**
`string` is a class in C#, `String` is a class in .NET Framework (it won't show blue in C# IDE). C# `string` maps to .NET Framework's `String`. If you use `string`, the compiler will compile it into `String`, so using `String` directly can save the compiler a little work.
If using C#, it is recommended to use `string`, which is more standard. `string` always represents `System.String` (1.x) or `::System.String` (2.0). `String` only represents `System.String` when preceded by `using System;` and when there is no type named `String` (class, struct, delegate, enum) in the current namespace. `string` is a keyword, `String` is not, meaning `string` cannot be used as the name of a class, struct, enum, field, variable, method, or property, while `String` can.
`String` is a CLR type name (also considered a keyword), while `string` is a keyword in C#. When `string` is compiled, the C# compiler will by default convert it to `String`, which adds a few lines of conversion code. Often, it is recommended to use the CLR type rather than the C# type (this is an expert's suggestion). For example, when using `int`, it's better to use `Int32`, etc. Often, it's a matter of habit and convention. Another difference is the color in VS: `String` is green, `string` is blue.
(#)assistant
404***626@qq.com
(https://www.cnblogs.com/ShaYeBlog/p/3210355.html) 8 years ago (2018-02-26)
4. #0 code beginner
dre***w@qq.com
(https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/value-types) [](#)80
**Characteristics of Value Types:**
* 1. A new type cannot be derived from a value type, but a struct can implement interfaces;
* 2. Value types cannot contain null values;
* 3. Each value type has an implicit default constructor that initializes the type to its default value.
(#)
code beginner
dre***w@qq.com
(https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/value-types)
8 years ago (2018-03-05)
5. #0 pray
117***7443@qq.com [](#)185 Each value type has its own independent memory area to store its value. When you call it, you call its value. Reference types, however, call the memory address. For example, defining a reference type `a1=10`, at this time the memory stores 10. When you assign `a1` to `a2`, both refer to the same memory space, and `a2`'s value is saved as `a1`'s value. When you change `a2` to 20, because `a1` and `a2` refer to the same memory, `a1` also becomes 20. This is a reference type. For a value type, when you assign `a1` to `a2`, a new space is allocated for `a2` to save the value of `a1`. When you change `a2` to 20, it saves 20 in `a2`'s space, which has nothing to do with `a1`.
(javascript:;)pray
117***7443@qq.com 8 years ago (2018-03-12)
6. #0 Exception
Exc***ion@q
YouTip