如何在c#中将int数据类型转换为字符串数据类型?


当前回答

继续讨论@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

其他回答

or:

string s = Convert.ToString(num);
string str = intVar.ToString();

在某些情况下,您不必使用ToString()

string str = "hi " + intVar;

如果你从数据集中

string newbranchcode = (Convert.ToInt32(ds.Tables[0].Rows[0]["MAX(BRANCH_CODE)"]) ).ToString();

继续讨论@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
using System.ComponentModel;

TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string s = (string)converter.ConvertTo(i, typeof(string));