-3

i am confused. How i may create example of that class? this error said: The type 'type1' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from 'type1' to 'type2'. But where i am did wrong?

public interface IMyList<T>
{
    void Add(T a);
    T this[int index] { get; }
    int Count { get; }
    void Clear();
    bool Contains(T item);
}

public class Mylist<T> where T: IMyList<T>
{
    public T this[int index]
    {
        get { return this[index]; }
    }

    public List<T> array = null;

    public int Count()
    {
        int a = 0;
        foreach (var item in array)
        {
            a++;
        }
        return a;
    }

    public Mylist()
    {
        this.array = new List<T>(0);
        
    }

    public void Add(T a)
    {
        array.Add(a);
    }

    public void Clear()
    {
        throw new NotImplementedException();
    }

    public bool Contains(T item)
    {
        throw new NotImplementedException();
    }
}


class Program
{
    static void Main()
    {
        IMyList<int> list = new Mylist<IMyList<int>>() as IMyList<int>; //cs0311
        Mylist<IMyList<int>> mylist = new Mylist<IMyList<int>>(); //cs0311
        //a.Add(1);
        //Console.WriteLine(a.Count());
    }
}
Shy
  • 45
  • 1
  • 8

1 Answers1

1

The line public class Mylist<T> where T: IMyList<T> is wrong. What you are trying to do is to constrain the type T to IMyList<T>. Such a recursive type constraint cannot work. What you really want to do is to implement the interface IMyList<T>. You do not need type constraints at all here. The correct line is public class MyList<T> : IMyList<T>.

There are some other issues with your code; I took the liberty to correct them:

using System;
using System.Collections.Generic;

namespace Example
{
    public interface IMyList<T>
    {
        void Add(T a);
        T this[int index] { get; }
        int Count { get; }
        void Clear();
        bool Contains(T item);
    }

    public class MyList<T> : IMyList<T>
    {
        public T this[int index]
        {
            get { return this[index]; }
        }

        public List<T> array = null;

        public int Count
        {
            get
            {
                int a = 0;
                foreach (var item in array)
                {
                    a++;
                }
                return a;
            }
        }

        public MyList()
        {
            this.array = new List<T>(0);

        }

        public void Add(T a)
        {
            array.Add(a);
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(T item)
        {
            throw new NotImplementedException();
        }
    }


    class Program
    {
        static void Main()
        {
            IMyList<int> list = new MyList<int>();
            list.Add(1);
            Console.WriteLine(list.Count);
        }
    }
}

I tested this code and it compiles without errors or warnings.

Xaver
  • 1,035
  • 9
  • 17
  • Thank you very much, I am just learning and understood – Shy May 17 '21 at 11:59
  • No problem! Learning a new programming language is hard, and your code was not bad at all. Just a remark: .NET already contains an interface called `IList` (in the `System.Collections.Generic` namespace). Instead of defining your own interface, it is probably a good idea (depending on your use case) to just use this interface. – Xaver May 17 '21 at 12:14
  • took your advice and also found this https://stackoverflow.com/questions/14324987/why-isnt-array-a-generic-type and i inderstood I will get better!) thanks – Shy May 17 '21 at 14:11