C#编写高质量代码第二天

总结:

  • 使用tryParse比Parse要好,并且编写方法时可以使用TryDo模式,已提供程序运行效率
  • as和强制类型转换的区别,建议使用As方式,非继承关系无法使用As ,引出implictic和explic重载类的区别
  • int?可空类型,??判断左侧值是否为null,是传入右侧值,不是null传入左侧值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromoteDay02
{
    class Program
    {
        static void Main(string[] args)
        {
            FisrtType firstType = new FisrtType() { Name = "first Type" };
            SecondType secondType = (SecondType)firstType;
            // SecondType se = firstType as SecondType; 非继承关系无法用as转换
            DoWithSomeType(firstType);
            //Note:在FirstType是SecondType的基类时可以使用强转也可以用as转换,但是建议使用as
            //如果是继承关系,在父项转子项时先用is 判断是否可以父项是否包含子项类型


            /*----------------------------------------------------------*/
            //比较TryParse 比 Parse 好
            //Note:TryParse在转换失败时不会引发异常,Parse则会引发异常,转换成功的情况下TryParse也会比Parse要快点
            //public static bool TryParse(string s, out double result) 以Doule类型为例,转换失败则result值为0
            //√我们将提供的TryParse方法的这种行为叫做类型提供TryParse模式、TryParse模式为类型提供两种方法,假设第一个方法声明为Do
            //第二的方法叫TryDo,Do方法在执行过程中发生错误则引发异常,而TryDo则会返回一个bool值,方法执行失败返回false,如果要从TryDo
            //中获取实际的返回值,应该为方法提供out参数

            /*------------------------------------------------------------*/
            /*使用int?来确保值类型也可也为null
             *值类型不能为null
             
            int i= null 错误

             
             
             */
            //Nullable<int> j = null;对
            //int? i = null; 对
            //int k = null; 错

            int? i = 123;
            //int j;
            //if (i.HasValue)
            //{
            //    j = i.Value;

            //}
            //else
            //{
            //    j = 0;
            //}

            //?? 运算符,如果左侧不为null则把左侧赋值,否则赋值右侧
            int j = i ?? 0;


            Console.WriteLine(j.ToString());
            Console.ReadKey();

        }

        static void DoWithSomeType(object obj)
        {
            SecondType secondType = obj as SecondType;
            //Console.WriteLine(secondType.Name);
            //Console.ReadKey();
        }
    }

}
public class FisrtType
{
    public string Name { get; set; }
}
public class SecondType
{
    public string Name { get; set; }
 

    //1、传入的类型是在explicit(显示类型转换)修饰是需要使用强转将传入类型强转为SecondeType类型。
    //2、反之是implicit类型(隐式类型转换)修饰可直接将传入的类型赋值给重载类型
    //3、重载类是可以将类与类之间进行转换,他们之间不存在继承关系也可以进行转换
    //4、重载运算符不需要类型转换 所以不用implicit 或者explicit 

    //显示类型转换 通过强转改变FirstTpye类型,重载SecondType,
    public static explicit operator SecondType(FisrtType firstType)
    {
        SecondType secondType = new SecondType() { Name = "转型自:" + firstType.Name };
        return secondType;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值