How can I simplify conditional statement inside object initializer so that code will be more readible? If addNew is true, then new item is added to dictionary, otherwise it will have only one 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
}
}
...
new ChannelConf(bool addNew)if (addNew) { channel.Headers.Add(Constants.Key2, Port); }after the initialization is a vast improvement. Remember, initializers are only shorthand for after-the-fact property assignments anyway. You do not get a prize for squeezing everything in one block.