C# 3.5 partial class String IsNullOrWhiteSpace
Asked Answered
F

7

15

I'm trying to create extra functionality to the String class (IsNullOrWhitespace as in .NET4 ) But I'm having an problem with referencing:

Error 1 'String' is an ambiguous reference between 'string' and 'geolis_export.Classes.String'

I don't want to create an extension method. Because this will crash if string x = null;

Usage:

private void tbCabineNum_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    e.Handled = !e.Text.All(Char.IsNumber) || String.IsNullOrWhiteSpace(e.Text);
}

String partial:

public partial class String
{
    public static bool IsNullOrWhiteSpace(string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}

Is it not possible to create extras for the String class? I have tried to put the partial in the System namespace, but this gives other errors.

Renaming String to String2 fixes the problem also. But this is not what I want, because then there is no reference with the original String class.

Fatten answered 30/6, 2011 at 13:28 Comment(0)
F
47

It is not possible like this, because the string class in the .NET framework is not partial.
Instead, use a real extension method like this:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}

The usage would then be like this:

string s = "test";
if(s.IsNullOrWhiteSpace())
    // s is null or whitespace

As with all extension methods, the call will not result in a null reference exception if the string is null:

string s = null;
if(s.IsNullOrWhiteSpace()) // no exception here
    // s is null or whitespace

The reason for this behavior is that the compiler will translate this code into IL code that is equivalent to the IL code of the following:

string s = null;
if(StringExtensions.IsNullOrWhiteSpace(s))
    // s is null or whitespace
Flinger answered 30/6, 2011 at 13:30 Comment(3)
@Stinus: Right, you were missing that extension methods must live in a static class and have only static methods. They also must accept a this String parameter as the first argument to the method (assuming you're extending String).Chaing
Shouldn't you be returning true if the string is null?Narcosynthesis
@Stinus: No, it will not. See the last part of my answer. @Chris: Yes, you are right, I just copied that part from the OPs code. Fixed.Flinger
B
6

An extension method has to be defined as a static method inside a static class. Also notice the this keyword on the parameter.

public static class MyExtensions
{
    public static bool IsNullorWhitespace(this string input)
    {
         // perform logic
    }
}

What you have done by omitting the static on the class is define a competing class within your assembly, hence the ambiguous message from the compiler.

Bartz answered 30/6, 2011 at 13:30 Comment(0)
R
2

The string class is not declared as partial, you will have to write an extension method instead.

Refreshing answered 30/6, 2011 at 13:30 Comment(0)
K
1
public static class MyExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return false;
        return string.IsNullOrEmpty(value.Trim());
    }
}
Kerbing answered 30/6, 2011 at 13:31 Comment(0)
C
1

That isn't how you create an extension method. The class isn't a partial, it needs to be a static class and it can be named anything (MyExtensionMethods). You also need to mark your parameter with "this" on an extension method.

Try this instead

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value == null) return true;
        return string.IsNullOrEmpty(value.Trim());
    }
}
Confident answered 30/6, 2011 at 13:32 Comment(0)
L
0

In order to create an extension method, you need to use the following syntax. (Note the use of the keyword this):

public static bool IsNullOrWhiteSpace(this string value)
Liquefacient answered 30/6, 2011 at 13:30 Comment(0)
M
0

Trimming the string results in an avoidable string allocation (which can hurt performance). Better to check each character in turn and not allocate anything:

        public static bool IsNullOrWhiteSpace(this string value)
        {
#if NET35
            if (value == null)
                return true;

            foreach (var c in value)
            {
                if (!char.IsWhiteSpace(c))
                {
                    return false;
                }
            }

            return true;
#else
            return string.IsNullOrWhiteSpace(value);
#endif
        }

The #if NET35 code means that the fallback implementation exists only when targeting .NET 3.5. You can adjust that comparison to meet your project's target frameworks as needed. Otherwise, the default string.IsNullOrWhiteSpace method is used, and this util method would likely be inlined.

Montagnard answered 17/5, 2021 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.