2013-05-15 16 views
12

我試圖得到這個工作,但不知何故它離開我的手......我想能夠檢查空或任何類型的空我分配。通用的方法來檢查空或任何類型的類型,如int,string,double

EX:

int i =0; 
string mystring = ""; 

var reult = CheckNullOrEmpty(10) // passing int 
var result 1 = CheckNullOrEmpty(mystring) // passing string 

public bool CheckNullOrEmpty<T>(T value) 
{ 
    // check for null or empty for strings 
    // check for null i.e. 0 for int 

} 

有人可以幫助我這個..我想了解泛型是如何工作的這個簡單的方法。

+2

我認爲ü需要創建很多方法把它命名爲所有CheckNullOrEmpty。每個方法檢查不同的數據類型 –

+3

我認爲你不能一般地檢查* empty *這樣的東西,因爲沒有一般的定義:對於一個'string'它是一個空字符串,對於'int'它可能是'0 ',但對於*任何類型的'T' *它可能只是任何東西,或者甚至不存在... –

+1

如果你想要一個默認值比較,比較'default(T)'。這是你在這裏能做的最好的。 –

回答

24
public static bool CheckNullOrEmpty<T>(T value) 
{ 
    if (typeof(T) == typeof(string)) 
     return string.IsNullOrEmpty(value as string); 

    return value == null || value.Equals(default(T)); 
} 

如何使用:

class Stub { } 

bool f1 = CheckNullOrEmpty(""); //true 
bool f2 = CheckNullOrEmpty<string>(null); //true 
bool f3 = CheckNullOrEmpty(0); //true 
bool f4 = CheckNullOrEmpty<Stub>(null); //true 
+0

如果'T'是一個引用類型<>'字符串並且值爲'null'會怎麼樣? –

+1

這工作完美..... –

+1

if(typeof(T)== typeof(string))而不是if(value是字符串)的任何原因? –

1

您可以使用默認() -

如:

if(value != default(T)) 

從MSDN:

給出一個參數化類型T的變量t,語句T =空 被僅在T是參考類型時有效,t = 0僅適用於 數值類型,但不適用於結構。解決方案是使用默認關鍵字 ,該關鍵字將針對參考類型返回null,對於數值類型則返回零 。對於結構體,它將返回 的每個成員,初始化爲0或null的結構體取決於它們是否爲 值或引用類型。

http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.80).aspx

+0

這不包括string.Empty值 –

1

因爲你CheckNullOrEmpty實現相差類型,你不能有檢查作爲一種通用功能。

但是如果你使用Nullable值類型,你可以使用GetValueOrDefault()

int? i = 0; 

var result = i.GetValueOrDefault(10); 

那麼對於string,只是有一個擴展方法:

public static string GetValueOrDefault(this string item, string defaultValue = "") 
{ 
    return !string.IsNullOrWhiteSpace(item) ? item : defaultValue; 
} 

然後,你可以這樣做:

string i = null; 
string mystring = ""; 

var result = i.GetValueOrDefault(mystring); 
2

您可以檢查default(T);

public bool CheckNullOrEmpty<T>(T value) 
{ 
     return value == default(T); 
} 

欲瞭解更多信息:http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx

+0

這是我在找什麼... –

+1

不幸的是,這並不包括string.Empty。 – Maarten

+0

它表示運算符'=='不能應用於'T'和'T'類型的操作數 –

0
 
I've added a couple of re-factorings: 

1. DBNull is not seen as isnullorempty so added the (value as string) 
2. Passing a datatable row[column] that was created dynamically at runtime (like: row["PropertyName"]) will not see this as a string with typeof(T) due to the fact that the column type base is object. So added an extra check for typeof(object) and if not null test that the ToString() is empty value. I haven't tested this fully so may not work for all types of data columns. 
public static bool CheckNullOrEmpty<T>(T value) 
    { 
     // note: this does not see DBNull as isnullorempty. 
     if (typeof(T) == typeof(string)) 
     { 
      if (!string.IsNullOrEmpty(value as string)) 
      { 
       return string.IsNullOrEmpty(value.ToString().Trim()); 
      } 
      else 
      { 
       return true; 
      } 
     } 
     else if (typeof(T) == typeof(object)) 
     { 
      // note: datatable columns are always seen as object at runtime with generic T regardless of how they are dynamically typed? 
      if (value != null) { 
       return string.IsNullOrEmpty(value.ToString().Trim()); 
      } 
     } 


     return value == null || value.Equals(default(T)); 
    } 
+1

雖然這段代碼可能是解決方案,[包括解釋](// meta.stackexchange.com/questions/114762/解釋完全基於代碼的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – yivi

相關問題