Assign and check for a null value at the same time?
c#
Solution
A variable assignment also returns the value. So the syntax in the form of the following will do:
SomeType someVariable;
if ((someVariable = valueToAssign) != null)
{
// valueToAssign was not null
}
In your case:
XElement children;
if ((children = xml.Descendants(ns + "Children").FirstOrDefault()) != null)
{
}
Problem
I have the following scenario: ``` if(xml.Descendants(ns + "Children").FirstOrDefault() != null) { XElement children = xml.Descendants(ns + "Children").FirstOrDefault(); } ``` Is there a way I can check for null and assign the value at the same time instead of having to search for the value twice, something similar to: ``` //Not sure if this is correct. if (XElement children = xml.Descendants(ns + "Children").FirstOrDefault() != null) { } ```