如何在c#中将int数据类型转换为字符串数据类型?
当前回答
没有一个回答提到ToString()方法可以应用于整数表达式
Debug.Assert((1000*1000).ToString()=="1000000");
甚至是整型字面量
Debug.Assert(256.ToString("X")=="100");
虽然像这样的整数字面值通常被认为是糟糕的编码风格(魔术数字),但在某些情况下,这个特性是有用的…
其他回答
string myString = myInt.ToString();
如果你从数据集中
string newbranchcode = (Convert.ToInt32(ds.Tables[0].Rows[0]["MAX(BRANCH_CODE)"]) ).ToString();
int num = 10;
string str = Convert.ToString(num);
string str = intVar.ToString();
在某些情况下,您不必使用ToString()
string str = "hi " + intVar;
继续讨论@Xavier的回答,这里有一个页面,可以快速比较几种不同的方法,从100次迭代到21,474,836次迭代。
这似乎是一个平局:
int someInt = 0;
someInt.ToString(); //this was fastest half the time
//and
Convert.ToString(someInt); //this was the fastest the other half the time
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- Printf与std::字符串?
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 不区分大小写的“in”
- 自动化invokerrequired代码模式
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- 如何在PHP中截断字符串最接近于一定数量的字符?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 如何舍入一个双到最近的Int在迅速?