C
C#2y ago
morry329#

✅ Operator ?? functions

I came across ?? operators whilst I try to understand solutions like https://leetcode.com/problems/n-ary-tree-preorder-traversal/solutions/3065645/not-fast-but-good-solution-without-any-recursion/ For example there is this line of code
root.children[0].children = new List<Node>(root.children[0].children ?? new List<Node>()) {root.children[i]};
root.children[0].children = new List<Node>(root.children[0].children ?? new List<Node>()) {root.children[i]};
` In this case the ?? operator is ordering the IDE like "hey, the value root.children[0].children can be the value of root.children at the ith position ?
LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
9 Replies
rotor_
rotor_2y ago
The ?? operator just means "if that was null then try this" So var val = nullableType ?? defaultValueThatIsNotNull; Or if you're feeling adventurous return a ?? b ?? c ?? throw new NullReferenceException(); Although you might not be permitted an exception there let me check that Looks like you can't use an exception there pepehands
Anton
Anton2y ago
here's what the code actually does
var firstChild = root.children[0];
if (firstChild.children is not null)
firstChild.children = MakeShallowCopy(firstChild.children);
else
firstChild.children = MakeShallowCopy(new List<Node>(root.children[i]));
var firstChild = root.children[0];
if (firstChild.children is not null)
firstChild.children = MakeShallowCopy(firstChild.children);
else
firstChild.children = MakeShallowCopy(new List<Node>(root.children[i]));
it honestly doesn't make much sense
rotor_
rotor_2y ago
I disagree. The code reads
root.children[0].children = new List<Node>(
root.children[0].children ?? new List<Node>()
) {
root.children[i]
};
root.children[0].children = new List<Node>(
root.children[0].children ?? new List<Node>()
) {
root.children[i]
};
i.e.
var firstChild = root.children[0];
var newChildren = firstChild.children is not null
? firstChild.children
: new List<Node>()
var firstChildChildren = new List<Node>(newChildren);

firstChildChildren.Add(root.children[i]);

root.children[0].children = firstChildChildren;
var firstChild = root.children[0];
var newChildren = firstChild.children is not null
? firstChild.children
: new List<Node>()
var firstChildChildren = new List<Node>(newChildren);

firstChildChildren.Add(root.children[i]);

root.children[0].children = firstChildChildren;
Monsieur Wholesome
I dont know what youre talking about, catHeyHello
rotor_
rotor_2y ago
Whoops a dodgy test. Must've been because of the null literal
Anton
Anton2y ago
ah yeah you're right about the add. but it also makes a copy of children, you're wrong there
rotor_
rotor_2y ago
Ah yes good point it's unpacking and repacking the children how bizarre There I've edited my original code to fix it
morry329#
morry329#OP2y ago
Thank you all for the explanation, I see this operator more clearly than before now 🙂
Want results from more Discord servers?
Add your server