Does C# Have An Operation Similar to JavaScript's || Setter? - c#

Does C# have a similar operation to JavaScript's || setter?
For example, in JavaScript, if I want to check if a value is null and set a default, I can do something like this:
function foo(val){
this.name = val || "foo";
}
But, when I want this functionality in C# I have to go this route:
public Foo(string val)
{
this.name = (string.IsNullOrEmpty(val) ? "foo" : val);
}
I know this is petty and that beggars can't be choosers but, if at all possible, I'd like to avoid getting any answers that involve crazy hacks or extension methods. I'm fine with using the ternary operator, but I was just curious if there was a language feature I'd missed.
Update:
Great answers all around, but J0HN and Anton hit it on the head. C# doesn't do "falsy" values like JavaScript would in the example above, so the ?? operator doesn't account for empty strings.
Thanks for your time and the great responses!

There's a null-coalescing operator in C#, though it can't handle all the quirkiness of JavaScript:
this.name = val ?? "foo";
Since an empty string is false in JavaScript, this C# code will behave differently from its JS counterpart.

There is a ?? operator that essentially is the same as COALESCE operator in SQL:
int? a = null; //int? - nullable int
int q = a??1; // q is set to one;
However, ?? does not check the string for emptiness, so it does not share the same meaning as javascript's ||, which treats empty strings as false as well.

You can use ??:
private int Foo(string val){
this.name = val ?? "foo";
}

Go with this
this.name = val ?? "foo";

Yep, use ??:
private int Foo(string val){
this.name = val ?? "foo";
}
Check Msdn for more information: ?? Operator

Related

How can I declare a new operator "!??" (test object is not null)

Is there a way to declare a unary operator such as '!??'
I imagine something like this (non working code)
public bool operator !??<T> (this <T> item) {
return item != null;
}
so I would use it like such
// day is a value and might possibly be null
string greeting = name !?? "hello there " + name;
I find myself often having to do this awkwardness
// day is a value and might be null
string greeting = day != null ? "hello there " + name : "";
It's fairly clear in the example I've provided, but when you are using getter/setters for view-models, it gets a bit confusing to stare at, and raises the chance of a logical error being missed. As such:
public DateTime? SearchDate {
get { return _searchDate; }
set { _searchDate = value != null ? value.Value : value; }
}
This is completely impossible.
Instead, you can use C# 6's ?. operator:
value?.Value
As has been said, you cannot declare new operators in C#, ever, at all. This is a limitation of the language, the compiler, and the environment.
What you CAN do is utilize the null coalescing operator or write a few generic Extension Methods to handle what you want, such as
public static string EmptyIfNull(this string source, string ifValue, string ifNull = "")
{
return source != null ? ifValue : ifNull;
}
and implemented via
string greeting = name.EmptyIfNull("hello there " + name);
C# does not let you define completely new operators, you can only overload a specific set of existing operators.
There is no way to declare new operators in C# at all, all you can do is override the existing ones.

Avoiding NullReferenceException in Request.QueryString

This code throws a NullReferenceException if mode is not specified in the pages query string:
bool isAdvancedMode = Request.QueryString["mode"].Equals("advanced");
This is how I work around this:
bool isAdvancedMode = (Request.QueryString["mode"] + "").Equals("advanced");
Is this standard practise, or a hack?
You can use the null-coalescing operator:
bool isAdvancedMode = (Request.QueryString["mode"] ?? String.Empty).Equals("advanced");
Edit: If you want to re-use this logic, try this extension method:
public static bool EqualIfExists(this string source, string comparison)
{
return source != null && source.Equals(comparison);
}
Request.QueryString["mode"].EqualIfExists("advanced")
Add more overrides to match Equals signature. I'm not sure if this is a good name (I think it is not).
Well, I would recommend this instead:
bool isAdvancedMode = (Request.QueryString["mode"] ?? "").Equals("advanced");
In fact, this is what your code compiles to (Nearer the bottom, but it's a good read so I'd read it all). Yours is good practice, but this is a bit more clear.
Why not use the null coalescing operator?
bool isAdvancedMode = (Request.QueryString["mode"] ?? String.Empty).Equals("advanced");
Different approach, while a bit more code, I think is more clear the intent.
bool isAdvancedMode = String.IsNullOrWhitespace(Request.QueryString["mode"]) ?
false : Request.QueryString["mode"].Equals("advanced")
what about this
bool isAdvancedMode=(Request.QueryString["mode"] ?? string.Empty).Equals("advanced");

Concise usage of DBNull? (Ternary?)

It seems that there's some type confusion in the ternary operator. I know that this has been addressed in other SO threads, but it's always been with nullables. Also, for my case I'm really just looking for a better way.
I'd like to be able to use
proc.Parameters[PARAM_ID].Value =
string.IsNullOrEmpty(dest.Id) ? DBNull.Value : dest.Id;
but instead I'm stuck with this:
if (string.IsNullOrEmpty(dest.Id))
{
proc.Parameters[PARAM_ID].Value = DBNull.Value;
}
else
{
proc.Parameters[PARAM_ID].Value = dest.Id;
}
The ternary operator fails because there's no conversion possible between DBNull and string, and as silly as that seems considering Value is object, the compiler kicks it back to me and I'm forced to care. The answer to the nullable version of this question is to just cast null to string and be done with it; DBNull can't be cast to string, though, so no luck there.
Is there a more concise way to do this (without using nullables, by the way?)
Thanks!
You could change your first statement to:
proc.Parameters[PARAM_ID].Value =
string.IsNullOrEmpty(dest.Id) ? (object)DBNull.Value : dest.Id;
The Value property is of type object, so you should cast to object, not string:
proc.Parameters[PARAM_ID].Value = string.IsNullOrEmpty(dest.Id)
? (object)DBNull.Value
: (object)dest.Id;
Or you could add an extension method such as:
public static class DBNullExtensions
{
public static object AsDBNullIfEmpty(this string value)
{
if (String.IsNullOrEmpty(value))
{
return DBNull.Value;
}
return value;
}
}
And then you could just say
proc.Parameters[PARAM_ID].Value = dest.Id.AsDBNullIfEmpty();
(Adapted from Phil Haack)
Readable and concise, no?
What about using the ?? null-coalescing operator More details about ?? operator
proc.Parameters[PARAM_ID].Value = dest.Id ?? (object)DBNull.Value;

Negate the null-coalescing operator

I have a bunch of strings I need to use .Trim() on, but they can be null. It would be much more concise if I could do something like:
string endString = startString !?? startString.Trim();
Basically return the part on the right if the part on the left is NOT null, otherwise just return the null value. I just ended up using the ternary operator, but is there anyway to use the null-coalescing operator for this purpose?
You could create an extension method which returns null when it tries to trim the value.
public String TrimIfNotNull(this string item)
{
if(String.IsNullOrEmpty(item))
return item;
else
return item.Trim();
}
Note you can't name it Trim because extension methods can't override instance methods.
Not to spec: Not that I like it, but you could use:
string endString = (startString ?? String.Empty).Trim();
To spec, better as an Extension method like #Kevin's:
string endString = (startString == null ? null : startString.Trim());
string endString = string.IsNullOrEmpty(startString) ? startString : startString.Trim();
Though I've also gone the route of writing a string extension method called "safeTrim" which does what you're describing in one method instead of having to use this recipe every time. Check out Kevin's respone for the code.
EDIT: wow I had it all kinds of backwards, wrongly named variables and reversed ternary operators, all the more reason to write one extension method and code check it better than I did!
Starting with C# 6.0 (.NET Framework 4.6 / Visual Studio 2015) you can use null-conditional member access:
string? endString = startString?.Trim();
Sorry for the necromancy, but I was having this same problem and I solved this using a lambda operation. It isn't the prettiest, but it keeps my code succinct.
It's a shame C# doesn't support static imports or individual function imports, but anyway:
Define this function somewhere:
private static TResult N<TParent,TResult>(TParent parent, Func<TParent,TResult> operation) {
if( parent == null ) return default(TResult);
return operation( parent );
}
Then to use it in your example:
String endString = N(startString, s => s.Trim());
The N function returns null if the first argument is null, otherwise it will evaluate the specified lambda function with the value as the argument.
You can nest it, of course, like so. For example, to safely dereference a long chain, e.g.
String someValue = someObject.SomeProperty.SomeOtherProperty.SomeMethod().SomeFinalProperty;
if any of those properties or methods returns null then you have to insert null checks everywhere, or you could do this:
String someValue = N(N(N(N(someObject, o => o.SomeProperty), o => o.SomeOtherProperty), o => o.SomeMethod()), o => o.SomeFinalProperty);
As I said, it isn't the prettiest :)
You could simplify this by making N an extension method of System.Object, like so:
String someValue = someObject.N( o => o.SomeProperty ).N( o => o.SomeOtherProperty ).N( o => o.SomeMethod() ).N( o => o.SomeFinalProperty );
...which I think is a lot tidier.
Use
string endString = (startString ?? "").Trim();
This uses an empy string if startString is null. This, however, does not return null when endString is null.
Fast-forward to 2021:
10 years later startString?.Trim() is definitely the better option. And this does return null.
The following doesn't propagate null but it accepts null as a parameter and returns an empty string in that case.
using Microsoft.VisualBasic; // you need to add a reference to Microsoft.VisualBasic.dll
...
string endString = Strings.Trim(startString);
...
duck&run...
As as side note, if you're using .NET 4, there's a new convenient method String.IsNullOrWhiteSpace which you can use.

Shortest way to check for null and assign another value if not

I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null. I'm currently doing it like this:
if (string.IsNullOrEmpty(planRec.approved_by) == true)
this.approved_by = "";
else
this.approved_by = planRec.approved_by.toString();
There seems like there should be a way to do this in a single line something like:
this.approved_by = "" || planRec.approved_by.toString();
However I can't find an optimal way to do this. Is there a better way or is what I have the best way to do it?
Try this:
this.approved_by = IsNullOrEmpty(planRec.approved_by) ? "" : planRec.approved_by.toString();
You can also use the null-coalescing operator as other have said - since no one has given an example that works with your code here is one:
this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();
But this example only works since a possible value for this.approved_by is the same as one of the potential values that you wish to set it to. For all other cases you will need to use the conditional operator as I showed in my first example.
Starting with C# 8.0, you can use the ??= operator to replace the code of the form
if (variable is null)
{
variable = expression;
}
with the following code:
variable ??= expression;
More information is here
You are looking for the C# coalesce operator: ??. This operator takes a left and right argument. If the left hand side of the operator is null or a nullable with no value it will return the right argument. Otherwise it will return the left.
var x = somePossiblyNullValue ?? valueIfNull;
The coalesce operator (??) is what you want, I believe.
My guess is the best you can come up with is
this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty
: planRec.approved_by.ToString();
Of course since you're hinting at the fact that approved_by is an object (which cannot equal ""), this would be rewritten as
this.approved_by = (planRec.approved_by ?? string.Empty).ToString();
With C#6 there is a slightly shorter way for the case where planRec.approved_by is not a string:
this.approved_by = planRec.approved_by?.ToString() ?? "";
Use the C# coalesce operator: ??
// if Value is not null, newValue = Value else if Value is null newValue is YournullValue
var newValue = Value ?? YourNullReplacement;
To extend #Dave's answer...if planRec.approved_by is already a string
this.approved_by = planRec.approved_by ?? "";
The accepted answer was correct in time, when it was given.
For people still finding this question:
Today you can use the ??= Operator.
e.g:
private string _test = null;
private void InitIfNull(){
_test ??= "Init";
}
To assign a non-empty variable without repeating the actual variable name (and without assigning anything if variable is null!), you can use a little helper method with a Action parameter:
public static void CallIfNonEmpty(string value, Action<string> action)
{
if (!string.IsNullOrEmpty(value))
action(value);
}
And then just use it:
CallIfNonEmpty(this.approved_by, (s) => planRec.approved_by = s);
You can also do it in your query, for instance in sql server, google ISNULL and CASE built-in functions.
I use extention method SelfChk
static class MyExt {
//Self Check
public static void SC(this string you,ref string me)
{
me = me ?? you;
}
}
Then use like
string a = null;
"A".SC(ref a);

Categories

Resources