看板 C_Sharp 關於我們 聯絡資訊
哦喔,沒注意到你前一篇文章 但沒有看到整段程式,也沒辦法判斷為什麼要這麼做耶 這邊的this就是指class 所以加了會去找class內的變數,而不是使用function內的變數 如果你只是想看差別? 那我稍微這樣改一下 public class DemoVarible { int count = 100; string s = "類別變數"; private void button1_Click(object sender, EventArgs e) { Console.WriteLine("s={0},count={1}", this.s, this.count); // 這邊把this.拿掉就error int count = 200; string s = "區域變數"; this.count += 1; Console.WriteLine("s={0},count={1}", s, count); Console.WriteLine("s={0},count={1}", this.s, this.count); } } 這是你要的嗎? -- \孩子,人生是沒有存檔的/   ( ′_ゝ`)    (゚д゚ )< SAVE選項在哪裡? ˍ(__つ / ̄ ̄ ̄ /ˍˍ(__つ/ ̄ ̄ ̄/ˍ    \/    /   \/    / -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.187.23.199
bernachom:謝謝您,我研究一下,感謝 05/18 12:25
bernachom:不好意思,想再詢問一下 05/18 17:34
bernachom:我看書上寫的this是說,用來區別參數與私有欄位 05/18 17:34
bernachom:可是我還是不太了解,那為什麼要取一樣的變數名稱? 05/18 17:34
bernachom:是有什麼情況變數名一定會一樣呢@@? 05/18 17:35
bernachom:非使用一樣的變數名稱不可?....謝謝您了 05/18 17:35
bernachom:我剛才想到的理由是說,私有欄位和參數取一樣的名字 05/18 17:47
bernachom:比較容易知道,這個方法傳入什麼參數... 05/18 17:48
bernachom:不知道這樣子是否正確@@?謝謝 05/18 17:49
andymai:個人覺得並沒有非用一樣的名稱不可~程式是你在寫的~自己看 05/18 18:05
andymai:得快、看得懂最重要~不必拘泥於書上怎麼寫... 05/18 18:05
bernachom:因為我看書看了快1/4,他都這樣寫...QQ 05/18 18:18
bernachom:不過我大概知道了,應該是可以不用一樣 05/18 18:19
bernachom:只是我還是想不到有什麼情況是一定要的原因@@ 05/18 18:19
bernachom:謝謝您,我知道了。 05/18 18:19
markov:不用一樣 我常用這來測試公司只是代理還是會寫程式...XD 05/18 19:46
bera:這應該只是撰寫風格的問題, 不一定要一樣啦~~ 05/19 02:42
tnav:好處就~現在你遇到同名變數,你已經知道真正取的是哪邊的值囉 05/19 02:42
bernachom:謝謝各位前輩幫忙^_^ 05/19 12:57