本文介绍了尝试调用int IList< Location> .IndexOf(Location item)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IList泛型我试图弄清楚,我很难找到正确的格式来调用Locations类中的IndexOf方法:

  namespace Ilistprac 
{
class IList2
{
static void Main(string [] args)
{

string sSite =test;

位置test = new Locations();

test.Add(sSite)
}
}

公共类位置
{
公共位置()
{

}

私人字符串_site = string.Empty;
public string Site
{
get {return _site; }
set {_site = value; }
}
}


公共类位置:IList<位置>
{
列表<位置> _locs =新列表< Location>();

public Locations(){}


int IList< Location> .IndexOf(Location item)
{
return _locs.IndexOf (项目);
}

public void Add(string sSite)
{
Location loc = new Location();
loc.Site = sSite;
_locs.Add(loc);




解决方案

你的问题并不完全清楚。我假设你的评论 //没有任何,你的问题是其中之一:




1:

如果这是你的问题,那么你有两个选择。您可以将实现从显式接口成员实现更改为隐式

  public int IndexOf(Location item)
{
return _locs.IndexOf(item);
}

或者,您可以通过<$ c $类型的引用来访问该方法c> IList< Location> 而不是位置参考:

 位置位置= GetLocations(); 
位置位置= GetLocation();
int index =((IList< Location>)locations).IndexOf(location);

第一种方法比较常见,通常较少。






如果这是你的问题,那是因为你没有重写Location类中的 Equals(object)方法,并且你试图找到这样的位置:

  Locations locations = GetLocations(); 
string site = GetSite();
位置位置=新位置{Site = site};
int index =((IList< Location>)locations).IndexOf(location);

在这里,索引总是-1,因为IndexOf正在使用引用等式测试位置对象。如果你想考虑两个位置相等,当且仅当它们的Site属性相等时,你可以覆盖Equals。



如果该等式关系对于位置不是普遍有效的键入,但是,你不应该这样做。在这种情况下,您可以使用linq来查找其网站与所需值匹配的位置:

 位置位置= locations.Where x => x.Site == site).FirstOrDefault; 

如果您需要位置索引,只需执行此操作(假设位置不能合法地保留空值):

 位置位置= locations.Where x => x.Site == site).FirstOrDefault; 
int index = location == null? -1:locations.IndexOf(location);


I have an IList Generic I'm trying to figure out and I'm having a hard time finding the correct format to call the IndexOf method in the class Locations : IList.

namespace Ilistprac
{
   class IList2
   {
      static void Main(string[] args)
      {

        string sSite = "test";

        Locations test = new Locations();

        test.Add(sSite)
      }
    }

  public class Location
  {
      public Location()
      {

      }

      private string _site = string.Empty;
      public string Site
      {
        get { return _site; }
        set { _site = value; }
      }
   }


  public class Locations : IList<Location>
  {
    List<Location> _locs = new List<Location>();

    public Locations() { }


    int IList<Location>.IndexOf(Location item)
    {
       return _locs.IndexOf(item);
    }

   public void Add(string sSite)
   {
     Location loc = new Location();
     loc.Site = sSite;
     _locs.Add(loc);
   }
  }
}
解决方案

It's not entirely clear what your question is. I am assuming from your comment //nothing that your question is one of these:


1:

If this is your question, then you have two options. You could change the implementation from an explicit interface member implementation to an implicit one:

public int IndexOf(Location item)
{
   return _locs.IndexOf(item); 
}

Alternatively, you can access the method through a reference of type IList<Location> rather than a Locations reference:

Locations locations = GetLocations();
Location location = GetLocation();
int index = ((IList<Location>)locations).IndexOf(location);

The first approach is more common and usually less verbose.


2:

If this is your question, it's because you haven't overridden the Equals(object) method in your Location class, and you're trying to find a location like this:

Locations locations = GetLocations();
string site = GetSite();
Location location = new Location { Site = site };
int index = ((IList<Location>)locations).IndexOf(location);

Here, index will always be -1 because IndexOf is testing the location objects using reference equality. You can override Equals if you want to consider two locations to be equal if and only if their Site properties are equal.

If that equality relation is not universally valid for the Location type, however, you shouldn't do that. In that case, you could use linq to find a location whose Site matches the desired value:

Location location = locations.Where(x => x.Site == site).FirstOrDefault;

If you then need the index of the location, simply do this (assuming that locations can't legitimately hold a null value):

Location location = locations.Where(x => x.Site == site).FirstOrDefault;
int index = location == null ? -1 : locations.IndexOf(location);

这篇关于尝试调用int IList&lt; Location&gt; .IndexOf(Location item)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:24