C# 无法隐式转换“;“字符串”;至;int";从文件中读取整数时
我是新来C#的,需要一些帮助。如果有人能解释为什么会发生这种情况,而不仅仅是给我一个答案,那将不胜感激 我收到以下错误消息: 无法将字符串隐式转换为int 这是我的密码:C# 无法隐式转换“;“字符串”;至;int";从文件中读取整数时,c#,string,int,implicit-conversion,C#,String,Int,Implicit Conversion,我是新来C#的,需要一些帮助。如果有人能解释为什么会发生这种情况,而不仅仅是给我一个答案,那将不胜感激 我收到以下错误消息: 无法将字符串隐式转换为int 这是我的密码: int[] arrayData = new int[12]; StreamReader scores = new StreamReader("scores.txt") while (scores.Peek() != null) { arrayData[counter] = scores.ReadLine(); /
int[] arrayData = new int[12];
StreamReader scores = new StreamReader("scores.txt")
while (scores.Peek() != null)
{
arrayData[counter] = scores.ReadLine(); // <-- above error occurs here
counter = counter + 1;
}
scores.Close();
int[]数组数据=新的int[12];
StreamReader分数=新的StreamReader(“scores.txt”)
while(scores.Peek()!=null)
{
arrayData[counter]=scores.ReadLine();//y).//不能应用于字符串和int
简短回答:
使用其中一个
int.Parse();
Convert.ToInt32();
方法如下:
int[] arrayData = new int[12];
StreamReader scores = new StreamReader("scores.txt")
while (scores.Peek() != null)
{
arrayData[counter] = int.Parse(scores.ReadLine());
counter = counter + 1;
}
scores.Close();
您需要将字符串值转换为int,以便在比较中分配或使用它,例如
if (x > y) …
详细解释:
错误是不言自明的,您正在读取一个字符串,并试图将其分配给一个完全不同的类型!
该方法返回一个字符串,该字符串与int不同。
在C#
中,您有不同的类型,必须为每个类型使用兼容的值。
int
是数字(实际上数字是一个整数,所以您可以使用int来存储和处理数字(整数值)。
字符串是一个字符数组,例如
"Ali","Alex","Apple",....
在c#中,置于双引号之间的任何内容都被视为字符串。
所以基本上
1不等于“1”
虽然它们看起来一样,但却完全不同。(就好像你把一张苹果的照片和一张真实的照片进行比较!它们看起来一样,但它们是完全不同的两件事!)
因此,如果需要从字符串中提取该数字,则需要进行转换,在这种情况下,如果希望进行转换,则可以使用Convert.ToInt()
方法
您也可以使用:
Int32.Parse();
看一看
还有一个类似的例子
出于同样的原因,不能在不同类型上使用此类运算符
长话短说,
您需要这样做:
int[] arrayData = new int[12];
StreamReader scores = new StreamReader("scores.txt")
while (scores.Peek() != null)
{
arrayData[counter] = int.Parse(scores.ReadLine());
counter = counter + 1;
}
scores.Close();
简短回答:
使用其中一个
int.Parse();
Convert.ToInt32();
方法如下:
int[] arrayData = new int[12];
StreamReader scores = new StreamReader("scores.txt")
while (scores.Peek() != null)
{
arrayData[counter] = int.Parse(scores.ReadLine());
counter = counter + 1;
}
scores.Close();
您需要将字符串值转换为int,以便在比较中分配或使用它,例如
if (x > y) …
详细解释:
错误是不言自明的,您正在读取一个字符串,并试图将其分配给一个完全不同的类型!
该方法返回一个字符串,该字符串与int不同。
在C#
中,您有不同的类型,必须为每个类型使用兼容的值。
int
是数字(实际上数字是一个整数,所以您可以使用int来存储和处理数字(整数值)。
字符串是一个字符数组,例如
"Ali","Alex","Apple",....
在c#中,置于双引号之间的任何内容都被视为字符串。
所以基本上
1不等于“1”
虽然它们看起来一样,但却完全不同。(就好像你把一张苹果的照片和一张真实的照片进行比较!它们看起来一样,但它们是完全不同的两件事!)
因此,如果需要从字符串中提取该数字,则需要进行转换,在这种情况下,如果希望进行转换,则可以使用Convert.ToInt()
方法
您也可以使用:
Int32.Parse();
看一看
还有一个类似的例子
出于同样的原因,不能在不同类型上使用此类运算符
长话短说,
您需要这样做:
int[] arrayData = new int[12];
StreamReader scores = new StreamReader("scores.txt")
while (scores.Peek() != null)
{
arrayData[counter] = int.Parse(scores.ReadLine());
counter = counter + 1;
}
scores.Close();
虽然字符串
可以包含有效的int
数字的文本表示形式(例如“1”
),但它不是int
(1
)。字符串
也可以不是整数的文本表示形式,例如“ABC”
,而像“ABC”>1这样的比较显然没有意义,对吧?这就是为什么没有为
字符串和int
的组合定义
运算符的原因之一:它不能保证在所有情况下都有意义
int x=Console.ReadLine();
不起作用的原因是相同的:该方法返回一个字符串,并且您的变量类型为int
。这些类型不能隐式转换
您需要做的是:使用int.Parse
或int.TryParse
将字符串中整数的文本表示形式转换为int
:
string text = Console.ReadLine(); // get user input; we cannot know whether a number
// was entered or something else!
int a = int.Parse(text); // might throw an exception if `text` doesn't contain
// a textual representation of a valid int number
int b;
if (int.TryParse(text, out b))
{
// contents of `text` could be converted to an `int`, which is now stored in `b`
}
else
{
// contents of `text` could not be converted to an `int`, but no exception
// has been thrown; instead we end up here.
}
虽然字符串
可以包含有效的int
数字的文本表示形式(例如“1”
),但它不是int
(1
)。字符串
也可以不是整数的文本表示形式,例如“ABC”
,而像“ABC”>1这样的比较显然没有意义,对吧?这就是为什么没有为
字符串和int
的组合定义
运算符的原因之一:它不能保证在所有情况下都有意义
int x=Console.ReadLine();
不起作用的原因是相同的:该方法返回一个字符串,并且您的变量类型为int
。这些类型不能隐式转换
您需要做的是:使用int.Parse
或int.TryParse
将字符串中整数的文本表示形式转换为int
:
string text = Console.ReadLine(); // get user input; we cannot know whether a number
// was entered or something else!
int a = int.Parse(text); // might throw an exception if `text` doesn't contain
// a textual representation of a valid int number
int b;
if (int.TryParse(text, out b))
{
// contents of `text` could be converted to an `int`, which is now stored in `b`
}
else
{
// contents of `text` could not be converted to an `int`, but no exception
// has been thrown; instead we end up here.
}
主数组的字符串内容必须转换为目标数组的类型,即哪个整数…因此,它来了:
while (scores.Peek() != null)
{
arrayData[counter] = Convert.ToInt32(score.ReadLine());
counter = counter + 1;
}
主数组的字符串内容必须转换为目标数组的类型,即哪个整数…因此,它来了:
while (scores.Peek() != null)
{
arrayData[counter] = Convert.ToInt32(score.ReadLine());
counter = counter + 1;
}
ReadLine()方法返回一个字符串,您正在将其分配给int[]的一个元素,该元素是int数组。由于它的类型错误,编译器会检查您所使用的类型之间是否存在自动转换