[{"data":1,"prerenderedAt":503},["ShallowReactive",2],{"note-\u002Fnotes\u002Fsync\u002Fcsharp-c":3},{"id":4,"title":5,"body":6,"category":490,"date":491,"description":492,"draft":493,"extension":494,"meta":495,"navigation":90,"path":496,"seo":497,"sourcePath":498,"stem":499,"tags":500,"__hash__":502},"notes\u002Fnotes\u002Fsync\u002Fcsharp-c-基本型別.md","C# 基本型別",{"type":7,"value":8,"toc":472},"minimark",[9,13,18,22,41,45,50,126,134,138,143,147,152,156,179,182,187,190,194,199,203,208,217,247,272,279,282,286,328,356,402,407,409,413,417,429,432,435,438,441,448,451,454,457,460,463,466,468],[10,11,12],"h2",{"id":12},"整數和浮點數",[14,15,17],"h3",{"id":16},"整數-int","整數 int",[14,19,21],{"id":20},"雙精度浮點數-double","雙精度浮點數 double",[23,24,25],"blockquote",{},[26,27,28,32,33,36,37,40],"p",{},[29,30,31],"code",{},"浮點數","適合表示大小可非常大或非常小的非整數\n",[29,34,35],{},"雙精確度"," 是一個相對詞彙，描述用來儲存值的二進位數位數。雙精確度 數位的二進位數是 單精度的兩倍。\n",[29,38,39],{},"單精度"," 數是透過關鍵字 float 來宣告的",[14,42,44],{"id":43},"十進位類型-decimal","十進位類型 decimal",[23,46,47],{},[26,48,49],{},"decimal 類型的範圍較小，但精確度大於 double",[51,52,57],"pre",{"className":53,"code":54,"language":55,"meta":56,"style":56},"language-csharp shiki shiki-themes github-light github-dark","double a = 1.0;\ndouble b = 3.0;\nConsole.WriteLine(a \u002F b);\n### 0.3333333333333333\n\n\n\ndecimal c = 1.0M;\ndecimal d = 3.0M;\nConsole.WriteLine(c \u002F d);\n### 0.3333333333333333333333333333\n","csharp","",[29,58,59,67,73,79,85,92,97,102,108,114,120],{"__ignoreMap":56},[60,61,64],"span",{"class":62,"line":63},"line",1,[60,65,66],{},"double a = 1.0;\n",[60,68,70],{"class":62,"line":69},2,[60,71,72],{},"double b = 3.0;\n",[60,74,76],{"class":62,"line":75},3,[60,77,78],{},"Console.WriteLine(a \u002F b);\n",[60,80,82],{"class":62,"line":81},4,[60,83,84],{},"### 0.3333333333333333\n",[60,86,88],{"class":62,"line":87},5,[60,89,91],{"emptyLinePlaceholder":90},true,"\n",[60,93,95],{"class":62,"line":94},6,[60,96,91],{"emptyLinePlaceholder":90},[60,98,100],{"class":62,"line":99},7,[60,101,91],{"emptyLinePlaceholder":90},[60,103,105],{"class":62,"line":104},8,[60,106,107],{},"decimal c = 1.0M;\n",[60,109,111],{"class":62,"line":110},9,[60,112,113],{},"decimal d = 3.0M;\n",[60,115,117],{"class":62,"line":116},10,[60,118,119],{},"Console.WriteLine(c \u002F d);\n",[60,121,123],{"class":62,"line":122},11,[60,124,125],{},"### 0.3333333333333333333333333333\n",[51,127,132],{"className":128,"code":130,"language":131},[129],"language-text","M字母是在關鍵字double和decimal之間，從視覺上看最為顯眼的字母。\n","text",[29,133,130],{"__ignoreMap":56},[14,135,137],{"id":136},"短整數-short","短整數 short",[23,139,140],{},[26,141,142],{},"Stack 佔用大小：2 位元組 (16 bits)\n容量範圍：$-32,768$ 到 $32,767$",[14,144,146],{"id":145},"長整數-long","長整數 long",[23,148,149],{},[26,150,151],{},"Stack 佔用大小：8 位元組 (64 bits) ── 體積是 int 的雙倍！\n容量範圍：約正負 900 兆（天文數字）",[14,153,155],{"id":154},"無符號整數-unsigned","無符號整數 Unsigned",[157,158,159],"ul",{},[160,161,162,163],"li",{},"在上述的名字前面加上一個 u，代表 Unsigned（無符號，純正數）。\n",[157,164,165],{},[160,166,167,168],{},"它們物理體積不變，但因為放棄了負數，所以正數容量直接翻倍：\n",[157,169,170,173,176],{},[160,171,172],{},"ushort：2 位元組，範圍變成 $0$ 到 $65,535$。",[160,174,175],{},"uint：4 位元組，範圍變成 $0$ 到約 42 億。",[160,177,178],{},"ulong：8 位元組，範圍變成 $0$ 到約 1800 兆。",[14,180,181],{"id":181},"byte",[23,183,184],{},[26,185,186],{},"最小的整數\n只佔 1 位元組 (8 bits)，只能存 $0$ 到 $255$。",[188,189],"hr",{},[10,191,193],{"id":192},"list","List",[23,195,196],{},[26,197,198],{},"是一個標準的參考型別",[14,200,202],{"id":201},"與csharp自身的array差異","與Csharp自身的Array差異",[23,204,205],{},[26,206,207],{},"原生 Array 的讀寫速度比 List 快了約 15% 到 30%。",[23,209,210],{},[26,211,212,213],{},"在「底層硬體與生態系」上，它們的地位完全不重疊。因為 List",[214,215,216],"t",{}," 本質上只是個「寄生蟲」，它的內部肉身，百分之百是靠原生 Array 撐起來的！",[51,218,220],{"className":53,"code":219,"language":55,"meta":56,"style":56},"public class List\u003CT>\n{\n    internal T[] _items; \u002F\u002F 👈 抓到了！骨子裡就是一個最普通的原生陣列！\n    internal int _size;  \u002F\u002F 記錄目前真正裝了幾個格子\n}\n",[29,221,222,227,232,237,242],{"__ignoreMap":56},[60,223,224],{"class":62,"line":63},[60,225,226],{},"public class List\u003CT>\n",[60,228,229],{"class":62,"line":69},[60,230,231],{},"{\n",[60,233,234],{"class":62,"line":75},[60,235,236],{},"    internal T[] _items; \u002F\u002F 👈 抓到了！骨子裡就是一個最普通的原生陣列！\n",[60,238,239],{"class":62,"line":81},[60,240,241],{},"    internal int _size;  \u002F\u002F 記錄目前真正裝了幾個格子\n",[60,243,244],{"class":62,"line":87},[60,245,246],{},"}\n",[157,248,249],{},[160,250,251,252],{},"假設宣告 var myList = new List",[253,254,255,256],"int",{},"();\n",[157,257,258,265],{},[160,259,260,261,264],{},"宿主出生：List 在 Heap 大倉庫裡悄悄 new int",[60,262,263],{},"4"," 蓋了一個長度只有 4 的原生 Array（叫底層陣列）。",[160,266,267,268,271],{},"動態擴容（Resize）：當你塞滿 4 個格子，想塞第 5 個時，List 會在後台發動大絕招──在 Heap 另一個地方 new int",[60,269,270],{},"8"," 蓋一個雙倍大的新原生 Array，把舊的 4 個資料影印過去，然後把舊的 Array 丟給 GC 丟掉。",[26,273,274],{},[275,276],"img",{"alt":277,"src":278},"image","https:\u002F\u002Fhackmd.io\u002F_uploads\u002FSyHAe9fgMl.png",[26,280,281],{},"========",[14,283,285],{"id":284},"跟js的array非常類似","跟JS的Array非常類似",[157,287,288,312],{},[160,289,290,291],{},"強型別的「單一品種限制」（最核心的差別）",[157,292,293,300],{},[160,294,295,296,299],{},"JS 的 Array（自由散漫）：\n一個陣列裡可以同時塞字串、數字、甚至是物件：",[60,297,298],{},"\"Alice\", 30, { isOk: true }","。因為 JS 骨子裡沒有強型別鐵律。",[160,301,302,303],{},"C# 的 List",[214,304,305,306],{},"（基因純正）：\n你必須在角括號 ",[214,307,308,309],{}," 裡死死指定這個櫃子只能裝什麼。一旦宣告 List",[253,310,311],{},"，如果你敢把 \"Alice\" 塞進去，編譯器在編譯那一秒就會直接物理性亮紅燈報警！這保證了櫃子裡所有資料的形狀與大小完全一致。",[160,313,314,315],{},"底層的「連續圈地大作戰」",[157,316,317,320],{},[160,318,319],{},"JS 的 Array（骨子裡其實是雜湊表\u002F物件）：JS 的陣列在記憶體裡可能是不連續的（Sparse Array），它是靠 Key-Value 的對照表去模擬陣列。",[160,321,302,322],{},[214,323,324,325],{},"（骨子裡包著一個實體靜態 Array）：\n這就是最有趣的硬體真相。當你 new List",[253,326,327],{},"() 時，C# 其實是在 Heap 大倉庫裡，悄悄圈了一塊絕對連續、死板的「實體靜態陣列 (Array)」空間（預設容量通常是 4）。",[51,329,331],{"className":53,"code":330,"language":55,"meta":56,"style":56},"List\u003Cstring> names = [\"\u003Cname>\", \"Ana\", \"Felipe\"];\nforeach (var name in names)\n{\n    Console.WriteLine($\"Hello {name.ToUpper()}!\");\n}\n",[29,332,333,338,343,347,352],{"__ignoreMap":56},[60,334,335],{"class":62,"line":63},[60,336,337],{},"List\u003Cstring> names = [\"\u003Cname>\", \"Ana\", \"Felipe\"];\n",[60,339,340],{"class":62,"line":69},[60,341,342],{},"foreach (var name in names)\n",[60,344,345],{"class":62,"line":75},[60,346,231],{},[60,348,349],{"class":62,"line":81},[60,350,351],{},"    Console.WriteLine($\"Hello {name.ToUpper()}!\");\n",[60,353,354],{"class":62,"line":87},[60,355,246],{},[51,357,359],{"className":53,"code":358,"language":55,"meta":56,"style":56},"    \nConsole.WriteLine();\nnames.Add(\"Maria\");\nnames.Add(\"Bill\");\nnames.Remove(\"Ana\");\nforeach (var name in names)\n{\n    Console.WriteLine($\"Hello {name.ToUpper()}!\");\n}\n    \n",[29,360,361,366,371,376,381,386,390,394,398],{"__ignoreMap":56},[60,362,363],{"class":62,"line":63},[60,364,365],{},"    \n",[60,367,368],{"class":62,"line":69},[60,369,370],{},"Console.WriteLine();\n",[60,372,373],{"class":62,"line":75},[60,374,375],{},"names.Add(\"Maria\");\n",[60,377,378],{"class":62,"line":81},[60,379,380],{},"names.Add(\"Bill\");\n",[60,382,383],{"class":62,"line":87},[60,384,385],{},"names.Remove(\"Ana\");\n",[60,387,388],{"class":62,"line":94},[60,389,342],{},[60,391,392],{"class":62,"line":99},[60,393,231],{},[60,395,396],{"class":62,"line":104},[60,397,351],{},[60,399,400],{"class":62,"line":110},[60,401,246],{},[26,403,404],{},[275,405],{"alt":277,"src":406},"https:\u002F\u002Fhackmd.io\u002F_uploads\u002FSJjg40bgGg.png",[188,408],{},[10,410,412],{"id":411},"dictionary","Dictionary",[14,414,416],{"id":415},"dictionary-的-key","Dictionary 的 Key",[23,418,419],{},[26,420,421,422,425,426],{},"要在 C# 裡面有資格當 Dictionary 的 Key，在物理和邏輯上必須通過兩大硬性考驗",[29,423,424],{},"Equals","跟",[29,427,428],{},"GetHashCode",[26,430,431],{},"🎟️ 通行證一：Equals（它必須能精準比對「我們一不一樣」）\n字典在找 Key 的時候，就像管理員在核對身分。",[26,433,434],{},"如果 Key 是參考型別（物件），電腦預設比對的是「記憶體位址」。如果用物件當 Key，你兩次宣告 new Person(\"Alice\") 就算內容一樣，因為在 Stack 上的位址不同，字典就會判定它們是不同的 Key。",[26,436,437],{},"但 Tuple 是值型別！C# 官方在設計 ValueTuple 時，特別幫它寫好了物理規則：「只要裡面的每一個元素內容完全相同，它們在邏輯上就是完全相等的東西。」",[26,439,440],{},"你的 target 宣告為 (1, 3)。",[26,442,443,444,447],{},"字典裡本來就存了一個 ",[60,445,446],{},"(1, 3)","。",[26,449,450],{},"當 TryGetValue 啟動時，電腦拿著 target 去跟字典裡的 Key 比對。電腦會一格一格檢查：1 == 1（對）、3 == 3（對）。管理員蓋章認證：「完全一致！」，所以能精準找到對應的 \"Sensor A\"。",[26,452,453],{},"🎟️ 通行證二：GetHashCode（它能算出專屬的「分類雜湊值」）\n這是字典物理效能極快的核心祕密。字典內部不是一條線慢慢排隊找 Key 的，而是像郵局一樣，有很多個「分類信箱（Buckets）」。",[26,455,456],{},"當你把一個 Key 丟進去，電腦會對這個 Key 執行一個數學公式，算出一個整數，叫做 HashCode（雜湊值）。這個數字決定了這個 Key 該去哪一個信箱。",[26,458,459],{},"以前大家喜歡用 string 當 Key，因為字串算出來的 HashCode 很穩定。",[26,461,462],{},"微軟在設計 ValueTuple 時，也幫它內建了超厲害的算式：它會把 Row (1) 和 Column (3) 的記憶體二進位訊號混在一起，算出一串專屬於 (1, 3) 的唯一 HashCode 數字。",[26,464,465],{},"因為只要內容是 (1, 3)，算出來的 HashCode 永遠一模一樣，所以 TryGetValue 就能以極速、點對點地直接衝到正確的信箱裡把資料拔出來。",[188,467],{},[469,470,471],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":56,"searchDepth":69,"depth":69,"links":473},[474,483,487],{"id":12,"depth":69,"text":12,"children":475},[476,477,478,479,480,481,482],{"id":16,"depth":75,"text":17},{"id":20,"depth":75,"text":21},{"id":43,"depth":75,"text":44},{"id":136,"depth":75,"text":137},{"id":145,"depth":75,"text":146},{"id":154,"depth":75,"text":155},{"id":181,"depth":75,"text":181},{"id":192,"depth":69,"text":193,"children":484},[485,486],{"id":201,"depth":75,"text":202},{"id":284,"depth":75,"text":285},{"id":411,"depth":69,"text":412,"children":488},[489],{"id":415,"depth":75,"text":416},"backend","2026-07-18","`浮點數`適合表示大小可非常大或非常小的非整數",false,"md",{},"\u002Fnotes\u002Fsync\u002Fcsharp-c",{"title":5,"description":492},"C#\u002FC# 基本型別.md","notes\u002Fsync\u002Fcsharp-c-基本型別",[501],"C#","arKU5-Kp_21O0TluP2FM1rYCEy5Pg8-sGgq6wqkQTjw",1788767747626]