无法从int转换为system.collections.generic.icomparer <int> </int>

时间:2013-03-12 16:12:20

标签: c# list compilation foreach int

C#的新手只是想知道错误是什么,无法理解错误。无需纠正,只想知道错误。 错误: 最佳重载方法匹配&#39; Systems.Collections.Generic.List.BinarySearch(int,System.Collections.Generic.Icomparer)&#39;有一些无效的论点 Argument1:无法从system.collections.generic.list转换为int Argument2:无法从int转换为system.collections.generic.icomparer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;enter code here
using System.Windows.Forms;

namespace LotoNumbers
{
    public partial class FrmLotto : Form
{
    // declare class level variables
    List<int> player = new List<int>(7);
    List<int> computer = new List<int>(6);

    public FrmLotto()
    {
        InitializeComponent();
    }

    // event method to generate player's numbers
    private void btnPlayer_Click(object sender, EventArgs e)
    {
        // declare and initalize local variables
        string display = "";

        // reset the winning number label before generating the player's numbers
        lblComputerNumber.Text = "00  00  00  00  00  00";

        // generate unique random numbers
        GenerateRandomNumbers(player);
        player.Sort();
        // build display string
        display = BuildDisplayString(player);

        // display winning number in label
        lblPlayerNumber.Text = display;
    }

    // method to generate computer's random numbers (the 'winning numbers')
    // and determine how many winning numbers
    private void btnComputer_Click(object sender, EventArgs e)
    {
        // declare and initalize local variables
        int winCount = 0;
        string display = "";

        // generate unique random numbers
        GenerateRandomNumbers(computer);

        // sort the array in ascending order
        computer.Sort();

        // build display string
        display = BuildDisplayString(computer);

        // display winning number in label
        lblComputerNumber.Text = display;

        // determine if this number matches any of the players numbers
        winCount = CompareTwoList(player, computer);

        // display the total winning numbers
        lblMatching.Text = winCount.ToString("D2");
    }

    private void GenerateRandomNumbers(List<int> numberList)
    {


        Random lucky = new Random();

        // generate unique random numbers
        for (int index = 0; index < numberList.Capacity; index++)
        {
            int temp = lucky.Next(1, 50);
            if (numberList.IndexOf(temp) < 0)
            {
                numberList.Add(temp);
            }
            else
            {
                index--;
            }
        }

    }

    private string BuildDisplayString(List<int> numberList)
    {
        // declare method variable
        string display = "  ";

        // loop through the array and build a display string
        foreach (int number in numberList)
            display += number.ToString("D2") + "  ";

        // return display string
        return display;
    }

    private int CompareTwoList(List<int> list1, List<int> list2)
    {
        // declare method variable
        int numberMatching = 0;

        // loop through each element in the first array looking for a match in the second array
        foreach (int value in list1)
        {
            if (player.BinarySearch(list2, value) >= 0)
                numberMatching++;  // a matching value is found
        }

        return numberMatching;
    }
}

}

2 个答案:

答案 0 :(得分:2)

看起来你想要:

if (list2.BinarySearch(value) >= 0)
    numberMatching++;  // a matching value is found

您此刻正在呼叫List<int>.BinarySearch(int, IComparer<int>)。由于valueint,因此您会收到类型错误。

请注意,您也可以这样做:

int numberMatching = list2.Intersect(list1).Count();

答案 1 :(得分:0)

List<int>.BinarySearch()接受List<int>int作为参数,没有任何重载。 See MSDN

您需要使用player.BinarySearch(value)