【问题标题】:С# constraint generic does not work and brain-breakingС# 约束泛型不起作用和脑残
【发布时间】:2021-08-06 16:01:15
【问题描述】:

我很困惑。我如何创建该类的示例?这个错误说:类型'type1'不能用作泛型类型或方法''中的类型参数'T'。没有从“type1”到“type2”的隐式引用转换。但是我哪里做错了?

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());
    }
}

【问题讨论】:

  • IMyList&lt;int&gt; list = new MyList&lt;int&gt;();

标签: c# list generics arraylist integer


【解决方案1】:

public class Mylist&lt;T&gt; where T: IMyList&lt;T&gt; 行是错误的。您要做的是将T 类型限制为IMyList&lt;T&gt;。这样的递归类型约束不起作用。你真正想做的是实现接口IMyList&lt;T&gt;。您在这里根本不需要类型约束。正确的行是public class MyList&lt;T&gt; : IMyList&lt;T&gt;

您的代码还有其他一些问题;我冒昧地纠正它们:

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);
        }
    }
}

我测试了这段代码,它编译时没有错误或警告。

【讨论】:

  • 非常感谢,我只是学习和理解
  • 没问题!学习一门新的编程语言很难,而且你的代码一点也不差。只是备注:.NET 已经包含一个名为IList&lt;T&gt; 的接口(在System.Collections.Generic 命名空间中)。与其定义自己的界面,不如只使用这个界面(取决于您的用例)。
  • 听取了您的建议,也找到了这个stackoverflow.com/questions/14324987/…,我知道我会变得更好!)谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多