C#构造函数和可空类型

今天做了一个构造函数和可空类似的实验,晚上再做详细的说明。

写博客是为了记录自己,给自己主动学习提供线索,没有高深东西,不喜勿扰。

实验代码

public class ConstructorTest
{
    public ConstructorTest()
    {
        Console.WriteLine("这个是默认的构造函数");
    }
    public ConstructorTest(string name)
        : this()
    {
        Console.WriteLine(string.Format("My Name is {0}", name));
    }
    public double? String2Double(string str)
    {
        double? result = null;
        if (string.IsNullOrEmpty(str)) return result;
        double tmpValue;
        if (double.TryParse(str, out tmpValue))
        {
            result = tmpValue;
            return result;
        }
        return result;
    }
}

 

ConstructorTest ct = new ConstructorTest("Hello");
double? result = ct.String2Double("123");
double? result1 = ct.String2Double("abc");
Console.WriteLine(result == null ? "NULL" : result.Value.ToString());
Console.WriteLine(result1 == null ? "NULL" : result1.Value.ToString());

 

输出:

这个是默认的构造函数
My Name is Hello
123
NULL

 

理论补充:

posted @ 2013-04-24 17:42  小白快跑  阅读(431)  评论(0编辑  收藏  举报