c# - Object initializer with conditional statement -




how can simplify conditional statement inside object initializer code more readible? if addnew true, new item added dictionary, otherwise have 1 item.

... var channel = new channelconf { name = "abc" headers = !addnew ?  new dictionary<string, string>            {                 [constants.key1] = id           }           : new dictionary<string, string>           {                [constants.key1] = id,                [constants.key2] = port           } } ... 

you call method initialize headers:

... new channelconf { name = "abc" headers = getnewdictionary(addnew) } ...  private dictionary<string, string> getnewdictionary(bool addnew) {     dictionary<string, string> output = new dictionary<string, string> { [constants.key1] = id };      if (addnew) { output.add(constants.key2, port); }      return output; } 

alternately, leave way , reduce number of lines:

... var channel = new channelconf { name = "abc" headers = !addnew ?  new dictionary<string, string>  { [constants.key1] = id }           : new dictionary<string, string> { [constants.key1] = id, [constants.key2] = port } } ... 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -