【问题标题】:Clean way to concatenate a char and an int连接 char 和 int 的干净方法
【发布时间】:2018-10-25 15:16:56
【问题描述】:

我有一个带有两个ints 的坐标类,它有一种方法可以将坐标显示为“A1”或“C5”。

对于我使用字符的字母,我不知道连接两个变量的最佳方法是什么。

方法的返回是这样的:

//letter is a char
//line is an int
return letter+""+line;

我正在使用"",因为它隐式地将 char 和 int 转换为字符串。如果letter 是一个字符串,我可以只使用return letter+line;,但使用char,则隐式转换不起作用。

我对这种方式并不满意,不是吗?什么是更清洁的方法?

【问题讨论】:

    标签: c# types type-conversion


    【解决方案1】:

    试试这个

    return $"{letter}{line}";
    

    【讨论】:

    【解决方案2】:

    我想这是最好的方法:

    return String.Concat(letter, line);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多