作者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
1F:推 sohumi:好方法 12/21 12:35
2F:推 linxiaoxi:好方法 12/22 11:55