开发者

String.IsNullOrEmpty() or IsEmpty()

I just noticed on string they have a lot of extension methods that I guess I never noticed on strings.

Some are like

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

I am just wonder do they use the same code under the hook and these are just away to reduce typing or is more going on?

Some do seem to be missing though like

 String.IsNullOrWhiteSpace()

Edit

Sorry when I said String.IsNullOrWhiteSpace() is missing I ment that there was no extension method. I do have that method is I write what I do above.

It also seems that these are not standard in the framework so I am trying to figure out where they came from?

I am not sure if resharper added these or if some other reference I have. I don't think I ever imported any extension plugin.

When I click definition over IsEmpty()

I get this

#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 S开发者_StackOverflowystem.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);
    }
}


These aren't "standard" extension methods - chances are they've been added by someone else working on your project. That means we can't tell what the code's doing under the hood, but you should be able to find out yourself.

In Visual Studio you should be able to navigate to the definition of either method - it will either show which assembly the method is in, or go straight to the source code if it can.

EDIT: Given the comments, it looks like they're the extension methods from MVC's StringExtensions class... which violates various bad practices in naming, as far as I can tell - particularly using language specific names within method names instead of the CLR type name. (So AsFloat should be AsSingle for example.) I'd also argue that it should be "To" rather than "As" given that it's providing a full conversion rather than just returning a view on the original value. Bah humbug 'n all.


Some do seem to be missing though like

String.IsNullOrWhiteSpace()

That one was introduced in Fx4, are you running 3.5 ?


I use for long time now this Extension method for every string...it's easy to use it, quick....make extension methods your daily habit where you can and should and you will see big difference writing code.

Create a library and use it across your applications....

public static bool IsNullOrEmpty(this string testString)
        {
            return string.IsNullOrEmpty(testString);
        }


Check your references - the extension methods can be defined in one of the libraries you've referenced. You can try removing one reference at a time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜