[ACCEPTED]-List<> Get Next element or get the first-c#

Accepted answer
Score: 24
lastAgentIDAarhus = agents[index == -1 ? 0 : index % agents.Count];

The use of the MOD operator % atuomatically 7 chops the index to the range of possible 6 indexes.

The modulo operator is the compliment 5 to the DIV (/) operator and returns the 4 remainder of a division of two whole numbers. For 3 example if you divide 9 by 6 the result 2 is 1 with a remainder of 3. The MOD operator 1 asks for the 3.

Score: 15

Or simply:

public static T NextOf<T>(this IList<T> list, T item)
{
    var indexOf = list.IndexOf(item);
    return list[indexOf == list.Count - 1 ? 0 : indexOf + 1];
}

Example:

List<string> names = new List<string>();

names.Add("jonh");
names.Add("mary");

string name = String.Empty;    

name = names.NextOf(null);   //name == jonh

name = names.NextOf("jonh"); //name == mary

name = names.NextOf("mary"); //name == jonh

0

Score: 6

As a slightly different take on this, here's 3 an extension method you could use to make 2 any IEnumerable 'circular'

  public static IEnumerable<T> AsCircularEnumerable<T>(this IEnumerable<T> enumerable)
  {
    var enumerator = enumerable.GetEnumerator();
    if(!enumerator.MoveNext())
      yield break;

    while (true)
    {
      yield return enumerator.Current;
      if(!enumerator.MoveNext())
        enumerator = enumerable.GetEnumerator();
    }
  }

so you could use 1 that like this

  var agents = new List<int> {1, 2, 3, 4, 123, 234, 345, 546};

  foreach(var i in agents.AsCircularEnumerable())
  {
    Console.WriteLine(i);
  }

Which will just keep going... :)

Score: 4

I like Vinicius's idea of using an extension method. Unfortunately 7 the code he posted will throw an exception. This 6 is based on his, but it will not throw an 5 exception and it is very straightforward 4 and easy to read (IMHO):

public static T Next<T>(this IList<T> list, T item)
{
    var nextIndex = list.IndexOf(item) + 1;

    if (nextIndex == list.Count)
    {
        return list[0];
    }

    return list[nextIndex];
}

It will return the 3 first item in the list if the item passed 2 in is not in the list, since list.IndexOf(item) will return 1 -1 in that case;

Score: 2

Not a great difference, but at least some 1 less code (at least in the editor) ;o)

List<int> agents = taskdal.GetOfficeAgents(Branches.aarhusBranch);
if (lastAgentIDAarhus != -1)
{
    int index = agents.IndexOf(lastAgentIDAarhus);
    lastAgentIDAarhus = (agents.Count > index + 1 ? agents[index + 1] : agents[0]); 
}
else
{
    lastAgentIDAarhus = agents[0];
}
Score: 2

What do you think if we add some check to 1 avoid common errors ?

public static class ListExtensions
{
    public static TType Next<TType>(this IList<TType> list, TType item)
    {
        if (list == null) return default(TType);

        var itemIndex = list.IndexOf(item);
        if (itemIndex < 0) return list.FirstOrDefault();

        var nextIndex = itemIndex + 1;

        return nextIndex >= list.Count 
            ? list.FirstOrDefault() 
            : list[nextIndex];
    }
}

More Related questions