博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
object 类型转换 ?
阅读量:5145 次
发布时间:2019-06-13

本文共 4837 字,大约阅读时间需要 16 分钟。

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>

 

转载于:https://www.cnblogs.com/hongdada/archive/2013/03/22/2975210.html

你可能感兴趣的文章
Android现学现用第十一天
查看>>
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
CI控制器调用内部方法并载入相应模板的做法
查看>>
Hdu - 1002 - A + B Problem II
查看>>
HDU - 2609 - How many
查看>>
每天CookBook之Python-003
查看>>
每天CookBook之Python-004
查看>>
Android设置Gmail邮箱
查看>>
StringBuffer的用法
查看>>
js编写时间选择框
查看>>
Java数据结构和算法(四)--链表
查看>>
JIRA
查看>>