Clever trap: Combining constructor with collection initializer

C# gives various methods for introducing accumulations.

On the off chance that an accumulation has a solitary parameter Add strategy, you can include things into the gathering as a major aspect of introduction:

Note that in the event that you don’t give a contention list after the sort, one will be accommodated you, to be specific ().

On the off chance that a gathering has a multi-parameter Add technique, you can include things into the accumulation as a major aspect of the instatement, yet you have to put supports around every parameter list:

On the off chance that an accumulation has a list setter, you can include things into the gathering with indexer linguistic structure:

You can’t join these diverse initializer documentations, be that as it may.

In any case, one thing that is now and again intriguing to do is consolidate the constructor with the gathering initializer. This gives you a chance to clone a gathering and afterward alter it.

You can pass whatever is a legitimate constructor parameter. For instance, List licenses development from any enumerable, so you can do this:

This mix documentation is helpful in the event that you need to clone a current gathering and, make a few changes to it.

Labels Code