Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在Silverlight上找不到列表项的索引_C#_Silverlight_Indexof - Fatal编程技术网

C# Can';在Silverlight上找不到列表项的索引

C# Can';在Silverlight上找不到列表项的索引,c#,silverlight,indexof,C#,Silverlight,Indexof,我有一个Country类型的项目列表,我试图在列表中查找和特定国家的索引,但是IndexOf()方法总是返回-1 Country对象如下所示: public class Country { public string CountryCode { get; set; } public string CountryName { get; set; } } public bool Equals(Country other) { retu

我有一个Country类型的项目列表,我试图在列表中查找和特定国家的索引,但是IndexOf()方法总是返回-1

Country对象如下所示:

    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }
public bool Equals(Country other)
{
    return this.CountryName.Equals(other.CountryName);
}
然后,当我尝试使用IndexOf()方法时,我会执行以下操作:

var newcountry = new Country
                     {
                         CountryCode = "VE",
                         CountryName = "VENEZUELA"
                     };
        var countries = ListBoxCountries.Items.Cast<Country>().ToList();

        if (countries.IndexOf(newcountry) == -1)
            countries.Add(newcountry);

还有另一个问题:仅仅为了比较两个对象就可以这么做吗?

这是因为IndexOf使用引用等式来比较对象

你可以用这个

var newcountry = new Country
                 {
                     CountryCode = "VE",
                     CountryName = "VENEZUELA"
                 };


bool country = ListBoxCountries.Items.Cast<Country>().FirstOrDefault(c=>c.CountryCode == newcountry.CountryCode && c.CountryName == newcountry.CountryName)

if(country == null)
  countries.Add(newcountry);
var newcountry=新国家
{
CountryCode=“VE”,
CountryName=“委内瑞拉”
};
bool country=ListBoxCountries.Items.Cast().FirstOrDefault(c=>c.CountryCode==newcountry.CountryCode&&c.CountryName==newcountry.CountryName)
如果(国家==null)
国家。添加(新国家);

或者您可以更好地使用ovverride Equals()方法来比较对象。

这是因为IndexOf使用引用相等来比较对象

你可以用这个

var newcountry = new Country
                 {
                     CountryCode = "VE",
                     CountryName = "VENEZUELA"
                 };


bool country = ListBoxCountries.Items.Cast<Country>().FirstOrDefault(c=>c.CountryCode == newcountry.CountryCode && c.CountryName == newcountry.CountryName)

if(country == null)
  countries.Add(newcountry);
var newcountry=新国家
{
CountryCode=“VE”,
CountryName=“委内瑞拉”
};
bool country=ListBoxCountries.Items.Cast().FirstOrDefault(c=>c.CountryCode==newcountry.CountryCode&&c.CountryName==newcountry.CountryName)
如果(国家==null)
国家。添加(新国家);

或者您可以更好地使用ovverride Equals()方法来比较对象。

我怀疑这是由于引用问题造成的。您需要重写
Equals()国家/地区
类中的code>方法

我会使用如下代码:

    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }
public bool Equals(Country other)
{
    return this.CountryName.Equals(other.CountryName);
}

我怀疑这是由于一个参考问题。您需要重写
Equals()国家/地区
类中的code>方法

我会使用如下代码:

    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }
public bool Equals(Country other)
{
    return this.CountryName.Equals(other.CountryName);
}

是的,IndexOf使用Equals()来比较对象。因此,如果您想使用IndexOf,您必须在Country类中重写此方法。您可能还需要实现
GetHashCode()
,但我不明白为什么
Equals()
不够。是的,IndexOf使用Equals()来比较对象。因此,如果您想使用IndexOf,您必须在Country类中重写此方法。您可能还需要实现
GetHashCode()
,但我不明白为什么
Equals()
不够。