ref var #854
Replies: 4 comments
-
I don't understand the example. If And when you want to always have a value, you can use long? personIndex = …;
var value = personIndex ?? 99;
Console.WriteLine(value); Do you have an example that uses the default value and can't be replaced by |
Beta Was this translation helpful? Give feedback.
-
I've updated my example. |
Beta Was this translation helpful? Give feedback.
-
How so? Compare: TaskCounts.TryGetValue(TaskID, ref var total = 0);
TaskCounts.TryGetValue(TaskID, out var total, 0); The second option doesn't look more messy to me. (Also, if the default you want is |
Beta Was this translation helpful? Give feedback.
-
Anyhow, I actually prefer TaskCounts.TryGetValue(TaskID, out var total) ? total : 0 |
Beta Was this translation helpful? Give feedback.
-
Hi C# team,
Ever since the introduction of "out var", I have found myself writing numerous methods that basically test a condition (actual return value) and return a value (out var) all in one step. A constant limitation that I am running into with these methods though, is the ability to specify a default value. I know that I can specify another parameter but that can make calling messy.
Ideally, I would like to be able to do things like:
I hope someone else finds the concept of ref var useful for when you want to "out var" something conditionally.
Beta Was this translation helpful? Give feedback.
All reactions