相关文章推荐
睡不着的橡皮擦  ·  IllegalArgumentExcepti ...·  2 月前    · 
英姿勃勃的山羊  ·  给我一个spring ...·  7 月前    · 
严肃的青蛙  ·  android emulator for ...·  1 年前    · 
干练的茶壶  ·  catch pandas errors ...·  1 年前    · 
勤奋的香槟  ·  IntelliJ IDEA ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What is an elegant way to convert IDictionary<string, object> to IDictionary<string, string>

Ask Question

I have a method I am not happy with, can you please show me how to do this better.

public Foo WithBar(IDictionary<string, object> parameters) {
    var strStrDict = new Dictionary<string, string>(parameters.Count);
    foreach(var pair in parameters)
        strStrDict.Add(pair.Key, pair.Value != null ? pair.Value.ToString() : (string)null);
    // Call overload which takes IDictionary<string, string>  
    return this.WithBar(strStrDict);

This code works but I'm sure there is a nice linq'y way of doing this I am missing.

One reason you should be unhappy with your method: It's broken! pair.Value == null ? pair.Value.ToString() : (string)null should blow up quite nicely. – Anthony Pegram Apr 20, 2011 at 21:21 Thanks Anthony, I asked the question and went off to write the unit test. So just found that. – David Waters Apr 20, 2011 at 21:25 @dykam, absolutely not. as performs a reference conversion, which will return null if the object is not a string. ToString returns a string representation of the object, which is something completely different... – Thomas Levesque Apr 20, 2011 at 21:25 Technically, (string)null is not required. In the expression condition ? obj.ToString() : null; the compiler has enough information to determine the type of the expression to be a string. – Anthony Pegram Apr 20, 2011 at 21:27 @Thomas Levesque, I assumed he had a Dict<string, object> where all keys where strings, so he just needed a cast. That still doesn't make asequivalent to your code indeed. – Dykam Apr 21, 2011 at 9:23 Convert.ToString(null) return String.Empty msdn.microsoft.com/en-us/library/astxcyeh.aspx, and failed my unit test :) – David Waters Apr 20, 2011 at 21:33 Interesting twist. In LinqPad (about to test in VS), Convert.ToString(null) returns null, but object obj = null; Convert.ToString(obj) returns empty string. – Anthony Pegram Apr 20, 2011 at 21:38 @David Waters I see :) I apparently tested against the wrong Convert.ToString (see updated answer). @Anthony Pegram Yeah, it takes the most "concrete" overload it can. – Lasse Espeholt Apr 20, 2011 at 21:42 yes, the documentation supports your edit. When passed null explicitly, overload resolution is picking the string version, which returns the string unchanged (hence getting null back). Since null works for object and string, overload resolution chooses the more specific type, which is string. Edit: Or what you just said. – Anthony Pegram Apr 20, 2011 at 21:44

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.