首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#逗号和问号有关问题

2012-09-24 
c#逗号和问号问题、using Systemusing System.Collections.Genericusing System.Linqusing System.Text

c#逗号和问号问题、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
  class Program
  {
  static void Main(string[] args)
  {
  int code = (int)'a';
  Console.WriteLine("a->{0}" , code);
  }
  }
}
Console.WriteLine("a->{0}" , code);这一句,用逗号输出//a->97
Console.WriteLine("a->{0}" +code);用加号输出//a->{0}97
这2个效果为什么不一样,求老师解释下 谢谢!


[解决办法]
Console.WriteLine("a->{0}" , code);
 一句中{0}表示code的值
Console.WriteLine("a->{0}" +code);
 一句中{0}则是字符串,("a->{0}" +code表示是字符串("a->{0}" 与字符串code(+号转化为了字符串)合并
[解决办法]
因为WriteLine有多个重载,对应不同个数的参数。{0}在string.Format中是有含义的,表示后面的第一个参数,以此类推还有{1},{2}...。

你的代码中第一个对应原型为WriteLing(format, arg0),所以code转换为字符串替换掉了{0};第二个对应原型为WriteLing(str),就原样输出了。

热点排行