要查看幫助文檔,可以選中相關詞,直接按 F1,就可以啟用幫助文檔了。
# 程序基本結構

# 常用關鍵字

# 代碼編寫規范

# 命名規范

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` 內容變了。但整體程序執行的時候,仍按從上到下打印一遍。

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} ]); //注意,{}花括號里面的內容,空格多少都不影響,但花括號外面的空格內容對排版有影響。
```
顯示結果如下:

## 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}]");
```
得到以下情況:

> **c#不像 ror 一樣,變量改變了,就默認返回最后的變量值。c#中,可以從上到下打印,如上述例子。變量trimmedmessage名字不變,但引用的message. xxx 內容變了。但整體程序執行的時候,仍按從上到下打印一遍。**

> **在變量后面可以直接加“.xxx”,比如“message.ToUpper()”,可以直接弄成全部大寫。或“message.Replace”可直接替換。“.”點后面的叫“方法 method”。**
## 7 字符替換
```
string message = "hello";
Console.WriteLine(message);
message = message.Replace("hello", "bye");
Console.WriteLine(message);
```
得到:

## 8 直接變成全部大寫或全部小寫:
```
string message = "Hello";
Console.WriteLine(message);
Console.WriteLine(message.Toupper());
Console.WriteLine(message.ToLower());
```
得到:

## 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"));
```
得到:

# 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個數。

與 ror 對比:
ror中,`while...end`語句與 end 配套。`if...elsif...end`
語句也需要 end結尾。

## 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);
```
得到:

> console.write語句寫在 for 循環外面,能得到一個最終值,如果寫在 for 語句里面,則得到所有過程值:

## 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 等類后面加點,調用方法,如果沒有,則直接用對象名后面加點調用方法。

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)) //要用對象名后面加點;
```
- 幫助文檔 microsoft helo viewer
- c#開發環境及visual studio安裝注意事項
- c#程序基本結構-基本語法
- Q1: public static void main(String[] args) 是什么意思
- Q2: c#命名空間+Main方法
- Q3:注釋+命名規則+代碼規則
- Q4: c#語句 system => console
- Q5: 數據類型 .net
- Q5: 常用名字、變量、運算符
- Q6: 對話窗輸入-屬性
- Q7: 遞歸
- Q8:決策分支、條件判斷語句 if 語句
- Q9:數組
- Q10:字符串
- Q11:對象、類、訪問權限、靜態動態函數
- Q12:方法及參數——繼承于類
- Q13:構造函數
- Q14:繼承——base 關鍵字
- Q15:多態、虛方法、接口
- Q16:創建窗體應用、控件
- Q17:Ado數據訪問、連接 sqlserver 數據庫
- Q18: 讀取數據command + DataRead( )、DataSet + DateAdapter
- Q19: Entity Framwork、entity 與 ADO.net的區別
- Q20: 對話框、文件、文件夾
- Q21: 導入excel數據、更新到 dbo 數據庫中
- Q26: 獲取 excel 中每個 sheet 的表名
- Q22: 兩個窗體之間數據+方法傳遞
- Q23: 數學對象
- Q24: c#網站編寫
- Q25: visual studio2017如何查看幫助
- Q27: c# dictionary 字典對象
- Q28: 數組與dataTable互相轉化