英文:
C# casting, Indexof
问题
I'm new to programming (for 5 days). I wrote this code but index is always -1
(when 2
is expected) and I couldn't fix it.
我的老师给了我这个建议:
Casting, Array.IndexOf(geburtsjahr, (int)1966);
我尝试过了,但仍然一样。
这是我的相关代码:
var geburtsjahr = new short[4] { 1998, 1992, 1966, 2006 };
// 预期结果:2,实际结果:-1
int index = Array.IndexOf(geburtsjahr, (int)1966);
Console.WriteLine(index);
我试图查看正确的“index”数字(应该是2)。
英文:
I'm new to programming (for 5 days). I wrote this code but index is always -1
(when 2
is expected) and I couldn't fix it.
My teacher wrote me just that:
Casting, Array.IndexOf(geburtsjahr, (int)1966);
I tried but still same.
This is my relevant code:
var geburtsjahr = new short[4] { 1998, 1992, 1966, 2006 };
// Expected: 2, Actual: -1
int index = Array.IndexOf(geburtsjahr, (int)1966);
Console.WriteLine(index);
I tried to see right index
number (should be 2
).
答案1
得分: 2
代码中的主要问题是您有一个short[]
数组和一个要在其中查找的int
值,而.NET不能为您从int
到short
进行隐式转换。有几种Array.IndexOf
实现:
- Array.IndexOf<T>(T[], T): 不会被使用,因为.NET不能将
int
转换为short
或将short[]
转换为int[]
并将其转换为单个泛型T
- Array.IndexOf(Array, Object): 这将被调用
对于Array.IndexOf(Array, Object)
情况,.NET会在对象数组中查找对象(装箱的int
值)。数组包含short
值(可以装箱,但仍然是short
),而不是int
,因此.NET无法找到int
。
可能的更正:
- 使
geburtsjahr
具有int
值:
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 };
- 搜索
short
:
int index = Array.IndexOf(geburtsjahr, (short)1966);
- 让.NET为您执行转换(
1966
将是short
类型):
int index = Array.IndexOf<short>(geburtsjahr, 1966);
英文:
The main problem with your code is that you have short[]
array and int
value to find in it and .net can't do implicit cast from int
to short
for you. There are several overload Array.IndexOf
implementations:
- Array.IndexOf<T>(T[], T): will not be used, since .net can't cast
int
toshort
orshort[]
toint[]
and come to a single genericT
- Array.IndexOf(Array, Object): this will be called
In case of Array.IndexOf(Array, Object)
.net looks for an object (boxed int
value) in an array of objects. The array contains short
values (which can be boxed, but still short
), not int
, so .net can't find int
.
Possible corrections:
- Make
geburtsjahr
haveint
values:
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 };
- Search for
short
:
int index = Array.IndexOf(geburtsjahr, (short)1966);
- Let .net do the cast for you (
1966
will be of typeshort
):
int index = Array.IndexOf<short>(geburtsjahr, 1966);
答案2
得分: 0
这样做是可行的:
(1)
var geburtsjahr = new short[4] { 1998, 1992, 1966, 2006 };
int index = Array.IndexOf(geburtsjahr, (short)1966);
(2)
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 };
int index = Array.IndexOf(geburtsjahr, (int)1966); //或者不使用强制转换,直接写1966
问题在于在 short
类型的数组中没有任何 int
类型的值,无论这些数字对我们人类来说代表什么。
英文:
It works if you do either of these:
(1)
var geburtsjahr = new short[4] { 1998, 1992, 1966, 2006 };
int index = Array.IndexOf(geburtsjahr, (short)1966);
(2)
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 };
int index = Array.IndexOf(geburtsjahr, (int)1966); //or just 1966 without the cast.
The issue is that there is no int
of any value in an array of short
values, no matter what the numbers represent to us humans.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论