zoukankan      html  css  js  c++  java
  • 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

     

    理论补充:

  • 相关阅读:
    MTK 官方 openwrt SDK 使用
    PF_RING packet overwrites
    pycares cffi
    libevent evbuffer bug
    浮点转字符串性能比较
    重写 libev 的 EV_WIN32_HANDLE_TO_FD
    thrift TNonblockingServer 使用
    accel-pptp 部署
    boost::asio 使用 libcurl
    蜂鸟A20开发板刷 cubietruck 的 SD 卡固件
  • 原文地址:https://www.cnblogs.com/wangn/p/3035471.html
Copyright © 2011-2022 走看看