out參數
- out參數側重于在函數中返回多個值
- out參數要求必須在方法的內部為其賦值
static void Main(string[] args)
{
int n;
string s;
bool b = Test(out n,out s);
Console.Write(b);
Console.Write(n);
Console.Write(s);
Console.ReadKey();
}
static bool Test(out int number, out string res)
{
number=10;
res = "張三";
return true;
}
ref參數
- ref參數側重于把一個值帶到函數中進行改變,再將改變的值帶出函數
- ref參數在函數內不用賦值,函數外必須為ref參數賦值
static void Main(string[] args)
{
int n1 = 10;
int n2 = 10;
Change(ref n1,ref n2);
Console.Write("{0}---{1}",n1,n2);
Console.ReadKey();
}
static bool Change(ref int n1, ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
List泛型集合
- Count:獲取集合中實際包含的元素的個數
- Capcity:集合中可以包含的元素的個數
- Add:添加單個元素
- AddRange:添加集合
- Remove:移除最先匹配到的元素
- RemoveAll:移除所有符合條件的元素
- RemoveAt:根據索引移除元素
- RemoveRange:移除一個集合
- Insert:根據索引插入元素
- InsertRange:根據索引插入一個集合
- ToArray():集合轉換成數組
- ToList():數組轉換成集合
- 集合初始化器
List<string> list1 = new List<string>() { "a", "b", "c" };
Dictionary鍵值對集合
- 鍵值對集合中的鍵必須是唯一的,值是可以重復的
- 可以給鍵值對集合中的某個值進行重新賦值
- ContainsKey():判斷集合中是否已經包含某個鍵
- 使用foreach循環,通過遍歷鍵值對的形式對鍵值對集合進行遍歷
//第一種遍歷方式
foreach (string item in dict.Keys)
{
Console.WriteLine("鍵--{0},值--{1}", item, dic[item]);
}
//第二種遍歷方式
foreach(KeyValuePair<string, string> kv in dic)
{
Console.WriteLine("鍵--{0},值--{1}", kv.Key, kv.Value);
}
常用類庫之File類
常用方法
- Exist():判斷指定文件是否存在
- Create():創建
- Move():剪切
- Copy():復制
- Delete():刪除
- ReadAllLines():讀取文件所有行
- ReadAllText():讀取文件所有文本
常用類庫之Directory類
- CreateDirectory():創建一個新的文件夾
- Delete():刪除
- Move():剪切
- Exist():判斷指定文件夾是否存在
- GetFiles():獲得指定目錄下所有文件的全路徑
- GetDirectories():獲得當前目錄下的所有文件夾路徑
正則表達式
常用的3種情況
- 判斷是否匹配:Regex.IsMatch("字符串","正則表達式");
- 字符串提取:Regex.Match("字符串","要提取的字符串的正則表達式");(提取一次)
- 字符串提取(循環提取所有):Regex.Matches();(可以提取所有匹配的字符串)
- 字符串替換:Regex.Replace("字符串","正則","替換內容");