Is it possible to override the null-coalescing operator for a class in C#?
Say for example I want to return a default value if an instance is null and return the instance if it's not. The code would look like something like this:
return instance ?? new MyClass("Default");
But what if I would like to use the null-coalescing opera...
Is there a VB.NET equivalent for C#'s ?? operator?
...
Is there a null coalescing operator in Javascript?
For example, in C#, I can do this:
String someString = null;
var whatIWant = someString ?? "Cookies!";
The best approximation I can figure out for Javascript is using the conditional operator:
var someString = null;
var whatIWant = someString ? someString : 'Cookies!';
Which is so...
Hi guys,
Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#?
...
It would be really nice if C# allowed an ??= operator. I've found myself writing the following frequently:
something = something ?? new Something();
I'd rather write it like this:
something ??= new Something();
Thoughts? New language extensions are always controversial by their nature.
...
I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me.
I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense.
Can someone enlighten...
I was wondering about "??" signs in c# code.. what is it for? And how can i use it?
what about "int?"? is it nullable int?
See also:
?? Null Coalescing Operator —> What does coalescing mean?
...
Everyone knows that this is not thread safe:
public StringBuilder Builder
{
get
{
if (_builder != null)
_builder = new StringBuilder();
return _builder;
}
}
What about this?
public StringBuilder Builder
{
get { return _builder ?? (_builder = new StringBuilder()); }
}
...
I've looked around a little and haven't found an equivalent question.
Is this bad coding practice? I can read it easily, but is it too cryptic for someone reading the code?
bool? testBool = null;
string result;
result = (testBool ?? false ? "Yes" : "No");
Edit: My apologies to everyone for some horrible code! Here is a working e...
I have a method that will receive a string, but before I can work with it, I have to convert it to int. Sometimes it can be null and I have to change its value to "0". Today I have:
public void doSomeWork(string value)
{
int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know)
}
I did it, but my boss asked me t...
I'm writing a C# routine to call a stored proc. In the parameter list I'm passing in, it is possible that one of the values can legally be null. So I thought I'd use a line like this:
cmd.Parameters.Add(new SqlParameter("@theParam", theParam ?? DBNull.Value));
Unfortunately, this returns the following error:
CS0019: Operator '??...
I have a number of methods doing next:
var result = command.ExecuteScalar() as Int32?;
if(result.HasValue)
{
return result.Value;
}
else
{
throw new Exception(); // just an example, in my code I throws my own one
}
I wish I could use operator ?? like this:
return command.ExecuteScalar() as Int32? ?? throw new Eception();
but ...
is there a C++ equivalent for C# null coalescing operator? i am doing too many null checks in my code.. so was looking for a way to reduce the amount of null code
...
myFoo = myFoo ?? new Foo();
instead of
if (myFoo == null) myFoo = new Foo();
Am I correct in thinking that the first line of code will always perform an assignment? Also, is this a bad use of the null-coalescing operator?
...
I started to really like C#'s ?? operator. And I am quite used to the fact, that where there is something handy in some language, it's most probably in Perl too.
However, I cannot find ?? equivalent in Perl. Is there any?
...
Below is some linqpad test code. When this runs it errors because the second instance of "item" has a null list of subitems as opposed to an empty list.
I want to treat both situations (null or empty list) in exactly the same way but I wondered if there was a cleaner way than just putting a null check on the list and initialising an emp...
Everyone knows at least two common c# idioms including coalesce operator:
a singleton one:
return _staticField = _staticField ?? new SingletonConstructor();
and a chain one:
notNullableResult = nullable1 ?? nullable2 ?? nullable3 ?? default(someType);
it's readable, consistent, worth using and recognizable in code.
But, unfortu...
If this is a duplicate please point me to it and I'll close, I couldn't find anything. 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 equivale...
Do you think that C# will support something like ??= operator?
Instead of this:
if (list == null)
list = new List<int>();
It might be possible to write:
list ??= new List<int>();
Now, I could use (but it seems to me not well readable):
list = list ?? new List<int>();
...
Is it possible to use together any way operator ?? and operator && in next case:
bool? Any
{
get
{
var any = this.ViewState["any"] as bool?;
return any.HasValue ? any.Value && this.SomeBool : any;
}
}
This means next:
if any is null then this.Any.HasValue return false
if any has value, then it returns value cons...