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.
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
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.
The string
class is not declared as partial, you will have to write an extension method instead.
public static class MyExtensions
{
public static bool IsNullOrWhiteSpace(this string value)
{
if (value == null) return false;
return string.IsNullOrEmpty(value.Trim());
}
}
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());
}
}
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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.