透過 Type 類別的 IsValueType() 方法,判斷是否為實值類別。
Byte var_byte = 0; Int32 var_int = 0; DateTime var_datetime = System.DateTime.Now; Guid var_guid = new Guid(); string var_str = ""; Exception var_ex = new Exception(); object[] types = { var_byte, var_int, var_guid, var_datetime, var_str, var_ex }; foreach (object o in types) { string type; if (o.GetType().IsValueType) type = "Value type"; else type = "Reference Type"; Console.WriteLine("{0}: {1}", o.GetType(), type); } //System.Byte: Value type //System.Int32: Value type //System.DateTime: Value type //System.Guid: Value type //System.String: Reference Type //System.Exception: Reference Type
全站熱搜
留言列表