[心得] 真的很討厭null
null這個東西,真的是很討厭,雖然它叫做連空白也沒有。
但,沒有就沒有,幹麻報錯。
舉個例來說:
我有一個 class 叫person ,裡面有人名,年紀二個欄位
若我查詢 人名叫josn 的就怎樣怎樣。
假設這個person是null
if(person.name == "josn"){怎樣。}
就錯,為什麼不直接跳過去就好,因為我只是判別呀。
而非要寫成
if(person != null && person.name == "josn"){怎樣。}
就這樣,就要多幾個字,很討厭
由上面的例子,person 是null 那在person.name 當然就不可能為josn,就
跳過去就好,幹麻要報錯。
由其是dictionary物件,為了安全,非要一個containskey,先看有沒有再處理。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.78.48
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1493996089.A.75B.html
推
05/05 23:01, , 1F
05/05 23:01, 1F
→
05/05 23:02, , 2F
05/05 23:02, 2F
→
05/05 23:09, , 3F
05/05 23:09, 3F
→
05/05 23:10, , 4F
05/05 23:10, 4F
→
05/05 23:10, , 5F
05/05 23:10, 5F
推
05/06 00:03, , 6F
05/06 00:03, 6F
→
05/06 00:03, , 7F
05/06 00:03, 7F
→
05/06 00:04, , 8F
05/06 00:04, 8F
推
05/06 01:45, , 9F
05/06 01:45, 9F
→
05/06 02:31, , 10F
05/06 02:31, 10F
→
05/06 02:31, , 11F
05/06 02:31, 11F
→
05/06 11:22, , 12F
05/06 11:22, 12F
推
05/06 11:46, , 13F
05/06 11:46, 13F
推
05/06 21:59, , 14F
05/06 21:59, 14F
→
05/07 00:23, , 15F
05/07 00:23, 15F
噓
05/07 00:42, , 16F
05/07 00:42, 16F
→
05/07 02:59, , 17F
05/07 02:59, 17F
→
05/07 03:08, , 18F
05/07 03:08, 18F
噓
05/07 04:39, , 19F
05/07 04:39, 19F
→
05/07 10:55, , 20F
05/07 10:55, 20F
噓
05/07 11:22, , 21F
05/07 11:22, 21F
→
05/07 17:49, , 22F
05/07 17:49, 22F
推
05/07 19:32, , 23F
05/07 19:32, 23F
→
05/07 19:34, , 24F
05/07 19:34, 24F
→
05/07 20:36, , 25F
05/07 20:36, 25F
噓
05/07 20:54, , 26F
05/07 20:54, 26F
→
05/08 02:30, , 27F
05/08 02:30, 27F
推
05/08 18:16, , 28F
05/08 18:16, 28F
推
05/08 18:37, , 29F
05/08 18:37, 29F
→
05/08 18:37, , 30F
05/08 18:37, 30F
→
05/08 18:37, , 31F
05/08 18:37, 31F
→
05/08 18:37, , 32F
05/08 18:37, 32F
→
05/08 18:37, , 33F
05/08 18:37, 33F
→
05/08 18:37, , 34F
05/08 18:37, 34F
→
05/08 20:44, , 35F
05/08 20:44, 35F
推
05/10 00:47, , 36F
05/10 00:47, 36F
→
05/10 00:47, , 37F
05/10 00:47, 37F
→
05/10 00:47, , 38F
05/10 00:47, 38F
→
05/10 00:48, , 39F
05/10 00:48, 39F
→
05/10 00:58, , 40F
05/10 00:58, 40F
→
05/11 04:56, , 41F
05/11 04:56, 41F
噓
05/12 21:12, , 42F
05/12 21:12, 42F
→
05/12 21:13, , 43F
05/12 21:13, 43F
→
05/13 06:24, , 44F
05/13 06:24, 44F
→
05/15 01:31, , 45F
05/15 01:31, 45F
→
05/19 14:58, , 46F
05/19 14:58, 46F
→
05/19 14:59, , 47F
05/19 14:59, 47F
→
05/19 14:59, , 48F
05/19 14:59, 48F
→
05/19 15:00, , 49F
05/19 15:00, 49F
→
05/19 15:00, , 50F
05/19 15:00, 50F
→
05/19 15:01, , 51F
05/19 15:01, 51F
→
05/19 15:01, , 52F
05/19 15:01, 52F
→
05/31 14:45, , 53F
05/31 14:45, 53F
→
05/31 14:50, , 54F
05/31 14:50, 54F
討論串 (同標題文章)
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章