25

I define a string and check it by string.IsNullOrEmptyOrWhiteSpace().

But I got this error:

'string' does not contain a definition for 'IsNullOrEmptyOrWhiteSpace' and no extension method 'IsNullOrEmptyOrWhiteSpace' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) D:\project\project\Controllers\aController.cs 23 24 project

What is the reason?

8
  • 3
    @4thpage: there's no such thing as "csharp". It's "C#". Commented Jul 4, 2010 at 9:13
  • 1
    If your using VS2010 its easy to switch target framework to 4.0 and like magic the error will disappear Commented Jul 4, 2010 at 9:21
  • 1
    @John Saunders: If it looks like csharp, smells like csharp, and is spelled like csharp, it must be csharp. Commented Aug 17, 2010 at 21:17
  • 3
    @AMi there is no csharp, c-sharp, c_#, or cshizzarp tag. There is a c# tag, which you can use to tag a c# question. Commented Sep 7, 2010 at 11:47
  • 3
    You're looking for a function that isn't defined. string (System.String) only includes a definition for IsNullOrEmpty or IsNullOrWhitespace, not IsNullOrEmptyOrWhitespace. string.IsNullOrWhitespace will return true if the string is empty. Also, this is a question about the .NET framework and is not specific to c#. Commented Sep 14, 2010 at 22:09

7 Answers 7

75

String.IsNullOrWhiteSpace has been introduced in .NET 4. If you are not targeting .NET 4 you could easily write your own:

public static class StringExtensions { public static bool IsNullOrWhiteSpace(string value) { if (value != null) { for (int i = 0; i < value.Length; i++) { if (!char.IsWhiteSpace(value[i])) { return false; } } } return true; } } 

which could be used like this:

bool isNullOrWhiteSpace = StringExtensions.IsNullOrWhiteSpace("foo bar"); 

or as an extension method if you prefer:

public static class StringExtensions { public static bool IsNullOrWhiteSpace(this string value) { if (value != null) { for (int i = 0; i < value.Length; i++) { if (!char.IsWhiteSpace(value[i])) { return false; } } } return true; } } 

which allows you to use it directly:

bool isNullOrWhiteSpace = "foo bar".IsNullOrWhiteSpace(); 

For the extension method to work make sure that the namespace in which the StringExtensions static class has been defined is in scope.

Sign up to request clarification or add additional context in comments.

5 Comments

@Darin, you are absolutely right, the method is enabled only in .NET 4 .0 and .NET Client Profile 4.0
A question please, Why the inner for loop instead of value.trim()!=string.empty ?
@Jon, this way you don't have to create a new string, and can return false as soon as you find the first non-whitespace character (Trim will look at characters at the beginning and end, until it finds non-whitespace). TrimStart wouldn't be as bad, but you're still creating a new (possibly long) string unnecessarily.
@KMan, it's considered bad practice to allow extension methods to run properly if the extended this parameter is null. It would be inconsistent with the behavior of other object references to not throw an ArgumentNullException within your extension method in this scenario. This is according to Microsoft's recommendations.
@uosɐſ I guess this is an exceptional case where exception shouldn't be thrown ))
33

Here's another alternative implementation, just for fun. It probably wouldn't perform as well as Darin's, but it's a nice example of LINQ:

public static class StringExtensions { public static bool IsNullOrWhiteSpace(this string value) { return value == null || value.All(char.IsWhiteSpace); } } 

1 Comment

Since .All() also returns true if value is empty, this is probably exactly what OP is looking for.
9

Maybe IsNullOrWhiteSpace is the method you are searching for? http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

Comments

3

I have used (in .NET v2.0):

public static class StringExtensions { public static bool IsNullOrEmptyOrWhitespace(this string value) { return string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim()); } } 

The Trim() method will remove all leading or trailing whitespace so if your string is entirely whitespace, it will be reduced to the empty string.

I can't say that performance has been an issue.

3 Comments

>>I have used (in .NET v2.0): How can you use Extension method in NET2.0 since it was introduced in NET3.5?
Originally, in VS2005 this was a utility method (remove 'this' from the first parameter). Later we moved to VS2008, while still targetting .net2.0 and fudged the use of extension methods (this is a compiler feature, not a .NET Framework feature) using this method: geekswithblogs.net/robp/archive/2007/12/20/…. Now we're on .NET 3.5 so we lost the fudge.
Doesn't this generate garbage and an allocation? Trim() will generate a new string since strings are immutable. It's a waste to trigger an allocation for such a simple operation. String manipulations should be avoided when possible.
2

Exact copy from Microsoft's source code for .NET 4 Framework, ..\RefSrc\Source.Net\4.0\DEVDIV_TFS\Dev10\Releases\RTMRel\ndp\clr\src\BCL\System\String.cs\1305376\String.cs

 public static bool IsNullOrEmpty(String value) { return (value == null || value.Length == 0); } public static bool IsNullOrWhiteSpace(String value) { if (value == null) return true; for(int i = 0; i < value.Length; i++) { if(!Char.IsWhiteSpace(value[i])) return false; } return true; } 

Remarks

(from http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx)

IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0; 

White-space characters are defined by the Unicode standard. The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

Comments

2

Funny enough nobody makes use of the Trim function here:

public static class StringExtensions { public static bool IsNullOrEmptyOrWhiteSpace(this string value) { return string.IsNullOrEmpty(value) || ReferenceEquals(value, null) || string.IsNullOrEmpty(value.Trim(' ')); } } 

Update: I see in the comments now it was proposed and rejected for various reasons, but there it is if one prefers brevity over efficiency...

4 Comments

How is that briefer than my version? I'd also suggest that using || is rather more readable than using the conditional operator here.
@Jon, it's not. I will restrain myself from posting on SO on Sundays :)
What a convoluted way to write some simple logic! I'm tempted to give it -1, but it's not "wrong", just not easy to read.
what about the "if" statement?
0

Pre .NET 4.0, the shortest:

public static bool IsNullOrWhiteSpace(this string value) { return value == null || value.Trim() == ""; } 

Not that efficient; Jon's is better considering readability and performance.

2 Comments

Note that the .Trim() creates an extra string that must be garbage collected, which the for/Char.IsWhiteSpace solutions do not.
@HansKesting yes, and also Trim() requires more traversal through the string than for/All.Char.IsWhiteSpace since in the latter, execution is stopped after first non whitespace char. As I said in the answer, its not efficient, but for 90% of the cases it shouldn't matter. Was giving the most succinct answer I could think of. Jon's answer is indeed the best I found here.