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

     

    理论补充:

  • 相关阅读:
    由 基本数据型态转换成 String/ 由 String 转换成 数字的基本数据型态
    屏幕适配(UGUI)非UI
    转载 Unity Text 插入超链接
    File类的使用
    抽奖
    竖倾斜ScrollView
    本地资源_Asset
    小型自动朝向转盘
    简易C# socket
    Lua class
  • 原文地址:https://www.cnblogs.com/wangn/p/3035471.html
Copyright © 2011-2022 走看看