读改善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";
        }
    }   

 

转载于:https://www.cnblogs.com/sunchong/p/4647253.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值