Is there any shorthand in c# now that will cutdown the following code:
var testVar1 = checkObject();
if (testVar1 != null)
{
testVar2 = testVar1;
}
In this situation only want to assign testVar2 if testVar1 is not null from the CheckObject() result (testVar2 has a setter that will fire off code). Was trying to think how could use the null coalesce stuff but not really working out.
Adding on to this testVar2 has code on it's setter to fire, so do not want testVar2 being set to anything if the value is null.
public MyObj testVar2
{
get { return _testVar2; }
set
{
_testVar2 = value;
RunSomeCode();
}
}
testVar2 = testVar1 ?? testVar2;
; means it will settestVar2
totestVar1
if its not null, if it is null it will settestVar2
totestVar2
. Example: dotnetfiddle.net/hUPYDU – Recency