[{"data":1,"prerenderedAt":834},["ShallowReactive",2],{"note-\u002Fnotes\u002Fsync\u002Fcsharp-04":3},{"id":4,"title":5,"body":6,"category":820,"date":821,"description":822,"draft":823,"extension":824,"meta":825,"navigation":132,"path":826,"seo":827,"sourcePath":828,"stem":829,"tags":830,"__hash__":833},"notes\u002Fnotes\u002Fsync\u002Fcsharp-易混淆概念-易混淆概念04.md","易混淆概念04",{"type":7,"value":8,"toc":799},"minimark",[9,13,24,46,50,80,210,213,217,228,237,294,297,307,319,325,347,349,353,356,388,392,467,471,489,492,517,520,543,546,550,586,590,625,632,670,677,737,739,743,751,765,767,770,774,783,786,795],[10,11,12],"h2",{"id":12},"擴充方法",[14,15,16],"blockquote",{},[17,18,19,20],"p",{},"關鍵字 ",[21,22,23],"code",{},"this",[14,25,26],{},[17,27,28,29,32,33,45],{},"在方法參數裡寫 ",[21,30,31],{},"this string str","，只是在向編譯器下指令：",[34,35,36,37,40,41,44],"strong",{},"「請允許工程師把第一個參數 ",[21,38,39],{},"str"," 挪到最前面，用 ",[21,42,43],{},"str.方法名稱()"," 的方式來呼叫它。」"," 它不是物件引用自己，純粹是呼叫語法的左右位置對調而已。",[47,48,49],"h3",{"id":49},"擴充方法的語法規則",[51,52,53,63,72],"ul",{},[54,55,56,57,62],"li",{},"必須放在一個 ",[34,58,59],{},[21,60,61],{},"public static class"," 裡面。",[54,64,65,66,71],{},"方法本身必須是 ",[34,67,68],{},[21,69,70],{},"public static","。",[54,73,74,79],{},[34,75,76,77],{},"第一個參數前加上 ",[21,78,23],{},"，代表這個方法是「擴充給哪一種型別」使用的。",[81,82,87],"pre",{"className":83,"code":84,"language":85,"meta":86,"style":86},"language-csharp shiki shiki-themes github-light github-dark","public static class LogAnalysis\n{\n    public static string SubstringAfter(this string str, string delimiter)\n    {\n        \u002F\u002F 1. 找到分隔符號在字串中的起始索引\n        int index = str.IndexOf(delimiter);\n\n        \u002F\u002F 2. 如果找不到，通常回傳空字串或原字串（視需求而定）\n        if (index == -1)\n        {\n            return string.Empty;\n        }\n\n        \u002F\u002F 3. 起始位置要跳過 delimiter 本身的長度\n        int startIndex = index + delimiter.Length;\n\n        \u002F\u002F 4. 從該位置一路截取到最後\n        return str.Substring(startIndex);\n    }\n}\n","csharp","",[21,88,89,97,103,109,115,121,127,134,140,146,152,158,164,169,175,181,186,192,198,204],{"__ignoreMap":86},[90,91,94],"span",{"class":92,"line":93},"line",1,[90,95,96],{},"public static class LogAnalysis\n",[90,98,100],{"class":92,"line":99},2,[90,101,102],{},"{\n",[90,104,106],{"class":92,"line":105},3,[90,107,108],{},"    public static string SubstringAfter(this string str, string delimiter)\n",[90,110,112],{"class":92,"line":111},4,[90,113,114],{},"    {\n",[90,116,118],{"class":92,"line":117},5,[90,119,120],{},"        \u002F\u002F 1. 找到分隔符號在字串中的起始索引\n",[90,122,124],{"class":92,"line":123},6,[90,125,126],{},"        int index = str.IndexOf(delimiter);\n",[90,128,130],{"class":92,"line":129},7,[90,131,133],{"emptyLinePlaceholder":132},true,"\n",[90,135,137],{"class":92,"line":136},8,[90,138,139],{},"        \u002F\u002F 2. 如果找不到，通常回傳空字串或原字串（視需求而定）\n",[90,141,143],{"class":92,"line":142},9,[90,144,145],{},"        if (index == -1)\n",[90,147,149],{"class":92,"line":148},10,[90,150,151],{},"        {\n",[90,153,155],{"class":92,"line":154},11,[90,156,157],{},"            return string.Empty;\n",[90,159,161],{"class":92,"line":160},12,[90,162,163],{},"        }\n",[90,165,167],{"class":92,"line":166},13,[90,168,133],{"emptyLinePlaceholder":132},[90,170,172],{"class":92,"line":171},14,[90,173,174],{},"        \u002F\u002F 3. 起始位置要跳過 delimiter 本身的長度\n",[90,176,178],{"class":92,"line":177},15,[90,179,180],{},"        int startIndex = index + delimiter.Length;\n",[90,182,184],{"class":92,"line":183},16,[90,185,133],{"emptyLinePlaceholder":132},[90,187,189],{"class":92,"line":188},17,[90,190,191],{},"        \u002F\u002F 4. 從該位置一路截取到最後\n",[90,193,195],{"class":92,"line":194},18,[90,196,197],{},"        return str.Substring(startIndex);\n",[90,199,201],{"class":92,"line":200},19,[90,202,203],{},"    }\n",[90,205,207],{"class":92,"line":206},20,[90,208,209],{},"}\n",[211,212],"hr",{},[10,214,216],{"id":215},"ienumerable-可被走訪可迭代的能力","IEnumerable 可被走訪（可迭代）」的能力",[14,218,219],{},[17,220,221,224,225,71],{},[21,222,223],{},"IEnumerable"," 的意思純粹是：",[34,226,227],{},"「你可以拿放大鏡從頭到尾看過我身上的每一個字元」",[14,229,230],{},[17,231,232,233,236],{},"LINQ 只認 ",[21,234,235],{},"IEnumerable\u003CT>","，不認具體型別。",[51,238,239,252,270,286],{},[54,240,241,244,245,248,249,251],{},[34,242,243],{},"統一介面","：只要能被 ",[21,246,247],{},"foreach"," 走訪（實作了 ",[21,250,223],{},"），就能直接掛上 LINQ。",[54,253,254,257,258,261,262,265,266,269],{},[34,255,256],{},"底層無關","：不論是 ",[21,259,260],{},"string","、",[21,263,264],{},"char[]"," 還是 ",[21,267,268],{},"List","，對 LINQ 來說都是同一種「可迭代的資料流」。",[54,271,272,275,276,261,279,282,283,285],{},[34,273,274],{},"隨插即用","：",[21,277,278],{},".Select()",[21,280,281],{},".Where()"," 等所有擴充方法，本質上都是寫給 ",[21,284,235],{}," 的通用工具箱。",[54,287,288,275,291,293],{},[34,289,290],{},"唯讀瀏覽",[21,292,223],{}," 只提供「唯讀瀏覽」權限",[17,295,296],{},"============",[47,298,300,301,303,304,306],{"id":299},"字串string本身就是一個字元的集合char","字串（",[21,302,260],{},"）本身就是一個字元的集合（",[21,305,264],{},"）",[14,308,309],{},[17,310,311,312,315,316,318],{},"它的底層儲存結構是連續的 ",[21,313,314],{},"char"," 記憶體區塊，但它在型別上不是 ",[21,317,264],{},"，而是一個獨立的物件。",[17,320,321,322,324],{},"因為 ",[21,323,260],{}," 實作了陣列最核心的兩大特徵：",[326,327,328,331],"ol",{},[54,329,330],{},"可以用索引讀取（Indexer）",[54,332,333,334,337,338,340,341,261,344,346],{},"實作了 ",[21,335,336],{},"IEnumerable\u003Cchar>","： 這代表它可以像陣列一樣被 ",[21,339,247],{}," 走訪，也可以直接套用所有 LINQ 方法（如 ",[21,342,343],{},".Count()",[21,345,281],{},"）。",[211,348],{},[10,350,352],{"id":351},"時間用法datetime","時間用法DateTime",[47,354,355],{"id":355},"建構與解析",[51,357,358,370],{},[54,359,360,275,363,366,367,346],{},[34,361,362],{},"手動指定",[21,364,365],{},"new DateTime(year, month, day)","（時分秒預設自動歸零為 ",[21,368,369],{},"00:00:00",[54,371,372,375,376],{},[34,373,374],{},"字串轉時間","：\n",[51,377,378,383],{},[54,379,380],{},[21,381,382],{},"DateTime.Parse(str)",[54,384,385],{},[21,386,387],{},"DateTime.TryParse(str, out var dt)",[47,389,391],{"id":390},"格式化代號大小寫鐵則tostring","格式化代號大小寫鐵則（ToString）",[51,393,394,411,432],{},[54,395,396,399,400,403,404,407,408,71],{},[21,397,398],{},"MM","（大寫）是",[34,401,402],{},"月份","；",[21,405,406],{},"mm","（小寫）是",[34,409,410],{},"分鐘",[54,412,413,416,417,403,420,423,424,427,428,431],{},[21,414,415],{},"HH","（大寫）是 ",[34,418,419],{},"24 小時制",[21,421,422],{},"hh","（小寫）是 ",[34,425,426],{},"12 小時制","（搭配 ",[21,429,430],{},"tt"," 顯示 AM\u002FPM）。",[54,433,434,435,261,438,261,441,444,445,448,449,452,453,261,455,261,458,444,460,448,463,466],{},"單碼 ",[21,436,437],{},"M",[21,439,440],{},"d",[21,442,443],{},"h"," ",[34,446,447],{},"不補零","（例如 ",[21,450,451],{},"7"," 月）；雙碼 ",[21,454,398],{},[21,456,457],{},"dd",[21,459,422],{},[34,461,462],{},"自動補零",[21,464,465],{},"07"," 月）。",[47,468,470],{"id":469},"物件不可變性immutable","物件不可變性（Immutable）",[51,472,473,479],{},[54,474,475,476,346],{},"時間不能直接修改屬性（不能 ",[21,477,478],{},"dt.Year = 2026",[54,480,481,482,485,486,346],{},"加減時間必須使用方法並",[34,483,484],{},"接收回傳的新物件","（例如：",[21,487,488],{},"dt = dt.AddDays(1)",[47,490,491],{"id":491},"時間比對與運算",[51,493,494,501],{},[54,495,496,497,500],{},"比較先後直接用運算子：",[21,498,499],{},"appointment \u003C DateTime.Now","（是否已過期）。",[54,502,503,504,485,509,512,513,516],{},"兩個時間相減會得到 ",[34,505,506],{},[21,507,508],{},"TimeSpan",[21,510,511],{},"TimeSpan diff = date2 - date1;","，可用 ",[21,514,515],{},"diff.TotalDays"," 取得天數差）。",[47,518,519],{"id":519},"歸零與常用屬性",[51,521,522,530],{},[54,523,524,527,528,71],{},[21,525,526],{},".Date","：快速取得當天午夜 ",[21,529,369],{},[54,531,532,535,536,539,540,346],{},[21,533,534],{},".Hour","：內部永遠以 ",[34,537,538],{},"0 ~ 23 的整數"," 儲存，判斷上下午或營業時段最方便（例如 ",[21,541,542],{},"dt.Hour >= 12",[47,544,545],{"id":545},"常用方法",[547,548,549],"h4",{"id":549},"靜態屬性與取得當前時間",[51,551,552,558,564,576],{},[54,553,554,557],{},[21,555,556],{},"DateTime.Now","：目前的當地時間（含時分秒）。",[54,559,560,563],{},[21,561,562],{},"DateTime.UtcNow","：目前的 UTC 標準時間（伺服器\u002F跨時區常用）。",[54,565,566,569,570,572,573,346],{},[21,567,568],{},"DateTime.Today","：今天的午夜零點 ",[21,571,369],{},"（相當於 ",[21,574,575],{},"DateTime.Now.Date",[54,577,578,581,582,585],{},[21,579,580],{},"DateTime.MinValue"," \u002F ",[21,583,584],{},"DateTime.MaxValue","：C# 支援的最小時間（0001\u002F1\u002F1）與最大時間（9999\u002F12\u002F31）。",[547,587,589],{"id":588},"解析與字串轉換靜態方法","解析與字串轉換（靜態方法）",[51,591,592,597,607,613,619],{},[54,593,594,596],{},[21,595,382],{},"：字串轉時間（格式不對會拋出例外）。",[54,598,599,602,603,606],{},[21,600,601],{},"DateTime.TryParse(str, out dt)","：安全轉換，回傳 ",[21,604,605],{},"bool"," 代表成功與否。",[54,608,609,612],{},[21,610,611],{},"DateTime.ParseExact(str, format, ...)","：指定嚴格格式進行解析。",[54,614,615,618],{},[21,616,617],{},"DateTime.DaysInMonth(year, month)","：查詢某年某月一共有幾天（自動算閏年）。",[54,620,621,624],{},[21,622,623],{},"DateTime.IsLeapYear(year)","：檢查是否為閏年。",[547,626,628,629,306],{"id":627},"時間加減與操作物件實例方法-dtxxx","時間加減與操作（物件實例方法 ",[21,630,631],{},"dt.XXX()",[51,633,634,646,658,664],{},[54,635,636,581,639,581,642,645],{},[21,637,638],{},"dt.AddYears(n)",[21,640,641],{},"dt.AddMonths(n)",[21,643,644],{},"dt.AddDays(n)","：加減年月日。",[54,647,648,581,651,581,654,657],{},[21,649,650],{},"dt.AddHours(n)",[21,652,653],{},"dt.AddMinutes(n)",[21,655,656],{},"dt.AddSeconds(n)","：加減時分秒。",[54,659,660,663],{},[21,661,662],{},"dt.Add(timeSpan)","：直接加上一段自訂時間長度。",[54,665,666,669],{},[21,667,668],{},"dt.ToString(\"格式\")","：轉成自訂格式字串。",[547,671,673,674,306],{"id":672},"提取內部數值物件實例屬性-dtxxx","提取內部數值（物件實例屬性 ",[21,675,676],{},"dt.XXX",[51,678,679,691,706,715,721,729],{},[54,680,681,261,684,261,687,690],{},[21,682,683],{},"dt.Year",[21,685,686],{},"dt.Month",[21,688,689],{},"dt.Day","：取得整數的年月日。",[54,692,693,261,696,261,699,261,702,705],{},[21,694,695],{},"dt.Hour",[21,697,698],{},"dt.Minute",[21,700,701],{},"dt.Second",[21,703,704],{},"dt.Millisecond","：取得整數的時分秒毫秒。",[54,707,708,711,712,346],{},[21,709,710],{},"dt.DayOfWeek","：取得星期幾（回傳列舉，如 ",[21,713,714],{},"DayOfWeek.Monday",[54,716,717,720],{},[21,718,719],{},"dt.DayOfYear","：這一天是當年的第幾天（1 ~ 366）。",[54,722,723,726,727,71],{},[21,724,725],{},"dt.Date","：保留年月日，時分秒全部歸零為 ",[21,728,369],{},[54,730,731,734,735,346],{},[21,732,733],{},"dt.TimeOfDay","：單獨取出當天的時間段（回傳 ",[21,736,508],{},[211,738],{},[10,740,742],{"id":741},"dictionary跟hashset","Dictionary跟HashSet",[14,744,745],{},[17,746,747,748],{},"把它們想成是同一種底層技術的「雙胞胎」，差別只在於 ",[34,749,750],{},"有沒有附帶 Value",[14,752,753],{},[17,754,755,764],{},[34,756,757,760,761],{},[21,758,759],{},"HashSet\u003CT>"," 本質上就是一個「Value 為空（不需要附帶資料）」的特化版 ",[21,762,763],{},"Dictionary","，因為省去了存 Value 的空間，它比 Dictionary 更省記憶體且更輕量。",[211,766],{},[10,768,769],{"id":769},"寫法",[47,771,773],{"id":772},"陣列初始設定式array-initializer","陣列初始設定式（Array Initializer）",[81,775,777],{"className":83,"code":776,"language":85,"meta":86,"style":86},"string[] words = { \"apple\", \"banana\", \"apple\", \"orange\", \"banana\", \"apple\" };\n",[21,778,779],{"__ignoreMap":86},[90,780,781],{"class":92,"line":93},[90,782,776],{},[17,784,785],{},"以下是標準寫法：",[81,787,789],{"className":83,"code":788,"language":85,"meta":86,"style":86},"string[] words = new string[] { \"apple\", \"banana\", \"apple\" };\n",[21,790,791],{"__ignoreMap":86},[90,792,793],{"class":92,"line":93},[90,794,788],{},[796,797,798],"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":86,"searchDepth":99,"depth":99,"links":800},[801,804,808,816,817],{"id":12,"depth":99,"text":12,"children":802},[803],{"id":49,"depth":105,"text":49},{"id":215,"depth":99,"text":216,"children":805},[806],{"id":299,"depth":105,"text":807},"字串（string）本身就是一個字元的集合（char[]）",{"id":351,"depth":99,"text":352,"children":809},[810,811,812,813,814,815],{"id":355,"depth":105,"text":355},{"id":390,"depth":105,"text":391},{"id":469,"depth":105,"text":470},{"id":491,"depth":105,"text":491},{"id":519,"depth":105,"text":519},{"id":545,"depth":105,"text":545},{"id":741,"depth":99,"text":742},{"id":769,"depth":99,"text":769,"children":818},[819],{"id":772,"depth":105,"text":773},"backend","2026-08-24","關鍵字 `this`",false,"md",{},"\u002Fnotes\u002Fsync\u002Fcsharp-04",{"title":5,"description":822},"C#\u002F易混淆概念\u002F易混淆概念04.md","notes\u002Fsync\u002Fcsharp-易混淆概念-易混淆概念04",[831,832],"C#","Concepts","tJ4kph3J8xTWctsXASN2w5nOXtMe_9gei_VPmq0ITiI",1788767747447]