C# 将 long 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11610224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# convert a long to string
提问by Code Beast
Here my problem is:
这里我的问题是:
I have this code:
我有这个代码:
static long CountLinesInFile(string f)
{
long count = 0;
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
count++;
}
}
return count;
}
Which counts the lines of a text file. The problem I have is that when I'm trying this:
它计算文本文件的行数。我遇到的问题是,当我尝试这样做时:
textBox1.Text = CountLinesInFile("test.txt");
I'm getting an error:
我收到一个错误:
Error 1 Cannot implicitly convert type 'long' to 'string'
It seems legit, but how am I supposed to convert it to string? In Java its a simple toString()
看起来合法,但我应该如何将其转换为字符串?在 Java 中它是一个简单的toString()
Can someone give me a solution?
有人可以给我一个解决方案吗?
采纳答案by Blachshma
Use the ToString()method like this:
使用这样的ToString()方法:
textBox1.Text = CountLinesInFile("test.txt").ToString();
回答by Pranay Rana
just write
写就好了
textBox1.Text =(CountLinesInFile("test.txt")).ToString();
MSDN: Object.ToString Method- Returns a string that represents the current object.
MSDN:Object.ToString 方法- 返回表示当前对象的字符串。
回答by Jamiec
In Java its a simply .ToString
在 Java 中它是一个简单的 .ToString
And in C#, its simply .ToString().
而在 C# 中,它只是.ToString().
Happy learning.
快乐学习。
回答by JohnnBlade
try this textBox1.Text = CountLinesInFile("test.txt").ToString();
尝试这个 textBox1.Text = CountLinesInFile("test.txt").ToString();
回答by CelzioBR
champs.
冠军。
I did this: "Cast the dynamic value to long and convert to string"
我这样做了:“将动态值转换为 long 并转换为字符串”
((long)x.PersonId).ToString();

