<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                要查看幫助文檔,可以選中相關詞,直接按 F1,就可以啟用幫助文檔了。 # 程序基本結構 ![](https://ws2.sinaimg.cn/large/006tKfTcly1fr1ghetps8j31kw0w0kjm.jpg) # 常用關鍵字 ![](https://box.kancloud.cn/9a73a4a25e41250663672673e184d3a0_658x302.png) # 代碼編寫規范 ![](https://box.kancloud.cn/054d1f23cae82aa4bd2a0ff2b7f19d34_567x362.png) # 命名規范 ![](https://box.kancloud.cn/b62c8b69fd4c6fae7718ad0d270fb0d2_668x298.png) w3c參考教程: https://www.w3cschool.cn/csharp/csharp-data-types.html 以及微軟教程: 快速入門 https://docs.microsoft.com/zh-cn/dotnet/csharp/quick-starts/ # 與 ROR 對比 1. ror 中把 mvc 分開,但 c#里面把三個部分合并,比較混亂; 2. 每個句子都以 “;” 分號結束;命令語句的大小寫非常嚴格; 3. 變量類型在一開始就指定:`int a = 4`; 意思是 a 類型是整形; ROR中,把類型寫在變量后面:`a.to_i = 4`; 4. 在變量后面可以直接跟method,比如`message.ToUpper()`;表示把 message 這個變量里面的字母全部變為大寫(用的是 ToUpper() 這個命令);每個 method 后面都必須跟一個括號,括號里面可以直接寫變量名稱,或者直接不寫; 5. c#不像 ror 一樣,變量改變了,就默認返回最后的變量值。c#中,可以從上到下打印,如上述例子。變量trimmedmessage名字不變,但引用的`message. xxx` 內容變了。但整體程序執行的時候,仍按從上到下打印一遍。 ![](https://box.kancloud.cn/11fd655cd39f1695c625dae32f2be2e9_438x412.png) 6. 一般在自己定義的類里面,寫自己的方法,最后實現的時候,一般在class program(){}這個類當中顯示實現。比如在program里面寫console.writeline(); console.readline()等,這些真正被看到到最終效果。 hello,world # 輸入 hello,world ## 1 方法1: 直接寫 ``` using System namespace HelloApplication { class HelloWorld { static void Main(string[] args) { /* my first c# app*/ Console.WriteLine("Hello amy"); Console.ReadKey(); } } } ``` ## 2 方法2: 用變量寫 ``` using System namespace HelloApplication { static void Main(string[] agrs) { class HelloWorld { /*my first c# app*/ var name = "amy"; //記得字符型變量要用引號 Console.WriteLine("Hello" + name + "!"); Console.ReadKey; } } } ``` ## 3 方法3: 用字符插入 ``` using System namespace HelloApplication { static void Main(string[] agrs) { class Helloworld { var name = "amy"; Console.WriteLine($"Hello {name}!"); //**$和引號之間不能有空格** } } } ``` ## 4 方法4: 用ToUpper()函數 is a method you can invoke on a string, like the name variable. It will return the same string, converted to uppercase. ``` using System namespace HelloApplication { static void Main(string[] agrs) { class Helloworld { var name = "amy"; Console.WriteLine($"Hello {name.ToUpper()} !"); } } } ``` # 輸入字符串集合 https://docs.microsoft.com/zh-cn/dotnet/csharp/how-to/index#strings ``` var names = new List<string> {"amy", "tony", "pony"}; foreach (var name in names) { Console.WriteLine($"Hello {name.ToUpper()}"); } ``` ## 5 空格排版的使用1 ``` string message = "Hello world"; Console.WriteLine($ [ { message} ]); //注意,{}花括號里面的內容,空格多少都不影響,但花括號外面的空格內容對排版有影響。 ``` 顯示結果如下: ![](https://ws2.sinaimg.cn/large/006tNc79ly1fnxs1tctybj305h00vmx0.jpg) ## 6 空格排版的使用2 ``` string message = " hello world "; Console.Write($"[{message}]"); string trimmedmessage = message.TrimStart(); Console.WriteLine($"[{trimmedmessage}]"); trimmedmessage = message.TrimEnd(); Console.WriteLine($"[{trimmedmessage}]"); trimmedmessage = message.Trim(); Console.WriteLine ($"[{trimmedmessage}]"); ``` 得到以下情況: ![](https://ws1.sinaimg.cn/large/006tNc79ly1fnxvrav4ekj307u02oq2x.jpg) > **c#不像 ror 一樣,變量改變了,就默認返回最后的變量值。c#中,可以從上到下打印,如上述例子。變量trimmedmessage名字不變,但引用的message. xxx 內容變了。但整體程序執行的時候,仍按從上到下打印一遍。** ![](https://box.kancloud.cn/11fd655cd39f1695c625dae32f2be2e9_438x412.png) > **在變量后面可以直接加“.xxx”,比如“message.ToUpper()”,可以直接弄成全部大寫。或“message.Replace”可直接替換。“.”點后面的叫“方法 method”。** ## 7 字符替換 ``` string message = "hello"; Console.WriteLine(message); message = message.Replace("hello", "bye"); Console.WriteLine(message); ``` 得到: ![](https://ws3.sinaimg.cn/large/006tNc79ly1fnxvw43m8cj303402p3yd.jpg) ## 8 直接變成全部大寫或全部小寫: ``` string message = "Hello"; Console.WriteLine(message); Console.WriteLine(message.Toupper()); Console.WriteLine(message.ToLower()); ``` 得到: ![](https://ws1.sinaimg.cn/large/006tNc79ly1fnxw1x2levj302t03jt8l.jpg) ## 9 搜索文本 ``` string message = "You say goodbye, and I say hello"; Console.WriteLine(message.Contains("goodbye")); Console.WriteLine(message.Contains("greetings")); ``` 得到: false true 或者StartWith/EndWith: ``` string message = "you say goodbye and I say hello"; Console.WriteLine(message.StartsWith("you")); Console.WriteLine(message.EndsWith("hello")); ``` 得到: ![](https://ws3.sinaimg.cn/large/006tNc79ly1fnxwem2y1mj3038030t8k.jpg) # 3 數學計算 Math.PI https://docs.microsoft.com/zh-cn/dotnet/api/system.math.pi?view=netframework-4.7.1 # 4 分支和循環 ## 10 while循環 while語句 ``` int counter = 0; //需要在一開始就指定數據類型 while (counter < 10 ) //這里不需要分號,用花括號包起來的命令都不需要再分號。 { Console.WriteLine(counter); counter++; } ``` 得到:0-9 一共10個數。 ![](https://box.kancloud.cn/2b82820264105b1f1b9b53eee4aeb738_97x162.png) 與 ror 對比: ror中,`while...end`語句與 end 配套。`if...elsif...end` 語句也需要 end結尾。 ![](https://ws3.sinaimg.cn/large/006tNc79ly1fnxxnth1w0j30al05rt8r.jpg) ## 11 do...while 循環 do...while語句 ``` int counter = 0; do { Console.WriteLine($"Hello World! The counter is {counter}"); counter++; } while (counter < 10); ``` 與前面 while 語句作用一樣。 ## 12 for循環 for 語句 ``` for (int counter = 10; counter < 10; counter++) { Console.WritelLine(counter); } ``` 說明:for 語句第一句是聲明變量 counter 以及初始值;第二句是聲明變量的執行條件,也就是當counter在小于10的時候,這個 for 循環將一直執行;最后一句是執行了一遍以后的iterator,也就是迭代:當執行了一次 for 循環以后,對變量如何操作,本例中,是將變量counter值加一處理。for 的句子之間用分號隔開。 支持,大家已了解 C# 語言中的 if 語句和循環構造。看看能否編寫 C# 代碼,計算 1 到 20 中所有可被 3 整除的整數的總和。 下面提供了一些提示: % 運算符可用于獲取除法運算的余數。 if 語句中的條件可用于判斷是否應將數字計入總和。 for 循環有助于對 1 到 20 中的所有數字重復執行一系列步驟。 ``` int a, sum = 0; for(a = 1; a < 20 ; a++ ) { if (a % 3 == 0) { sum += a; } } Console.WriteLine(sum); ``` 得到: ![](https://ws2.sinaimg.cn/large/006tNc79ly1fny8ya018fj3034029744.jpg) > console.write語句寫在 for 循環外面,能得到一個最終值,如果寫在 for 語句里面,則得到所有過程值: ![](https://ws4.sinaimg.cn/large/006tNc79ly1fny8zj93baj30830e4aae.jpg) ## 13 public 和 private `public class` 意思就是聲明公共類,大家可以存儲和讀取。 `private class` 就是private 是私有的資料(隱藏資料.保密資料) 意思是除了自己以外,沒有人能存取這段資料,除非你設一個窗口讓它存取,否則沒有人能變更這段資料(只適用該類別的成員函數)。 如果不聲明的話,默認的 class 是 internal class。也就是在該專案中是公共類。internal class 只能在本專案中公共,但public class 則可夸專案調用。 ## 14 內插字符串 ``` public class Vegetable { public Vegetable(string name) => Name = name; public string Name { get; } public override string ToString() => Name; } public class Example { public enum Unit { item, pound, ounce, dozen }; public static void Main() { var item = new Vegetable("eggplant"); var date = DateTime.Now; var price = 1.99m; var unit = Unit.item; Console.WriteLine($"On {date}, the price of {item} was {price} per {unit}."); } } ``` # 5 構造函數 在類被實例化的時候調用(即創建類的對象實力的時候),也是類的成員。構造函數名字必須與類的名字一致;沒有返回值;默認構造函數五參數,但也可以設定參數,但要一并把無參數的寫法一起寫進去。 如: ``` class Toy { public Toy(string name) { Console.WriteLine("正在創建{0}玩具." , name); } } ``` 會提示錯誤,因為構造函數默認無參數,也就是括號里面不能有內容。但如果非要有內容,則改成以下: ``` class Toy() { public Toy() {} // 無參數的構造函數,這部分先寫 public Toy(string name) //接下來寫我們需要的帶參數的構造函數,但一旦有參數以后,就必須有返回值 { Console.WriteLine("正在創建{0}玩具.", name); } Toy thetoy = new Toy(" 小騎車"); // 返回值 } ``` 下面實例 ``` using System; namespace MyApp { public class Test { //構造函數,名稱必須與類的名稱相同,所以接下來那句 public Test()必須寫。 public Test() { System.Diagnostics.Debug.WriteLine("構造函數被調用"); } //析構函數,用來銷毀不需要的類實例,以~開頭,沒有返回值 ~Test() { System.Diagnostics.Debug.WriteLine("析構函數被調用"); } } class Program //默認這部分必須寫 { static void Main(string[] args) { Test t = new Test(); } } } ``` # 注意語法中是否有 static。 如果有,則要用string arr 等類后面加點,調用方法,如果沒有,則直接用對象名后面加點調用方法。 ![](https://box.kancloud.cn/7be63120697e1c4a68b9e3e72ac218ab_816x382.png) 1.如果是用到類,則需要類名后面加點。比如: ``` string s1, s2; console.writeline(string.compare(s1,s2)) //在 string 這個類后面加點再調用 compare; ``` 2.如果是用方法,則直接用變量名后面加點,直接跟方法。比如: ``` string s1,s2; s1 = "hello"; s2 = "HELLO" comsole.writeline(s1.CcompareTo(s2)) //要用對象名后面加點; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看