毛豆 dplayerd@gmail.com --- Copy / Del / Md / Rd 等等 ```C# static void Main(string[] args) { int a = 90; int b = 80; int c = a + b; Console.WriteLine(c.ToString("0000")); string AAA = "HIHI"; Console.WriteLine(c + AAA); Console.ReadKey(); } ``` ## String Formater ```C# Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{a}+ {b} = {c}"); string inputText = Console.ReadLine(); Console.WriteLine(inputText); ``` ![](https://i.imgur.com/AV0LGvg.png) ## Catch & throw ```C# public static void Method2() { try { Method1(200, 100); } catch (Exception ex) { // Log to file throw; } } public static void Method1(int a, int b) { if (a > 50 || b > 40) throw new Exception(); } ``` --- # 下午 ## MyMath Class 模組 & 呼叫相同專案的模組 ```C# class MyMath { public static int Add(int a, int b) { return a + b; } } ``` ```C# class Program { static void Main(string[] args) { Console.WriteLine("App 6"); int result = MyMath.Add(5, 6); Console.WriteLine("5 + 6 =" + result); Console.ReadLine(); } } ``` ```C# class Class1 { public void GOGOGO() { MyMath.Add(1, 2); } } ``` ## 類別庫 Class Library ```C# public class MathCore { public static int Add(int a, int b) { return a + b; } } ``` 開放程度 ```c# private : 自己可以存取 protected : 繼承自己的類別可以存取 internal : 同專案都可以存取 public : 可以跨專案存取 ``` ### Debug & Release & #if ```C# #if DEBUG Console.WriteLine(1); #endif #if RELEASE Console.WriteLine(1); #endif ``` ## File ### ReadFile ```C# static void Main(string[] args) { string path = "F:\\CSharp\\"; string fileName = "AAA.txt"; if (!System.IO.File.Exists(path + fileName)) { Console.WriteLine($"{path}{fileName} doesn't exist;"); Console.ReadLine(); return; } string content = System.IO.File.ReadAllText(path + fileName); Console.WriteLine(content); Console.ReadLine(); } ``` ```C# class TestFileReader { /// <summary> /// 讀取檔案,不存在時回傳空字串 /// </summary> /// <param name="path">檔案路徑</param> /// <returns></returns> public static string ReadFile(string path) { if (!File.Exists(path)) { Console.WriteLine($"{path} doesn't exist;"); Console.ReadLine(); return string.Empty; } string content = File.ReadAllText(path); return content; } } public static void WriteFile(string path, string content) { if (!File.Exists(path)) { Console.WriteLine($"{path} doesn't exist;"); Console.ReadLine(); return; } File.WriteAllText(path, content); } public static void Append(string path, string content) { if (!File.Exists(path)) { Console.WriteLine($"{path} doesn't exist;"); Console.ReadLine(); return; } string cTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); content = System.Environment.NewLine + "[" + cTime + "]" + content; File.AppendAllText(path, content); } ``` ```C# class TestFolderReader { public static void ListAllFile(string path) { if (!Directory.Exists(path)) { Console.WriteLine($"{path} doesn't exist;"); Console.ReadLine(); return; } string[] folderItems = Directory.GetFileSystemEntries(path); foreach (string item in folderItems) { Console.WriteLine(item); } } public static void CreateFolder(string path) { if (Directory.Exists(path)) return; Directory.CreateDirectory(path); } public static void DeleteFolder(string path) { if (!Directory.Exists(path)) return; Directory.Delete(path); } } ``` ```C# static void Main(string[] args) { string path = "F:\\CSharp\\"; //string path = "Fake\\"; string fileName = "AAA.txt"; // Read file string content = TestFileReader.ReadFile(path + fileName); // Append File string content2 = "Hello world!!"; TestFileReader.Append(path + fileName, content2); TestFolderReader.ListAllFile(path); TestFolderReader.CreateFolder(path + "Temp"); TestFolderReader.DeleteFolder(path + "Temp"); string newPath = "F:\\CSharp\\aaa.txt"; // "F:\\CSharp\\aaa.txt" Console.WriteLine(System.IO.Path.Combine("F:\\CSharp", "aaa.txt")); //F:\\CSharp Console.WriteLine(System.IO.Path.GetDirectoryName(newPath)); // aaa.txt Console.WriteLine(System.IO.Path.GetFileName(newPath)); Console.ReadLine(); } ``` # 2021/3/7 上午 ## Measure Speed ```C# class SleepHelper { /// <summary> 暫停程式數秒鐘 </summary> /// <param name="second"></param> public static void SleepAWhile(int second) { int ms = second * 1000; System.Threading.Thread.Sleep(ms); } } class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); Console.WriteLine("Program start."); sw.Start(); SleepHelper.SleepAWhile(3); sw.Stop(); sw.Start(); SleepHelper.SleepAWhile(6); sw.Stop(); Console.WriteLine("Program End. Total use " + sw.ElapsedMilliseconds); Console.ReadLine(); } } ``` ## sql SELECT *, Some column INSERT DELETE UPDATE WHERE BETWEEN EQUALS NOT IN >= ORDER BY ASC, DESC SQL 伺服器定序:Chinese_Taiwan_Stroke_CI_AS