讀改善c#程式碼157個建議:建議4~6

K戰神發表於2015-07-15

目錄:

  • 建議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";
        }
    }   

 

相關文章