String.IsNullOrEmpty()或IsEmpty()

cho*_*bo2 9 .net c#

我刚注意到它们有很多扩展方法,我想我从来没有注意到字符串.

有些人喜欢

IsEmpty() // Seems to be equivalent to  String.IsNullOrEmpty() 
AsInt() //  seems to be equivalent to Convert.ToInt32(string); - does it throw exception as well?
Run Code Online (Sandbox Code Playgroud)

我只是想知道他们在钩子下使用相同的代码,这些只是为了减少打字或更多的进行?

有些人似乎确实缺少了

 String.IsNullOrWhiteSpace()
Run Code Online (Sandbox Code Playgroud)

编辑

很抱歉当我说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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 15

这些不是"标准"扩展方法 - 很可能是其他人正在为您的项目工作添加它们.这意味着我们不知道代码在幕后做了什么,但你应该能够找到自己.

在Visual Studio中,您应该能够导航到任一方法的定义 - 它将显示方法所在的程序集,或者如果可以,则直接转到源代码.

编辑:鉴于评论,看起来它们是MVC StringExtensions的扩展方法......据我所知,它违反了命名中的各种不良做法 - 特别是在方法名称中使用特定语言的名称而不是CLR类型名称.(所以AsFloat应该是AsSingle例如.)我也认为它应该是"To"而不是"As",因为它提供了完整的转换,而不仅仅是返回原始值的视图.呸所有.