Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify Building Dictionaries

I'm trying to send a Dictionary as a parameter to a method. It works, but it's getting a bit tedious. I'm doing it a million times and I'd like to do it a little slicker if at all possible. Right now, this is how it looks:

MyMethod(new Dictionary<string, object> { { string1, value1 }, { string2, value2 } });

I'd like to get it to look more like this:

MyMethod({ string1, value1 }, { string2, value2 });

Is this a pipe dream? Any ideas on how to do that?

like image 572
vbullinger Avatar asked Jun 01 '26 04:06

vbullinger


1 Answers

I don't think there's any way to get the type of syntax you're looking for. The closest, and shortest thing I can think of would be to define something like:

using D = System.Collections.Generic.Dictionary<string, object>;

And then you could do:

MyMethod(new D {{string1, value1},{string2, value2}});

Which is a bit shorter, but probably still not ideal.

like image 169
Eric Avatar answered Jun 03 '26 16:06

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!