內置值類型:
| C# 類型關鍵字 | .NET 類型 |
| --- | --- |
| [`bool`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/bool) | [System.Boolean](https://learn.microsoft.com/zh-cn/dotnet/api/system.boolean) |
| [`byte`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.Byte](https://learn.microsoft.com/zh-cn/dotnet/api/system.byte) |
| [`sbyte`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.SByte](https://learn.microsoft.com/zh-cn/dotnet/api/system.sbyte) |
| [`char`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/char) | [System.Char](https://learn.microsoft.com/zh-cn/dotnet/api/system.char) |
| [`decimal`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | [System.Decimal](https://learn.microsoft.com/zh-cn/dotnet/api/system.decimal) |
| [`double`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | [System.Double](https://learn.microsoft.com/zh-cn/dotnet/api/system.double) |
| [`float`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types) | [System.Single](https://learn.microsoft.com/zh-cn/dotnet/api/system.single) |
| [`int`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.Int32](https://learn.microsoft.com/zh-cn/dotnet/api/system.int32) |
| [`uint`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.UInt32](https://learn.microsoft.com/zh-cn/dotnet/api/system.uint32) |
| [`nint`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.IntPtr](https://learn.microsoft.com/zh-cn/dotnet/api/system.intptr) |
| [`nuint`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.UIntPtr](https://learn.microsoft.com/zh-cn/dotnet/api/system.uintptr) |
| [`long`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.Int64](https://learn.microsoft.com/zh-cn/dotnet/api/system.int64) |
| [`ulong`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.UInt64](https://learn.microsoft.com/zh-cn/dotnet/api/system.uint64) |
| [`short`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.Int16](https://learn.microsoft.com/zh-cn/dotnet/api/system.int16) |
| [`ushort`](https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) | [System.UInt16](https://learn.microsoft.com/zh-cn/dotnet/api/system.uint16) |
| 類型 | 描述 | 范圍 | 默認值 |
| --- | --- | --- | --- |
| bool | 布爾值 | True 或 False | False |
| byte | 8 位無符號整數 | 0 到 255 | 0 |
| ushort | 16 位無符號整數類型 | 0 到 65,535 | 0 |
| uint | 32 位無符號整數類型 | 0 到 4,294,967,295 | 0 |
| unint | 無符號32位 或 64位整數 | | 0 |
| nint | 有符號32位或 64 位整數 | | 0 |
| ulong | 64 位無符號整數類型 | 0 到 18,446,744,073,709,551,615 | 0 |
| sbyte | 8 位有符號整數類型 | \-128 到 127 | 0 |
| short | 16 位有符號整數類型 | \-32,768 到 32,767 | 0 |
| 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 |
| char | 16 位 Unicode 字符 | U +0000 到 U +ffff | '\\0' |
| float | 32 位單精度浮點型 | \-3.4 x 10^38到 + 3.4 x 10^38 | 0.0F |
| double | 64 位雙精度浮點型 | (+/-)5.0 x 10^-324到 (+/-)1.7 x 10^308 | 0.0D |
| decimal | 128 位精確的十進制值,28-29 有效位數 | (-7.9 x 10^28到 7.9 x 10^28) / 100 到 28 | 0.0M |
int和Integer(默認值:null)還不一樣?
long 和Long (默認值:null)還不一樣?
```
float price1=12f;//不加f則默認為double
double price2=12d//d可以省略
decimal price3=12m
```
```
public class PlacementPoint
{
public string Name; //成員變量
}
//Name是public成員變量,其它類對象可以直接讀寫它
但問題是,1.這樣做沒有封裝性,沒有數據安全性;2.并且有時我們是想定義只能讀或者只能寫的成員;甚至可能在寫(賦值)的時候加點其它判斷邏輯啊。
于是引出了屬性property,它有兩個方法,一個是get讀方法,一個是set寫方法
//早期版本的屬性語法
public class PlacementPoint
{
private string _name; //私有成員
//_name對應的屬性定義
public string Name{
get {
return _name;
}
set{
//此處前后還可以做點其它邏輯判斷
_name = value; //value是C#屬性關鍵字,代表賦值時右邊的新值
}
}
}
我就想定義一個簡單的屬性而已,寫的代碼是不是多了點,要先定義一個私有成員變量,再定義對應的set和get方法,如果一個類有多個這樣的屬性,代碼就千篇一律,垃圾代碼,于是C#又作出了改進,新語法中,上面的代碼被簡化成了:
public class PlacementPoint
{
public string Name { get; set; }
}
//C#編譯器會幫你做代碼擴展編譯成之前的那種代碼
如果我只想定義只讀成員:
public class StandResponse
{
public string Code{get;}
}
```
如需得到一個類型或一個變量在特定平臺上的準確尺寸,可以使用?sizeof?方法。表達式?sizeof(type)?產生以字節為單位存儲對象或類型的存儲尺寸。下面舉例獲取任何機器上?int?類型的存儲尺寸:
namespace?DataTypeApplication
{
???class?Program
???{
??????static?void?Main(string[]?args)
??????{
?????????Console.WriteLine("Size?of?int:?{0}",?sizeof(int));
?????????Console.ReadLine();
??????}
???}
}
- Visual Studio 2022安裝到非C盤
- .net平臺區別
- 常用單詞
- 關鍵字
- 操作符(運算符)
- 標識符(命名規范)
- 開始
- 變量
- 常量
- 數據類型
- 值類型
- 變量數據類型
- 枚舉類型enum(常量集合)
- 結構類型struct(結構體)
- 元組類型
- 可null類型(T?)
- 引用類型
- 數組(array)
- 集合(List)
- 內置引用類型
- object
- string
- Dynamic(動態類型)
- delegate委托(代理)類型
- 自定義引用類型
- 接口(interface)
- 類class
- record(定義一個引用類型)
- 指針類型(僅用于非安全代碼)
- get和set訪問器
- delegate委托
- delegate實現發布訂閱與事件
- 類型轉換
- 合并操作符??
- 類相關
- Partial 部分類
- 類定義以及訪問修飾符(封裝)
- abstract抽象類與sealed密封類
- virtual虛方法
- 接口interface
- C# 預處理器指令
- C#技術棧
- 判斷(流程控制)與三元運算
- if
- switch
- 三元運算
- 循環
- while 循環
- for循環
- foreach循環
- do...while 循環
- 文件操作
- 其他
- 多開