I have a simple dictionary call result:
Dictionary<String,String> results=new();
results["a"]="a";
results["b"]="b";
results["c"]="c";
To simplify the example my dictionary only contains 3 letter keys a,b,c. But sometimes it will not contain one of this values or even none, (it will always be initiallized). suppose this situation:
Dictionary<String,String> results=new();
if(anyconditionA) results["a"]="a";
if(anyconditionB)results["b"]="b";
if(anyconditionC)results["c"]="c";
So each time i want to operate with this dictionary i have to check the key value: var test= results["a"]; -> throws System.Collections.Generic.KeyNotFoundException if anycontitionA is not true. so to solve this i do:
if(results.ContainsKey("a"){
someOperation(results["a"]);
}
so if i have many values code looks like:
if(results.ContainsKey("a"){someOperation(results["a"]);}
if(results.ContainsKey("b"){... stuff}
if(results.ContainsKey("c"){... stuff}
if(results.ContainsKey("..."){... stuff}
if(results.ContainsKey("d"){someOperation(results["d"]);}
¿Is there a propper way to do this in one statement i mean check and do the operation if exists or i have to test every time that value exists? (like do with null operator in a list something like results[a]?.someOperation() ) Thanks!