So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like:
var x = (someObje
I've had the exact same problem as you, but in the space of using LinqToXML a tag may not be in the document so using ??
isn't viable for coallescing the property on that tag.
Since there are no such thing as global variables in C# everything is in a class so I find, like you, that ?? is mostly useless.
Even a simple property getter like:
MyObject MyObj() { get { return _myObj ?? new MyObj(); } }
Is probably better served by an initializer...
I imagine that like in SQL it would be somewhat useful in Linq queries, but I can't really derive one at this moment.
I often use it for lazily instantiated objects in properties, e.g.
public MyObject MyProperty
{
get
{
return this.myField ?? (this.myField = new MyObject());
}
}
This takes advantage of the fact that in C# an assignment is an expression so you can assign and use the result of the expression (the assigned value) all in the same statement. I still feel slightly dirty about this code pattern, but it is terser than the alternative (below) so it just about wins.
public MyObject MyProperty
{
get
{
if (this.myField == null)
{
this.myField = new MyObject();
}
return this.myField;
}
}
It often seems to be a nice idea to replace a verbose bit of syntax with a more compact form. But usually it ends up just obfuscating the code without really gaining anything. Modern standards advocate longer and more descriptive variable names and use of more verbose layout styles in order to make code more readable & more maintainable.
If I'm going to train my brain to quickly parse ??, I need to find a situation where it shows a clear advantage - something that can't be done another way, or somewhere that it saves a significant amount of typing.
And this is where ?? falls down for me. It is useful so infrequently and ?: and if/else are such quick and readable alternatives that there really seems little point in using a different syntax that achieves nothing new.
a = b ?? c ?? d ?? e; sounds great. But I've never needed to do that!
What I find irritating is that I want to use ??, but every single time I think "ooh, maybe I can use ?? here", I realise that I don't want to use the object itself, but a member of it.
name = (user == null) ? "doe" : user.Surname;
And unfortunately, ?? doesn't help here.
In your case you can create DefaultObject as a static property and use like below ...
var x = (someObject as someType ?? someType.DefaultObject).someMember;
Where your DefaultObject will return a static, read-only object.
Like EventArgs.Empty, String.Empty etc...
I use the null-coalescing operator often when parsing strings into other data types. I have a set of extension methods that wrap things like Int32.TryParse and return a nullable int instead of exposing an output parameter to the rest of my code. Then I can parse a string and provide a default value if the parse failed, in a single line of code.
// ToNullableInt(), is an extension method on string that returns null
// if the input is null and returns null if Int32.TryParse fails.
int startPage = Request.QueryString["start"].ToNullableInt() ?? 0;
The most useful is when dealing with nullable types.
bool? whatabool;
//...
bool realBool = whatabool ?? false;
without ?? you would need to write the following, which is much more confusing.
bool realbool = false;
if (whatabool.HasValue)
realbool = whatabool.Value;
I find this operator very useful for nullable types, but other than that I don't find myself using it very much. Definitely a cool shortcut.