作者iterator (rotareti)
看板C_Sharp
標題Re: [問題] 變數是null時判斷變數型態?
時間Wed Dec 21 11:14:05 2011
※ 引述《sohumi (皮老闆)》之銘言:
: 舉例說明
: string s;
: s.gettype(); <==出錯
: if(s is string) <==條件不成立
: s ="";
: s.gettype(); <==傳回system.string
: if(s is string) <==條件成立
public static Type GetValueType<T>(T value)
{
return typeof(T);
}
: 換言之 判斷變數型態之前 要先塞對應型態的值,
: 但就是在未知型態之下才要判斷要塞甚麼型態的值,
: ex:
: if(s is int){s = 123;}else if(s is stirng){s = "123";}...
: 有辦法在變數null時判斷其型態嗎?
這部分其實有問題, 先想想這樣是否 compile 得過.
實際上真的會這樣使用變數嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.23.102
推 sohumi:好方法 12/21 12:35
推 linxiaoxi:好方法 12/22 11:55