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...
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...
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...
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._...
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 ...
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...
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...
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";
}
...
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 ?...
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...
When this code finishes, what is the result of myObject?
object myObject = "something";
object yourObject = null;
myObject = null ?? yourObject;
...