English 中文(简体)
• 如何确定在视觉C#中阵列中的价值
原标题:How to find if a value is in an array in Visual C#

如果我有一阵,,我想迅速检查一下,这些阵列中是否有一定价值,是否有这样做的方法?

最佳回答

如果对阵列进行分类,那么最快的是:

Array.BinarySearch(myArray, value) >= 0;

如果对阵列进行了搜查,很少加以修改,那么你可能认为,在修改后(使用<代码>Array.Sort<>/code>)对阵列进行分类并使用上述编码是值得的。 否则,采用你选择的办法:

Array.IndexOf(myArray, value) >= 0; //.Net 1

Array.Exists(array, delegate(int x) { return x == value; }); //.Net 2

myArray.Contains(value); //.Net 3

< 编码>IndexOf对未安装阵列的最好性能。 第二种选择是使用权势代表,第三项要求设定标语。

问题回答

例如,如果你想要检查一下贵国的阵列是否包含第0号:

if (your_int_array.Contains(0))
{
    //Code
}

千分之四 方法就是您的朋友。

您可使用IndexOf。 方法:

int[] array = new int[] { 1, 2, 3 };
bool isArrayContains17 = Array.IndexOf(array, 17) > -1;
var myArray = new []  { 1, 2};
if myArray.Contains(1)
{
do something
}

页: 1

祝福

丹麦

履行这一职能:

public static bool FindValueFromArray(object[] Values,object valueToSearch){
    bool retVal = false;
    Array myArray = (Array)Values;
    int found = Array.BinarySearch(myArray, valueToSearch);
    if (found != -1){
        retVal = true;
    }
    return retVal;
}

希望这一帮助。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签