I want to use a collection initializer for the next bit of code:
public Dictionary GetNames()
{
Dictionary names =
If you're looking for slightly less verbose syntax you can create a subclass of Dictionary
(or whatever your type is) like this :
public class DebugKeyValueDict : Dictionary
{
}
Then just initialize like this
var debugValues = new DebugKeyValueDict
{
{ "Billing Address", billingAddress },
{ "CC Last 4", card.GetLast4Digits() },
{ "Response.Success", updateResponse.Success }
});
Which is equivalent to
var debugValues = new Dictionary
{
{ "Billing Address", billingAddress },
{ "CC Last 4", card.GetLast4Digits() },
{ "Response.Success", updateResponse.Success }
});
The benefit being you get all the compile type stuff you might want such as being able to say
is DebugKeyValueDict
instead of is IDictionary
or changing the types of the key or value at a later date. If you're doing something like this within a razor cshtml page it is a lot nicer to look at.
As well as being less verbose you can of course add extra methods to this class for whatever you might want.