locked
Why does this C# code returns different compared with VB codes? RRS feed

  • Question

  • User-1651604128 posted

    Hi

    I my mvc web app, I have this code in web.config.

    <add key="ProdUser" value="PROD\prod_user" />

    In controller, I tried this C# codes:

    string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();

    It returns: "PROD\\prod_user"; //this is wrong

    But if I tried this VB codes:

    Dim sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString

    it returns: "PROD\prod_user"; //this is correct.

    Can anybody help me why the C# codes returns extra "\" which is wrong? and how to fix it?

    Thanks

    Wednesday, October 9, 2019 1:58 PM

Answers

  • User-474980206 posted

    There is no extra backslash. If you printed the value to the console you’d see that. You are probably use the debugger, which will display it as a string literal. In c# string (like c and JavaScript) backslashes must be quoted as backslash is the quote character.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 2:04 PM
  • User753101303 posted

    Hi,

    It seems a confusion between the actual value and how it is shown in the debugger. All those lines:

    sUser = "PROD\\prod_user"; // C#
    sUser=@"PROD\prod_user"; // C# using the @ "verbatim string" symbol
    sUser="PROD\prod_user" ' VB.NET

    are giving the SAME value to the sUser variable. It just happens that \ needs to be escaped as it usualy introduces a notation for special characters that you don't have in VB. In C# you can also use the @ prefix which can be convenient if your string contains a lot of \ characters.

    The debugger shows as well the value as written in the language you are using (rather than the "actual" value). So for now it seems you are looking at a "a\b" string which is shown as "a\\b" in the debugger as this is the syntax you would use in C# to get a string having the "a\b" value.

    If confirmed you may want to explain what is the exact problem you have but it doesn'ts seems caused by a double slash issue (that is likely not really part of your string).

    edit: in short

            static void Main(string[] args)
            {
                var a = "a\\b";
                var b = @"a\b";
                Console.WriteLine(a); // shows a\b
                Console.WriteLine(b); // shows a\b
            }

    shows both a\b  on the console but the debugger shows "a\\b" for both string as this is just the default C# syntax used to create a literal string having the a\b value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 3:15 PM
  • User475983607 posted

    I cannot reproduce your results.

    [HttpGet]
    public ActionResult Index()
    {
        string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();
        return Content(sUser);
    }
    <add key="ProdUser" value ="PROD\prod_user"/>

    My best guess is you have issues elsewhere in the code base or your test is flawed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 3:15 PM
  • User665608656 posted

    Hi Peter,

    As other respondents have answered, although you see a string with two backslashes when debugging, in fact it has only one backslash.

    The first slash is used to escape, which is the rule in C#.

    I don't know what you need to get the string and use it to do. I tested it with a label control. When I got the string, I assigned its value to label.

    In fact, the content label displayed on the page is exactly what you want.

    Here is the result :

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 5:32 AM
  • User-1651604128 posted

    Hi Peter,

    As other respondents have answered, although you see a string with two backslashes when debugging, in fact it has only one backslash.

    The first slash is used to escape, which is the rule in C#.

    I don't know what you need to get the string and use it to do. I tested it with a label control. When I got the string, I assigned its value to label.

    In fact, the content label displayed on the page is exactly what you want.

    Here is the result :

    Best Regards,

    YongQing.

    Hi YongQing,

    Thanks a lot, yes, your are right, I tested with ViewBag and I got the same result as yours, it is single backslash in viewbag as well.

    It is confusing the VB code returns correct result in Debug mode, and C# is not (with double backslash), so we have to use label or ViewBag to verify it in C#.

    Have a good day

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 12:48 PM

All replies

  • User-474980206 posted

    There is no extra backslash. If you printed the value to the console you’d see that. You are probably use the debugger, which will display it as a string literal. In c# string (like c and JavaScript) backslashes must be quoted as backslash is the quote character.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 2:04 PM
  • User-1651604128 posted

    There is no extra backslash. If you printed the value to the console you’d see that. You are probably use the debugger, which will display it as a string literal. In c# string (like c and JavaScript) backslashes must be quoted as backslash is the quote character.

    Yes, I checked it in debug mode to see the difference, I suspect this is the real return value which caused the VB codes work well, and C# code does not work because this user id with extra "\"

    Wednesday, October 9, 2019 2:23 PM
  • User-1651604128 posted

    bruce (sqlwork.com)

    There is no extra backslash. If you printed the value to the console you’d see that. You are probably use the debugger, which will display it as a string literal. In c# string (like c and JavaScript) backslashes must be quoted as backslash is the quote character.

    I have just tested using the Console to print the string variable, here is my codes,

    string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();

    Console.WriteLine("sUser is: " + sUser); // here I got: "sUser is: PROD\\prod_user", which is as the same result as the value in my debug codes,

    I am not sure what Console print statement you used, but from what I tried in this Console print, the C# codes returns different result compared with the VB codes.

    I know the backslash is the special character, we need to treat it differently, but in this case, VB codes works well, but C# codes is not.

    I also tried to hardcoded to assign the: sUser = "PROD\prod_user", this is not compile, I need to add extra backslash : sUser = "PROD\\prod_user", but this is not what I wanted.  

    I also tried this: sUser = "PROD" + @"\prod_user", this does not work as well 

    Any idea?

    Wednesday, October 9, 2019 2:54 PM
  • User753101303 posted

    Hi,

    It seems a confusion between the actual value and how it is shown in the debugger. All those lines:

    sUser = "PROD\\prod_user"; // C#
    sUser=@"PROD\prod_user"; // C# using the @ "verbatim string" symbol
    sUser="PROD\prod_user" ' VB.NET

    are giving the SAME value to the sUser variable. It just happens that \ needs to be escaped as it usualy introduces a notation for special characters that you don't have in VB. In C# you can also use the @ prefix which can be convenient if your string contains a lot of \ characters.

    The debugger shows as well the value as written in the language you are using (rather than the "actual" value). So for now it seems you are looking at a "a\b" string which is shown as "a\\b" in the debugger as this is the syntax you would use in C# to get a string having the "a\b" value.

    If confirmed you may want to explain what is the exact problem you have but it doesn'ts seems caused by a double slash issue (that is likely not really part of your string).

    edit: in short

            static void Main(string[] args)
            {
                var a = "a\\b";
                var b = @"a\b";
                Console.WriteLine(a); // shows a\b
                Console.WriteLine(b); // shows a\b
            }

    shows both a\b  on the console but the debugger shows "a\\b" for both string as this is just the default C# syntax used to create a literal string having the a\b value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 3:15 PM
  • User475983607 posted

    I cannot reproduce your results.

    [HttpGet]
    public ActionResult Index()
    {
        string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();
        return Content(sUser);
    }
    <add key="ProdUser" value ="PROD\prod_user"/>

    My best guess is you have issues elsewhere in the code base or your test is flawed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 3:15 PM
  • User665608656 posted

    Hi Peter,

    As other respondents have answered, although you see a string with two backslashes when debugging, in fact it has only one backslash.

    The first slash is used to escape, which is the rule in C#.

    I don't know what you need to get the string and use it to do. I tested it with a label control. When I got the string, I assigned its value to label.

    In fact, the content label displayed on the page is exactly what you want.

    Here is the result :

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 5:32 AM
  • User-1651604128 posted

    Hi Peter,

    As other respondents have answered, although you see a string with two backslashes when debugging, in fact it has only one backslash.

    The first slash is used to escape, which is the rule in C#.

    I don't know what you need to get the string and use it to do. I tested it with a label control. When I got the string, I assigned its value to label.

    In fact, the content label displayed on the page is exactly what you want.

    Here is the result :

    Best Regards,

    YongQing.

    Hi YongQing,

    Thanks a lot, yes, your are right, I tested with ViewBag and I got the same result as yours, it is single backslash in viewbag as well.

    It is confusing the VB code returns correct result in Debug mode, and C# is not (with double backslash), so we have to use label or ViewBag to verify it in C#.

    Have a good day

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 12:48 PM