开发者

Finding the index in a list of nullable types?

开发者 https://www.devze.com 2022-12-14 10:04 出处:网络
I have a list of nu开发者_开发知识库llable ushorts : List<ushort?> items = new List<ushort?>();

I have a list of nu开发者_开发知识库llable ushorts :

List<ushort?> items = new List<ushort?>();

and I'm trying to get the following to work, but I can't - for some reason.

int GetIndex(ushort value)
{
    return ?
}

what I'm trying is :

ushort? x = value;
int idx = items.FindIndex(x);

But I'm getting :

"Best overloaded method has some invalid arguments" error

Any ideas?


You should call IndexOf.

The FindIndex method is a more advanced method that takes a delegate and finds the index of the first item that matches the delegate (it calls the delegate on every item and returns the index of the first item for which the delegate returned true)

Unelss it contains other logic, your entire function can be replaced by a call to IndexOf

0

精彩评论

暂无评论...
验证码 换一张
取 消