Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
C#
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(1+'1');
            Console.ReadLine();
             
        }
    }
}


Above the programm result 50.at debug mode i had findout '1' take as 49 and final result 50 and also if u take '2' result is 51.is it any order following.why it represent.
please help me.
thank u.

What I have tried:

Above the programm result 50.at debug mode i had findout '1' take as 49 and final result 50 and also if u take '2' result is 51.is it any order following.why it represent.
Posted
Updated 24-Jun-18 5:52am
Comments
[no name] 24-Jun-18 11:41am    
'1' is a char which has the ordinal hex 0x31 or decimal 49. Therefore 49+1= 50 is very ok. For more information google for "ascii table" and read something like this:
https://www.asciitable.com/[^]

The only problem here is your understanding. You're adding the integer value 1 to a CHARACTER, '1', which is has the ASCII code of 49 (decimal). So, 1 + 49 = 50.
 
Share this answer
 
The numeric value of the character '1' is 49.  For a list of common character ASCII values, see this link: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^].  The expression you're printing causes the integer value (49) of the character '1' to be added to the integer literal 1, hence the output of 50.

/ravi
 
Share this answer
 
The key here is the order of the values in your expression. When the first value is a number or single character then the expression is converted to two numbers. If the first value is a string then the second value will be converted to a string and the two concatenated.
C#
Console.WriteLine(1 + '1'); // adds 1 + 49
Console.WriteLine('1' + 1); // adds 49 + 1
Console.WriteLine("1" + 1); // concatenates "1" and "1"

Output will be:
50
50
11
 
Share this answer
 
v2
Comments
Richard Deeming 26-Jun-18 13:19pm    
The order of the values is irrelevant; it's the type of the values that matters. :)
Console.WriteLine(1 + "1"); // Also concatenates "1" and "1"
Richard MacCutchan 26-Jun-18 14:35pm    
Thanks, my bad for not checking that last case, not getting the obvious from my first two..
See here: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^] It shows you the values of each ASCII code.
Because you are using C#, it's actually a Unicode character rather than ASCII, but for the characters you are interested in, ASCII is fine (Unicode is a superset of ASCII for the most part, but since it's 16 bits wide rather than 7 or 8, it's a lot easier to read the set map!). Unicode® character table[^]

If you want to increment to the next character, then you can. Try this:
char c = '1';
for (int i = 0; i < 30; i++)
    {
    Console.WriteLine("{0}:{1}", i + c, (char) (i + c));
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900