Feature Request: Create an operator that represents String.IsNullOrEmpty() #170
Replies: 9 comments
-
I use |
Beta Was this translation helpful? Give feedback.
-
I suppose |
Beta Was this translation helpful? Give feedback.
-
HasValue on Nullable could be convention-based so you could define an extension property for string with that effect. extension string {
public bool HasValue => string.IsNullOrEmpty(this);
}
var x = str ?? "empty"; Related: #147 |
Beta Was this translation helpful? Give feedback.
-
I write extension method in my own basis public static StringExt
{
public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str);
}
"abc".IsNullOrEmpty();
string s = null;
s.IsNullOrEmpty(); Should just have something like this in corefx |
Beta Was this translation helpful? Give feedback.
-
I almost never use Just as an example:
|
Beta Was this translation helpful? Give feedback.
-
Another way of doing this would be the ability to create custom operators: public static class StringExtensions
{
public static string operator (??!) IsNullOrEmpty(this string left, string right)
{
return string.IsNullOrEmpty(left) ? right : left;
}
} |
Beta Was this translation helpful? Give feedback.
-
I don't think this provides enough value to warrant language integration. The extension method call in the example is only a few characters longer than an expression with the new operator would be. I think it's fair that |
Beta Was this translation helpful? Give feedback.
-
If you use There's another way to do this I just thought of, too: |
Beta Was this translation helpful? Give feedback.
-
The arguments against this feature are understandable but I still believe having the specialized operator or the ability for the developer to create it would be very useful and in the end would help us be more productive. I'm sticking to my guns despite the down votes. @jamesqo I've already mentioned the use of extension methods in my initial post, that's the reason for my down vote on your comment. Your suggestion for the use of @eyalsk The fact that you almost never use IsNullOrEmpty is irrelevant, my argument is for a more efficient way of expressing the condition. You're comment actually helps my argument for less verbosity. @Thaina Again, I've already mentioned the use of extension methods in my initial post, please read thoroughly before commenting. @JamesFaix I strongly disagree with your opinion. @alrz Your option is intriguing and I will try it out but for the record I'd prefer the specialized operator. |
Beta Was this translation helpful? Give feedback.
-
In the same way that we have the null coalescing operator, I'd like to see one that can include an implicit check for empty strings.
Before
string result = String.IsNullOrEmpty(var1) ? var2 : var1;
//using an extension method:
string result = var1.IsNullOrEmpty() ? var2 : var1
After
string result = var1 ??! var2;
This would be a great way to clean up code!
Beta Was this translation helpful? Give feedback.
All reactions