Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 3.5 partial class String IsNullOrWhiteSpace

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.

like image 991
Stinus Avatar asked Jun 30 '11 13:06

Stinus


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

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
like image 135
Daniel Hilgarth Avatar answered Sep 22 '22 06:09

Daniel Hilgarth


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.

like image 32
Anthony Pegram Avatar answered Sep 21 '22 06:09

Anthony Pegram