Home [C# 筆記] 複習
Post
Cancel

[C# 筆記] 複習

里氏轉換

  1. 子類可以賦值給父類
    如果有一個方法需要一個父類做為參數,我們可以傳遞一個子類物件。

  2. 如果父類中裝的是子類物件,則可以將這個父類強轉為子類對象。

is as 轉換類型

  • is 成功:true,失敗:false
  • as 成功:返回對應的物件,失敗:null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//1.子類可以賦值給父類
Person p = new Student();

//2.如果父類裝的是子類物件,那麼這個父類就可以強制轉成子類
//is as 轉換類型
//is: 成功:返回對應的物件,失敗:null
Student s2 = p as Student; //
s2.StudentSayHello();

//as:通常父類強轉成子類會先判斷(成功:true,失敗:false)
if (p is Student)
{
    ((Student)p).StudentSayHello();
} else {
    Console.WriteLine("轉換失敗");
}

//父類
public class Person
{
    public void SayHello() {
        Console.WriteLine("我是人類");
    }
}
//子類
public class Student : Person
{
    public void StudentSayHello() {
        Console.WriteLine("我是學生");
    }
}

集合

ArrayList

  • list.add 加入單個元素
  • list.range 加入一個集合
1
2
3
4
5
6
using System.Collections;

ArrayList list = new ArrayList();
list.Add(1); //加入一個元素 
list.AddRange(new int[] { 1, 2, 3, 4, 5 }); //加入一個集合
//Remove RemoveAt RemoveRange Clear Insert InsertRange  Revers Sort

Hashtable

key:value, key是唯一的,不能重複

  • ht.Add(key, value); 加入相同的key, 會拋異常
  • ht[1] = "李四"; 不會拋異常,只會覆蓋原本的值
  • ht.ContainsKey很重要,用來判斷是否包含這個key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.Collections;

Hashtable ht = new Hashtable(); //key:value, key是唯一的,不能重複

ht.Add(1, "張三");
ht.Add(true, "男");
ht.Add(3.14, 5000m);

//ht.Add(1, "123"); //加入相同的key, 會拋異常
ht[1] = "李四"; //不想拋異常,只想覆蓋原本的值

//依據key遍歷hashtable每一個值
foreach (var item in ht.Keys) {
    Console.WriteLine($"key: {item}, vaule: {ht[item]}");
}

var推斷類型

Path

File

Create Delete Copy Move

File 檔案讀寫

File最大的缺點:只能讀小文件

1
2
3
4
5
ReadAllBytes() -> 字串陣列 -> 字串  
Encoding.Default.GetString(字串陣列)  

WriteAllBytes() -> 字串 -> 字串陣列    
Enconding.Default.GetBytes(字串) 

File.ReadAllBytes() 讀檔

將byte[]陣列中的每一個元素,都要按照我們指定的編碼格式解碼成字串
UTF-8、GB2312、GBK、ASCII、Unicode

ReadAllBytes() -> 字串陣列 -> 字串
Encoding.Default.GetString(字串陣列)

1
2
3
4
5
6
7
using System.Text;

byte[] buffer = File.ReadAllBytes(@"C:\Users\rivalin\Desktop\new.txt");
//將byte[]陣列中的每一個元素,都要按照我們指定的編碼格式解碼成字串  
//UTF-8 GB2312 GBK ASCII Unicode 
string s = Encoding.Default.GetString(buffer);
Console.WriteLine(s);

File.WriteAllBytes() 寫檔

沒有這個文件的話,會給你新建一個,有的話,會給你覆蓋掉。

WriteAllBytes() -> 字串 -> 字串陣列
Enconding.Default.GetBytes(字串)

1
2
3
4
5
string s = "今天天氣真好啊";
//需要將寫入的字串轉成byte[]陣列
byte[] buffer = Encoding.Default.GetBytes(s);
File.WriteAllBytes(@"C:\Users\rivalin\Desktop\lala.txt", buffer);
Console.WriteLine("done");

File.ReadAllLines() 以行的形式讀出

1
2
3
4
string[] contents = File.ReadAllLines(@"C:\Users\rivalin\Desktop\new.txt", Encoding.Default);
foreach (string item in contents) {
    Console.WriteLine(item);
}

File.WriteAllLines() 以行的形式寫入

1
2
File.WriteAllLines(@"C:\Users\rivalin\Desktop\new.txt", new string[] { "aoc","aec"});
Console.WriteLine("done");

File.ReadAllText()

1
2
string contents = File.ReadAllText(@"C:\Users\rivalin\Desktop\new.txt", Encoding.Default);
Console.WriteLine(contents);

File.WriteAllText()

1
2
File.WriteAllText(@"C:\Users\rivalin\Desktop\new.txt", "今天天氣真好啊");
Console.WriteLine("done");

File.AppendAllText() 內容不會覆蓋過去

有相同檔名的文件,內容不會覆蓋過去,寫入的文字只會追加在後面

1
2
File.AppendAllText(@"C:\Users\rivalin\Desktop\new.txt", "Append All Text 看我有覆蓋嗎");
Console.WriteLine("done");

R Note

  • ReadAllBytes() 除了可以讀txt檔,要讀讀影音檔、圖片…,就用它

只能讀文本格式

  • ReadAllLine() 可以拿到字串陣列,如果要對每一個元素做處理,就用它
  • ReadText() 只能拿到一個字串
This post is licensed under CC BY 4.0 by the author.