C# 类型转换,IndexOf

huangapple go评论96阅读模式
英文:

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不能为您从intshort进行隐式转换。有几种Array.IndexOf实现:

对于Array.IndexOf(Array, Object)情况,.NET会在对象数组中查找对象装箱int值)。数组包含short值(可以装箱,但仍然是short),而不是int,因此.NET无法找到int

可能的更正:

  1. 使geburtsjahr具有int值:
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 }; 
  1. 搜索short
int index = Array.IndexOf(geburtsjahr, (short)1966);
  1. 让.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:

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:

  1. Make geburtsjahr have int values:
var geburtsjahr = new int[4] { 1998, 1992, 1966, 2006 }; 
  1. Search for short:
int index = Array.IndexOf(geburtsjahr, (short)1966);
  1. Let .net do the cast for you (1966 will be of type short):
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.

huangapple
  • 本文由 发表于 2023年6月25日 18:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定