问题
I was wondering about ??
signs in C#
code. What is it for? And how can I use it?
What about int?
? Is it a nullable int?
See also:
?? Null Coalescing Operator —> What does coalescing mean?
回答1:
It's called the "null coalescing operator" and works something like this:
Instead of doing:
int? number = null;
int result = number == null ? 0 : number;
You can now just do:
int result = number ?? 0;
回答2:
It's the null coalescing operator. It was introduced in C# 2.
The result of the expression a ?? b
is a
if that's not null, or b
otherwise. b
isn't evaluated unless it's needed.
Two nice things:
The overall type of the expression is that of the second operand, which is important when you're using nullable value types:
int? maybe = ...; int definitely = maybe ?? 10;
(Note that you can't use a non-nullable value type as the first operand - it would be pointless.)
The associativity rules mean you can chain this really easily. For example:
string address = shippingAddress ?? billingAddress ?? contactAddress;
That will use the first non-null value out of the shipping, billing or contact address.
回答3:
That is the coalesce operator. It essentially is shorthand for the following
x ?? new Student();
x != null ? x : new Student();
MSDN Documentation on the operator
- http://msdn.microsoft.com/en-us/library/ms173224.aspx
回答4:
It's the new Null Coalesce operator.
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
You can read about it here: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx
回答5:
It is a shortcut for:
Text = (category == null ? "Home" : category);
回答6:
it's the coalesce operator. it will return another value if the first value is null
string value1 = null;
string value2 = "other";
string value3 = value1 ?? value2; // assigns "other" to value 3
回答7:
it checks if category is null - when this is the case the null value is replaced by "Home".
回答8:
One of my favorite uses for the null coalescing operator is to avoid if statements in my code (I think if statements are ugly and just clutter things up most times). For example, take a typical scenario where one might choose to load something from cache if available, otherwise load from the db and populate the cache.
private SomeData GetData() {
var data = HttpRuntime.Cache.Get("key") as SomeData;
if (data == null) {
data = DAL.GetData(some parameters...);
HttpRuntime.Cache.Add("key", data, ....);
}
return data;
}
To me, that's ugly code. I may be a bit anal, but why not refactor it to this instead?
private SomeData GetDataAndCache() {
var data = DAL.GetData(some parameters...);
HttpRuntime.Cache.Add("key", data, ....);
return data;
}
private SomeData GetData() {
var data = HttpRuntime.Cache.Get("key") as SomeData;
return data ?? GetDataAndCache();
}
It more closely follows SRP and is cleaner and easier to read, IMO. The functions perform exactly one clearly identifiable function each.
回答9:
if category is null, Text will become "Home"
回答10:
Returns the first not-null value. Handy.
回答11:
?? Null-Coalescing Operator
int? is a nullable int, which means it can have the values of a normal int and null. Read this for details.
回答12:
That's the null-coalescing operator . It's used with nullable types (among other things, sorry :)
来源:https://stackoverflow.com/questions/827454/what-is-the-operator-for