[英]String.IsNullOrEmpty() or IsEmpty()
我剛注意到它們有很多擴展方法,我想我從來沒有注意到字符串。
有些人喜歡
IsEmpty() // Seems to be equivalent to String.IsNullOrEmpty()
AsInt() // seems to be equivalent to Convert.ToInt32(string); - does it throw exception as well?
我只是想知道他們在鈎子下使用相同的代碼,這些只是為了減少打字或更多的進行?
有些人似乎確實缺少了
String.IsNullOrWhiteSpace()
編輯
很抱歉當我說String.IsNullOrWhiteSpace()丟失時我發現沒有擴展方法。 我確實有這種方法是我寫上面做的。
似乎這些在框架中不是標准的,所以我試圖弄清楚它們來自哪里?
我不確定resharper是否添加了這些或者我是否有其他參考。 我不認為我曾經導入任何擴展插件。
當我點擊IsEmpty()上的定義時
我明白了
#region Assembly System.Web.WebPages.dll, v4.0.30319
// c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll
#endregion
using System;
using System.Runtime.CompilerServices;
namespace System.Web.WebPages
{
// Summary:
// Provides utility methods for converting string values to other data types.
public static class StringExtensions
{
// Summary:
// Converts a string to a strongly typed value of the specified data type.
//
// Parameters:
// value:
// The value to convert.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value);
//
// Summary:
// Converts a string to the specified data type and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value, TValue defaultValue);
//
// Summary:
// Converts a string to a Boolean (true/false) value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static bool AsBool(this string value);
//
// Summary:
// Converts a string to a Boolean (true/false) value and specifies a default
// value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// false.
//
// Returns:
// The converted value.
public static bool AsBool(this string value, bool defaultValue);
//
// Summary:
// Converts a string to a System.DateTime value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value);
//
// Summary:
// Converts a string to a System.DateTime value and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// the minimum time value on the system.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value, DateTime defaultValue);
//
// Summary:
// Converts a string to a System.Decimal number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value);
//
// Summary:
// Converts a string to a System.Decimal number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or invalid.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value, decimal defaultValue);
//
// Summary:
// Converts a string to a System.Single number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static float AsFloat(this string value);
//
// Summary:
// Converts a string to a System.Single number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Returns:
// The converted value.
public static float AsFloat(this string value, float defaultValue);
//
// Summary:
// Converts a string to an integer.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static int AsInt(this string value);
//
// Summary:
// Converts a string to an integer and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or is an invalid value.
//
// Returns:
// The converted value.
public static int AsInt(this string value, int defaultValue);
//
// Summary:
// Checks whether a string can be converted to the specified data type.
//
// Parameters:
// value:
// The value to test.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool Is<TValue>(this string value);
//
// Summary:
// Checks whether a string can be converted to the Boolean (true/false) type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsBool(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.DateTime type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDateTime(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Decimal type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDecimal(this string value);
//
// Summary:
// Checks whether a string value is null or empty.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value is null or is a zero-length string (""); otherwise, false.
public static bool IsEmpty(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Single type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsFloat(this string value);
//
// Summary:
// Checks whether a string can be converted to an integer.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsInt(this string value);
}
}
這些不是“標准”擴展方法 - 很可能是其他人正在為您的項目工作添加它們。 這意味着我們不知道代碼在幕后做了什么,但你應該能夠找到自己。
在Visual Studio中,您應該能夠導航到任一方法的定義 - 它將顯示方法所在的程序集,或者如果可以,則直接轉到源代碼。
編輯:鑒於評論,看起來它們是MVC的StringExtensions
類的擴展方法...據我所知,它違反了命名中的各種不良做法 - 特別是在方法名稱中使用特定於語言的名稱而不是CLR類型名稱。 (所以AsFloat
應該是AsSingle
例如。)我也認為它應該是“To”而不是“As”,因為它提供了完整的轉換,而不僅僅是返回原始值的視圖。 呸所有。
有些人似乎確實缺少了
String.IsNullOrWhiteSpace()
那個是在Fx4中引入的,你運行3.5嗎?
我現在很長時間使用這個擴展方法的每一個字符串...它很容易使用它,快速....使擴展方法你的日常習慣,你可以和應該,你會看到編寫代碼的巨大差異。
創建一個庫並在您的應用程序中使用它....
public static bool IsNullOrEmpty(this string testString)
{
return string.IsNullOrEmpty(testString);
}
檢查您的引用 - 可以在您引用的庫中定義擴展方法。 您可以嘗試一次刪除一個引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.