int expenseCode;
if (int.TryParse(sourceRecord.ExpenseCode, out expenseCode) && _ExpenseCodeLookup.ContainsKey(expenseCode))
{
destRow.PROFIT_CENTER_NAME = _ExpenseCodeLookup[expenseCode];
}
else
destRow.PROFIT_CENTER_NAME = "Unknown";
The thing I am conerned about is will the first expression always be run (setting expenseCode in the process) before the second operation?
That’s fine.
&&is short-circuiting in C#, and theoutparameter will definitely have been assigned the appropriate value byTryParsebeforeContainsKeyis called.On the other hand, you could use the same trick again to fetch the value:
This way you’re only doing the lookup of the expense code once.