Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 363 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
?? Coalesce for empty string?

#1
Something I find myself doing more and more is checking a string for empty (as in `""` or null) and a conditional operator.

A current example:

s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;

This is just an extension method, it's equivalent to:

string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;

Since it's empty and not null, `??` won't do the trick. A `string.IsNullOrEmpty()` version of `??` would be the perfect solution. I'm thinking there has to be a cleaner way of doing this (I hope!), but I've been at a loss to find it.

Does anyone know of a better way to do this, even if it's only in .Net 4.0?
Reply

#2
There isn't a built-in way to do this. You could make your extension method return a string or null, however, which would allow the coalescing operator to work. This would be odd, however, and I personally prefer your current approach.

Since you're already using an extension method, why not just make one that returns the value or a default:

string result = s.SiteNumber.ConvertNullOrEmptyTo("No Number");
Reply

#3
how about a string extension method ValueOrDefault()

public static string ValueOrDefault(this string s, string sDefault)
{
if (string.IsNullOrEmpty(s))
return sDefault;
return s;
}

or return null if string is Empty:

public static string Value(this string s)
{
if (string.IsNullOrEmpty(s))
return null;
return s;
}

Didn't try these solutions though.
Reply

#4
A slightly faster extension method than proposed earlier perhaps:

public static string Fallback(this string @this, string @default = "")
{
return (@this == null || @this.Trim().Length == 0) ? @default : @this;
}
Reply

#5
I'm using a string Coalesce extension method of my own. Since those here are using LINQ, and absolutelly wasting resources for time intensive operations (I'm using it in tight loops), I'll share mine:

public static class StringCoalesceExtension
{
public static string Coalesce(this string s1, string s2)
{
return string.IsNullOrWhiteSpace(s1) ? s2 : s1;
}
}

I think it is quite simple, and you don't even need to bother with null string values. Use it like this:

string s1 = null;
string s2 = "";
string s3 = "loudenvier";
string s = s1.Coalesce(s2.Coalesce(s3));
Assert.AreEqual("loudenvier", s);

I use it a lot. One of those "utility" functions you can't live without after first using it :-)
Reply

#6
I like the brevity of the following extension method `QQQ` for this, though of course an operator like? would be better. But we can 1 up this by allowing not just two but three string option values to be compared, which one encounters the need to handle every now and then (see second function below).

#region QQ

[DebuggerStepThrough]
public static string QQQ(this string str, string value2)
{
return (str != null && str.Length > 0)
? str
: value2;
}

[DebuggerStepThrough]
public static string QQQ(this string str, string value2, string value3)
{
return (str != null && str.Length > 0)
? str
: (value2 != null && value2.Length > 0)
? value2
: value3;
}


// Following is only two QQ, just checks null, but allows more than 1 string unlike ?? can do:

[DebuggerStepThrough]
public static string QQ(this string str, string value2, string value3)
{
return (str != null)
? str
: (value2 != null)
? value2
: value3;
}

#endregion
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through