string str = "hongda"; int num = 54; DateTime time = DateTime.Now; string date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); float floatNum = 1.4f; double doubleNum=1.5d; decimal decimalNum = 1.6m; Guid guid = Guid.NewGuid(); Response.Write(str.GetType() + ""); Response.Write(num.GetType() + ""); Response.Write(time.GetType() + ""); Response.Write(date.GetType() + ""); Response.Write(floatNum.GetType() + ""); Response.Write(doubleNum.GetType() + ""); Response.Write(decimalNum.GetType() + ""); Response.Write(guid.GetType() + "");
string str = "hongda"; int? num = 54; DateTime? time = DateTime.Now; string date = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); float? floatNum = 1.4f; double? doubleNum = 1.5d; decimal? decimalNum = 1.6m; Guid? guid = Guid.NewGuid(); Response.Write(str.GetType() + ""); Response.Write(num.GetType() + ""); Response.Write(time.GetType() + ""); Response.Write(date.GetType() + ""); Response.Write(floatNum.GetType() + ""); Response.Write(doubleNum.GetType() + ""); Response.Write(decimalNum.GetType() + ""); Response.Write(guid.GetType() + "");
。。。
除了string类型能够为null,其他的都不行。
string str = "hongda"; string str1 = ""; string str2 = null; Console.WriteLine(str.GetType()); //System.String Console.WriteLine(str1.GetType()); //System.String Console.WriteLine(str2.GetType()); //未将对象引用设置到对象的实例
var d = null; //无法将“”赋予隐式类型的局部变量 string a = null; object o = a as object; Console.WriteLine(o.GetType()); //未将对象引用设置到对象的实例
string str = "23"; string str1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(str.GetType()); //System.String Console.WriteLine(str1.GetType()); //System.String
int num = 54; int? num3 = 53; int? num4 = null; Console.WriteLine(num.GetType()); //System.Int32 Console.WriteLine(num3.GetType()); //System.Int32 Console.WriteLine(num4.GetType()); //未将对象引用设置到对象的实例
DateTime time = DateTime.Now; DateTime? time3 = DateTime.Now.AddDays(3); DateTime? time4 = null; Console.WriteLine(time.GetType()); //System.DateTime Console.WriteLine(time3.GetType()); //System.DateTime Console.WriteLine(time4.GetType()); //未将对象引用设置到对象的实例
//DateTime date = Convert.ToDateTime(""); //该字符串未被识别为有效的 DateTime。 DateTime date = Convert.ToDateTime("0001-01-01 01:01:01"); //这种日期在.net中可以,在数据库中超出范围 Console.WriteLine(date.AddDays(5));
int a = new int(); DateTime date = new DateTime(); float f = new float(); double d = new double(); decimal de = new decimal(); Console.WriteLine(a); Console.WriteLine(date); Console.WriteLine(f); Console.WriteLine(d); Console.WriteLine(de);
public class TestClass2 { public TestClass2() { } public int num; public DateTime dt; public string td_Str { get; set; } public string td_Str2 { get; set; } public int td_Num { get; set; } public DateTime td_Time { get; set; } } TestClass2 t2 = new TestClass2(); Console.WriteLine(t2.td_Str); //null Console.WriteLine(t2.td_Num); Console.WriteLine(t2.td_Time); Console.WriteLine(t2.num); Console.WriteLine(t2.dt); System.Console.Read();
发现每一个类的对象进行实例化时,它的属性,字段都会初始化
class test { public int? num { get; set; } public string str { get; set; } public DateTime? date { get; set; } public float? fl { get; set; } public decimal? de { get; set; } }
test t = new test(); Console.WriteLine(t.num.GetType());//未将对象引用设置到对象的实例 Console.WriteLine(t.str.GetType());//未将对象引用设置到对象的实例 Console.WriteLine(t.date.GetType());//未将对象引用设置到对象的实例 Console.WriteLine(t.fl.GetType());//未将对象引用设置到对象的实例 Console.WriteLine(t.de.GetType());//未将对象引用设置到对象的实例
总结:加?的值类型是个特殊的值类型,它的值可以为null,应该是一个特殊的null,
Console.WriteLine(null);
test t = new test(); Console.WriteLine(t.num); Console.WriteLine(t.str); Console.WriteLine(t.date); Console.WriteLine(t.fl); Console.WriteLine(t.de);
这个还是可以正常运行,还占了5行
int? 默认初始化类型Nullable<int>,推 string Nullable<string>