null-coalescing-operator

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 terna...

Is there an "opposite" to the null coalescing operator? (…in any language?)

null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y;, where what follows th...

How to assign/add item(s) to a Dictionary using the key index?

We wanted to assign/add an item to a Dictionary using the key index, like this: Dictionary<string, object> dict = new Dictionary<string, object>(); dict["key"] = dict["key"] ?? "object"; But it results to: "The given key was not present in the dictionary." Is there any way we could assign values to this dictionary the same as the way...

Null-coalescing operator and lambda expression

Hello, take a look at the following code I attempted to write inside a constructor: private Predicate<string> _isValid; //... Predicate<string> isValid = //...; this._isValid = isValid ?? s => true; The code doesn't compile - just "invalid expression term"s and so one. In contrast that does compile and I could just use it: this._...

Weird operator precedence with ?? (null coalescing operator)

Recently I had a weird bug where I was concatenating a string with an int? and then adding another string after that. My code was basically the equivalent of this: int? x=10; string s = "foo" + x ?? 0 + "bar"; Amazingly enough this will run and compile without warnings or incompatible type errors, as will this: int? x=10; string s ...

C# ?? null coalescing operator question

I have defined Class Person property Birthday as nullable DateTime? , so why shouldn’t the null coalescing operator work in the following example? cmd.Parameters.Add(new SqlParameter("@Birthday", SqlDbType.SmallDateTime)).Value = person.Birthday ?? DBNull.Value; The compiler err I got was "Operator '??' cannot be appl...

Is there a more elegant way to add nullable ints?

I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g. can't I chain these statements together somehow, I've seen that before in other code. using System; namespace TestNullInts { class Prog...

Using null coalescing in foreach statement

Hey All Trying to figure out how to get the null coalescing operator to work in a foreach loop. I'm checking to see what a string ends with and based on that, route it to a certain method. Basically what I want to say is.... foreach (String s in strList) { if s.EndsWith("d") ?? Method1(s) ?? Method2(s) ?? "Unknown file type"; } ...

What does this ?? notation mean here

Possible Duplicate: What is the ?? operator for? What does the ?? notation mean here? Am I right in saying: Use id, but if id is null use string "ALFKI" ? public ActionResult SelectionClientSide(string id) { ViewData["Customers"] = GetCustomers(); ViewData["Orders"] = GetOrdersForCustomer(id ?...

Coalesce operator in C#?

I think i remember seeing something similar to the ?: ternary operator in C# that only had two parts to it and would return the variable value if it wasn't null and a default value if it was. Something like this: tb_MyTextBox.Text = o.Member ??SOME OPERATOR HERE?? "default"; Basically the equivalent of this: tb_MyTextBox.Text = o.Me...

What is the result if all parameters in a null coalescing operation are null?

When this code finishes, what is the result of myObject? object myObject = "something"; object yourObject = null; myObject = null ?? yourObject; ...