• 读改善c#代码157个建议:建议4~6


    目录:

    • 建议4:TryParse比Parse好
    • 建议5:使用int?确保值类型也可以为null
    • 建议6:区别 readonly 和 const 的用法

     一、建议4:TryParse比Parse 好

    TryParse:发生转换异常,内部处理异常。返回false并且result=0

    public static bool TryParse(string s, out int result);

    Parse:发生转换异常,会抛出异常。

    public static decimal Parse(string s);

    总结:TryParse会消化异常,Parse不会。如果产生异常,TryParse效率比Parse高。

    二、建议5:使用int?确保值类型也可以为null

    为什么?~数据库一个int类型的字段可以为null ; 一定的场景下0是有实际意义的,可为了区分0与null.

    Nullable<T> 值类型(where T : struct)可拥有null的能力.  简化为:T?(Nullable<int>=int?)

    public struct Nullable<T> where T : struct

    int? 比  int 多了一个null值:int?=int(可以成功转换);反之 int=int?(需要特殊处理null值)

    int?=int

    int? nullableNum = null;
    
    int normalNum = 10;
    
    nullableNum = normalNum;

     int=int?

    HasValue属性:判断是否有值;Value属性:具体的值

     int? nullableNum = null;
    
     int normalNum = 10;if (nullableNum.HasValue)
         normalNum = nullableNum.Value;
    else
          normalNum = 0;

    以上的思路就是:如果 nullableNum 为null,normalNum=0;不为null,就直接赋值。

    所以我们可以使用 ?? 简化以上操作

    int? nullableNum = null;
    
    int normalNum = 10;            
    
    normalNum = nullableNum ?? 0;

     当然也就是建议一下,有时缩写反而引起代码可读性降低。

    翻开当前做的项目,发现有许多地方需要进行优化:可读性和代码整洁度上都可以优化

    if (b.EnablePush)
    {
       if (b.PushRatio1 != null)
            postRatio = b.PushRatio1.Value;
       if (b.PushRatio2 != null)
            postRatio2 = b.PushRatio2.Value;
    }                      

    修改后:

    if (b.EnablePush)
    {
           if (b.PushRatio1.HasValue)
               postRatio = b.PushRatio1.Value;
           if (b.PushRatio2.HasValue)
               postRatio2 = b.PushRatio2.Value;
    }

     翻开项目,瞅了瞅,有个疑问:有什么区别???????(类似这样)

    DateTime t1 = DateTime.Now;            
    
    bool flag = false;
    
    //这句话编译不通过
    DateTime? t = flag ? null : t1;

    要这样修改才行:

    DateTime? t1 = DateTime.Now;            
    
    bool flag = false;
    
    DateTime? t = flag ? null : t1;

    或者这样:

    DateTime t1 = DateTime.Now;            
    
    bool flag = false;
    
    DateTime? t = flag ? (DateTime?)null : t1;

    三、建议6:区别 readonly 和 const 的用法

    使用场景:

    使用const的是为了效率,但不够灵活。

    使用readonly效率不是考虑的首要因素,不过够灵活。

    区别:

    readonly(只读):运行时常量;修饰类型不限。*readonly 是运行时常量,在第一次为它赋值后就不能再修改。修饰引用类型是引用不变。

    const(常量):编译期常量;只能修饰:基元类型、枚举类型或字符串类型。*const是编译期常量,在我们编译完后,值就固定,无法修改。所以也是默认的static修饰。

    1、readonly

    下面,编译不通过。一旦readonly赋值后,就不能修改。提示也很明确:不能给只读字段赋值。

    class People
        {
            public readonly String NAME="Sunn";
            public People()
            {
            }
        }
     People p = new People();
     p.NAME = "Yuan";
     Console.WriteLine(p.NAME);

    但是我们可以在类实例化时,对值进行赋值:

     class People
    {
            public readonly String NAME="Sunn";
            public People()
            {
                NAME = "Yuan";
            }
    }  

    我们平时可能经常见这样写:

    public static readonly String NAME="Sunn";

    static我们知道是属于类的标识,统一化的行为、共同的行为。非单个实例的行为。如果要修改值,那就需要在静态构造函数中进行赋值。

     class People
        {
            public static readonly String NAME="Sunn";
            static People()
            {
                NAME = "Yuan";
            }
            public People()
            {          
            }
        }   

    建议使用:static readonly 组合写法。

    2、const

    其实是类的常量,加上static反而出错。因为编译器会自动加上static.

     class People
        {
            public const String NAME = "Sunn";           
        }   

    IL代码:发现编译器为我们加上了 static

      .field public static literal string NAME = string('Sunn')

    我在想,既然是看作了类的常量,那我们,这样是不行滴~~因为是编译时常量,编译完成后就不能修改值

    class People
        {
            public const String NAME = "Sunn";
            static People()
            {
                NAME = "Yuan";
            }
        }   
  • 相关阅读:
    JSON跨域请求
    2013.9.26 心得体会
    MemCached用法
    使用SQL联合查询来构建临时vo对象的应用
    ubuntu 16.04 安装php 5 6等版本
    mac php版本切换
    windows 查看端口占用
    nginx 反向代理到目录
    linux挂在samba服务器到本地(用于备份文件到nas或者windows的文件服务器)
    ubuntu 加载新硬盘或分区
  • 原文地址:https://www.cnblogs.com/sunchong/p/4647253.html
Copyright © 2020-2023  润新知