none
add special char in comment RRS feed

  • Question

  • I want to add ~ in my comment , for example in c# var d = [~ myname] here  I am getting out put as [217 myname] or [Umyname]

    how to convert get ~ in my output, I have tried almost all Unicode nos still not getting the desired output can you please share one quick example for ~ only

    Friday, April 26, 2019 4:45 PM

Answers

  • Please use the Insert Code Block feature in the editor to post code so it is formatted. The code you said you typed isn't valid C# so it wouldn't have compiled.

    The problem you're having converting from PowerShell to C# is that PowerShell allows both single and double quotes for strings. Double quotes are used to allow string interpolation. But in C# single quotes are for single characters only (a char) whereas double quotes are for strings. Anytime you have more than a single character you have to use a string otherwise it won't compile.

    static void Main ( string[] args )
    {
        var a = '~';
        var b = '[';
        var c = ']';
    
        //ERROR: Character literals are single characters
        //var com = b + a + 'myname' + c;
    
        //VALID: String literals can have multiple characters.
        var com = b + a + "myname" + c;
    
        Console.WriteLine(com);
    }

    Now on to what the actual issue is. C#, like most languages, allows type coercion to allow you to mix types without them all being the same type. We do it all the time `3 + 4.5 = double`. This works with all types, not just the numerics. So let's see what the compiler sees here.

    var com = char + char + string + char
    var com = int + string + char
    var com = string + char
    var com = string

    The first 2 characters are combined with +. Because of the old C/C++ rules, chars can be treated as integral values and since you are using the + that is what happens here. So `b + a` is actually of type int. Now you have an int + string. Ints can be converted to strings without losing conversion so you `int + string = string`. We just took the (numeric) value of the first expression and prefixed it to the string. Finally we get to `string + char` which produces a string because, again, char can be upcast to a string. The result is the value you're seeing in the output.

    To fix this issue you need to ensure that the (char + char) never occurs so changing the type of one of the expressions is sufficient.

    var useACast = b.ToString() + a + "myname" + c;
    
    //Or change the types of a, b, c to string when you declare them
    
    //Or use String.Concat instead
    var useConcat = String.Concat(b, a, "myname", c);

    This isn't really specific to C# as almost all languages work this way. The only thing that makes C# a little different here is that it will apply coercion to strings which most other languages do not (because strings are not typically concat-able like .NET allows).


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 30, 2019 4:01 PM
    Moderator

All replies

  • The question is not sufficiently clear. The character ~ can be part of a comment without doing anything special. You just type it in and it becomes part of a comment. But then you talk about your "output" which is confusing, since a comment will not produce any output. It disappears when you compile the code. So you must be using it somewhere else and not in a comment. We need more information about where and how you are using the ~ to understand why it is not doing what you think that it should do. Please show a complete fragment of C# that demonstrates what you are trying to accomplish, so that we can see where the problem is.
    Friday, April 26, 2019 7:55 PM
    Moderator
  • Are you using an international keyboard?  Character 217 is capital U with grave accent.  I don't know why you'd get that unless your keyboard mapping doesn't match what your keyboard really is.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, April 27, 2019 12:57 AM
  • I have international keyboard but here keyboard is not question

    var a= '~'; var b ='['; var c=']'

    I am doing var com = b + a + 'myname' + c so  I expect [~myname] in output but I am getting 217myname] like this in output,

    if in powershell I have declare

    $a='['; $b=']';$c='~'

    $out=$a+$c+"name"+$b

    echo $out

    it print correct output, why in c# its so difficult

    I have tried

    var a= char.getnumericvalue('~'); then char.tryparse(a, out value) still this value is not convert into ~ as expected?

    Tuesday, April 30, 2019 10:39 AM
  • Please use the Insert Code Block feature in the editor to post code so it is formatted. The code you said you typed isn't valid C# so it wouldn't have compiled.

    The problem you're having converting from PowerShell to C# is that PowerShell allows both single and double quotes for strings. Double quotes are used to allow string interpolation. But in C# single quotes are for single characters only (a char) whereas double quotes are for strings. Anytime you have more than a single character you have to use a string otherwise it won't compile.

    static void Main ( string[] args )
    {
        var a = '~';
        var b = '[';
        var c = ']';
    
        //ERROR: Character literals are single characters
        //var com = b + a + 'myname' + c;
    
        //VALID: String literals can have multiple characters.
        var com = b + a + "myname" + c;
    
        Console.WriteLine(com);
    }

    Now on to what the actual issue is. C#, like most languages, allows type coercion to allow you to mix types without them all being the same type. We do it all the time `3 + 4.5 = double`. This works with all types, not just the numerics. So let's see what the compiler sees here.

    var com = char + char + string + char
    var com = int + string + char
    var com = string + char
    var com = string

    The first 2 characters are combined with +. Because of the old C/C++ rules, chars can be treated as integral values and since you are using the + that is what happens here. So `b + a` is actually of type int. Now you have an int + string. Ints can be converted to strings without losing conversion so you `int + string = string`. We just took the (numeric) value of the first expression and prefixed it to the string. Finally we get to `string + char` which produces a string because, again, char can be upcast to a string. The result is the value you're seeing in the output.

    To fix this issue you need to ensure that the (char + char) never occurs so changing the type of one of the expressions is sufficient.

    var useACast = b.ToString() + a + "myname" + c;
    
    //Or change the types of a, b, c to string when you declare them
    
    //Or use String.Concat instead
    var useConcat = String.Concat(b, a, "myname", c);

    This isn't really specific to C# as almost all languages work this way. The only thing that makes C# a little different here is that it will apply coercion to strings which most other languages do not (because strings are not typically concat-able like .NET allows).


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 30, 2019 4:01 PM
    Moderator
  • var a= '~'; var b ='['; var c=']'

    I am doing var com = b + a + 'myname' + c so  I expect [~myname] in output but I am getting 217myname] like this in output,

    Be careful: You are using single quotes for your strings, which in C# are used to represent the char datatype. To represent strings, you need double quotes. When you use the + sign between char values, it perform addition of their numerical values. To get concatenation instead of addition, you need to use strings and not chars.

    Your Powershell example is different because in Powershell both the single and the double quotes represent strings. The same occurs in javascript. But in C# you need the double quotes. It will not do what you expect if you use single quotes. To prevent some of these errors, I recommend declaring the variables with their correct type (i.e., string) instead of being lazy and using var.


    EDITED: Sorry, CoolDadTx, I said some things that were already in your answer, but at the time I clicked "Reply" your post was not yet visible.
    Tuesday, April 30, 2019 4:06 PM
    Moderator
  • please close the thread

    I have added "[~"+"myname.tostring()"+"]" now its not converted into any special char and except this I am have added any other code, its strange now its working fine as expected


    Friday, May 3, 2019 3:50 PM
  • please close the thread

    I have now marked the thread as "answered", but please note that you can do this yourself. You don't need to wait for a moderator to do it for you.
    Friday, May 3, 2019 5:58 PM
    Moderator