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#2y 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
More Posts
❔ Something weird with toolbox (duplicated options)please have a look at my screenshot❔ ✅ comprehension questions for a LeetCode puzzleI am analysing the following LC solution (the iterative one) https://leetcode.com/problems/n-ary-treNo applicable method 'Contains' exists in type 'String' (at index 31) when using KendoNet.DymaicLinqI have a a web app that I've built using Kendo's component library and I'm trying to introduce backe❔ string modifyhow can i transform string like this `1234 5678 9123 4567` to this `1234 **** **** 4567` in a lambda❔ How to differentiate in C#?How can I differentiate a function like: "f(x)=sqrt(ax^2+b+cx)+gx+p+...etc" and every time it loops ✅ Problem With MYSQL Connection Using Entity FrameworkHello all, I am having problems to connect MYSQL. I think it can be related with my connection strin❔ About .NET Forms in Resources.resx files, what is the cost of removing PublicKeyToken?Second time in a couple months where my project breaks PublicKeyToken thanks to VisualStudio updatesautomated event handleri'm trying to index my bot's client with an event name but it throws an error, what's a way i can ac❔ is it possible to add // at beginning of text line when caret is in current line?Hello all, in a RichTextBox I split text by line, but I need to add the symbol **//** at the beginni✅ Schedule event at future DateTime based on DateOnly input, doesn't work if called <2min before```public static void ScheduleMethod(DateOnly inputDate) { DateTime triggerTime = new Da