Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.IsNullOrEmpty() or IsEmpty()

Tags:

c#

.net

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 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);
    }
}
like image 200
chobo2 Avatar asked Feb 14 '11 17:02

chobo2


People also ask

Should I use empty string or null?

An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

How do I check if a string is empty or not?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

Should I use IsNullOrEmpty or IsNullOrWhiteSpace?

The main difference between IsNullOrEmpty and IsNullOrWhiteSpace in C# is that IsNullOrEmpty checks if there's at least one character, while IsNullOrWhiteSpace checks every character until it finds a non-white-space character.

Is nil the same as empty string?

nil is not empty, it is a nil value.


2 Answers

Some do seem to be missing though like

String.IsNullOrWhiteSpace()

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

like image 73
Henk Holterman Avatar answered Sep 22 '22 10:09

Henk Holterman


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.

like image 35
Jon Skeet Avatar answered Sep 26 '22 10:09

Jon Skeet