首页 > 解决方案 > C#: Char to string

问题描述

Why when I enter an input on each line = "a" "b" "c", I don't get an output "abc" but "294"? I get it that they get the number of each letter from the ascii table, but can someone explain me what to do to get "abc"?

using System;

namespace CharsToString
{
    public class Program
    {
        public static void Main()
        {
            char firstInput = char.Parse(Console.ReadLine());
            char secondInput = char.Parse(Console.ReadLine());
            char thirdInput = char.Parse(Console.ReadLine());

            Console.WriteLine(firstInput + secondInput + thirdInput);
        }
    }
}

标签: c#

解决方案


char is a integral type, you are adding values you need to call ToString on each

Console.WriteLine(firstInput.ToString() + secondInput.ToString() + thirdInput.ToString());

Alternatively you could use a string builder, but given the example it seems like a premature optimization


推荐阅读