1

maybe this is a stupid question, but I would like to know why this happend?

I'm creating a dictionary as follows :

new Dictionary<string, object>(){{ "latestVersion", true }}

but when i trying to get the value to send as queryString parameters with this :

var x = queryParameters.Select(x => x.Key + "=" + x.Value);

the result is : "latestVersion=True"

Why is changing the value from true to True, as the services is expecting a boolean value is failing.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Ivan Fontalvo
  • 433
  • 4
  • 21
  • 2
    It implicitly convert to a `string` and calls the `ToString()` for that. My guess is that in the `ToString()` it is printed as "True". Try this in a console application: `var result = true;` `Console.WriteLine(result);` – Paul Sinnema Jul 15 '22 at 20:35
  • Take a look here: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/Boolean.cs. It contains the code for the `boolean`. Look for `ToString()`. – Paul Sinnema Jul 15 '22 at 20:45
  • But in this case, where I can not do a to upper or to lower, cause maybe I receiving a string value as an additional param and I need the value just equals ? – Ivan Fontalvo Jul 15 '22 at 20:48
  • Use `((bool)x.Value).ToString().ToLower()` – Paul Sinnema Jul 15 '22 at 21:04
  • Did not read your comment well enough. This only works when the type is a `bool` of course. – Paul Sinnema Jul 15 '22 at 21:11
  • Is ok, I know now how to do it, but validating the type before. Thanks!! – Ivan Fontalvo Jul 15 '22 at 21:13
  • var queryParameters = new Dictionary() { { "latestVersion", true }, { "SomeInteger", 20 } }; var t = queryParameters.Select(x => x.Key + "=" + ConvertValue(x.Value)).ToList(); t.ForEach(s => Console.WriteLine(s)); Console.ReadKey(); string ConvertValue(object value) { if(value is Boolean) { return ((bool)value).ToString().ToLower(); } return $"{value}"; } – Paul Sinnema Jul 15 '22 at 21:17
  • The question is closed so I cannot give an answer anymore. Sorry for the messy response. – Paul Sinnema Jul 15 '22 at 21:18
  • @Moderator The problem here was not why the value "True", "False" was returned but how to solve this and get lower case. I think the question was closed too soon. – Paul Sinnema Jul 15 '22 at 21:21
  • @PaulSinnema The question literally was, *"Why is changing the value from true to True"*. The duplicate answers that. And provides multiple workarounds, both in the answers and in the comments. Your comments under this question provide an abridge version of the information contained in the duplicate. – GSerg Jul 15 '22 at 21:38
  • @GSerg Yeah, right. If you just interpret the title you are right. The question was answered already. But in the discussion some other issue that needed solving cropped up. In the background I was coding an example but was disappointed in the fact that I could not post it anymore. It would have helped the OP. – Paul Sinnema Jul 15 '22 at 21:52

0 Answers0