D
D
Dmitry Korolev2018-05-11 13:58:18
C++ / C#
Dmitry Korolev, 2018-05-11 13:58:18

What is the correct way to implement IEnumerable for a generic class?

Please explain why the error?

public class GenericArray<T, S>: IEnumerable where T: ItemGenericArray<S>
{
  private T[] objs;
  public GenericArray() 
  {
    objs = null;
  }
  public GenericArray(S[] n)
  {
    objs = new T[n.Length];
    for (int i = 0; i < n.Length; i++)
      objs [i] = new T(n[i]);//Cannot create an instance of the variable type `T' because it does not have the new() constraint , `T': cannot provide arguments when creating an instance of a variable type
  }
  public int Length
  {
    get { return objs.Length; }
  }
  public S[] ToSystemArray()
  {
    if (objs != null) 
    {
      S[] t = new S[objs.Length];
      for (int i = 0; i < t.Length; i++)
        t [i] = objs[i].obj;
      return t;
    } 
    else
      return null;
  }
  public T this[int index]
  {
    get 
    {
      return objs[index];
    }
    set
    {
      objs[index] = value;
    }
  }
  IEnumerator IEnumerable.GetEnumerator()
  {
    for (int i = 0; i < objs.Length; i++) 
    {
      yield return objs[i];
    }
  }
  public IEnumerable GetEnumeratorPage(int max)
  {
    for (int i = 0; i < max; i++) 
    {
      if (i == objs.Length) 
      {
        yield break;
      }
      else
        yield return objs[i];
    }
  }
}
public class ItemGenericArray<S>
{
  public S obj { get; set; }
  public ItemGenericArray(S obj)
  {
    this.obj = obj;
  }
}
public class ItemAssetObjectGenericArray: ItemGenericArray<Object>
{
  public static ObjectSorter.namerAction na;
  public static string directory;
  public Object Obj 
  {
    get 
    {
      return obj;
    } 
    set 
    { 
      string path = AssetDatabase.GetAssetPath(value);
      if (!string.IsNullOrEmpty(path) && path.Contains(directory)) 
      {
        char[] splitter = { '/' };
        string[] dirs = path.Split(splitter);
        if (na == ObjectSorter.namerAction.sortableFiles) 
        {
          if (dirs.Length == 4 && (new Regex(@"№\d{4}\..*\.\w*$").Matches(dirs[dirs.Length - 1]).Count) == 1) 
          {
            if (dirs[dirs.Length - 1].Contains (".jpg") || dirs[dirs.Length - 1].Contains (".png") || dirs[dirs.Length - 1].Contains (".mpeg") || dirs[dirs.Length - 1].Contains (".ogg")) 
            {
              obj = value;
            }
          }
        } 
        else 
        {
          if (dirs.Length == 3 && (new Regex(@"№\d{4}\..*$").Matches(dirs[dirs.Length - 1]).Count) == 1) 
          {
            obj = value;
          } 
        }
      }
    }
  }
  public ItemAssetObjectGenericArray(Object o) : base(o) 	{	}
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Makarov, 2018-05-11
@adressmoeistranici

Nothing will work with a parameter in k-re, these are language restrictions, that you cannot require a constructor with parameters from type T (only without parameters is possible). Pass a factory function for values/objects of type T to your class.
And yes, implement IEnumerable first and not just IEnumerable.

A
Alexander Ter, 2018-05-11
@alexsandr0000

This is a limitation, T must implement these interfaces

D
DarkByte2015, 2018-05-11
@DarkByte2015

public class GenericArray<T, S>: IEnumerable where T: ItemGenericArray<S>

limitation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question