Uri.EscapeUriString with square braces
.net-4.5, c#, escaping, linqpad, uri
Solution
This changed between .Net 4 and .Net 4.5, which you can verify by retargeting the framework version to .Net 4 and running the test program.
.Net 4 -> outputs "%5B" .net 4.5 (or later) -> outputs "["
This is mentioned here: Application Compatibility in the .NET Framework 4.5
under the section for Uri.EscapeDataString , Uri.EscapeUriString, and Uri.UnescapeDataString, which states that (with .Net 4.5):
The list of reserved and unreserved characters now supports RFC 3986.
Specific changes:
Unreserved escaped characters are un-escaped.
EscapeDataString escapes reserved characters based on RFC 3986.
EscapeUriString does not escape reserved characters.
UnescapeDataString does not throw an exception if it encounters an invalid escape sequence.
In particular, it is the EscapeUriString does not escape reserved characters which is significant.
Problem
Something of a strange question but let's see what kind of response it gets... If I code a console app (VS 2013, .NET 4.5.1) and execute this line of code: `Uri.EscapeUriString("[")` I get this: `[` However if I execute the same thing (well, technically `Uri.EscapeUriString("[").Dump()`) in LINQPad on my machine I get this: `%5B` To further complicate things, according to this post `Uri.EscapeUriString("[")` should indeed return `%5B`.The post was written on 27/06/2012. I'm thinking that perhaps LINQPad is referencing an older DLL than that used by VS, but that would imply that `EscapeUriString` has changed relatively recently, which I cannot find any record of. Does anyone have any ideas as to what could be causing this behaviour?