c# - Better way to Code Dictionary Lookup? -
probably simple issue, in writing code have mapping dictionary, found handling nulls , such has made code disgusting. there better way this?
int brokerid = 0; // set default value in case nothing found if(mytrade.counterparty!=null) // dont bother if counterparty null if (resolutionmap.result["broker"].containskey((mytrade.counterparty))) if (resolutionmap.result["broker"][mytrade.counterparty] != null) primebrokerid = convert.toint32(resolutionmap.result["broker"][mytrade.counterparty]);
store value of resolutionmap.result["broker"] variable rather looking multiple times e.g. var broker = resolutionmap.result["broker"];.
also use trygetvalue() instead of containskey() e.g.
counterparty counterparty; if (broker.trygetvalue(mytrade.counterparty, out counterparty) && counterparty != null) { ....
Comments
Post a Comment