C# 返回IEnumerable<;IMyInterface>;从列表<;MyInterfaceClass>;

C# 返回IEnumerable<;IMyInterface>;从列表<;MyInterfaceClass>;,c#,interface,C#,Interface,编辑:此问题在.NET 4中无效,因为它实际上可以按需要工作 我有一个必须实现如下接口的数据类: public interface IData { IEnumberable<IOther> OtherList { get; } IOther AddOther(); void RemoveOtherData(IOther data); } 公共接口IData { IEnumberable其他列表{get;} IOther AddOther(); 无效删除其他数据(其

编辑:此问题在.NET 4中无效,因为它实际上可以按需要工作

我有一个必须实现如下接口的数据类:

public interface IData
{
   IEnumberable<IOther> OtherList { get; }
   IOther AddOther();
   void RemoveOtherData(IOther data);
}
公共接口IData
{
IEnumberable其他列表{get;}
IOther AddOther();
无效删除其他数据(其他数据);
}
但我必须在数据中声明实际成员

public class Data : IData
{
   // desired, always return the same reference
   public IEnumberable<IOther> OtherList { get { return _mOtherList } }
   // Non persistent reference not desirable.
   public IEnumerable<IOther> OtherList { get { return _mOtherList.Select(x => x as IOther); } }        
   List<IOther> _mOtherList = new List<Other>(); // error, type mismatch
   List<Other> _mOtherList = new List<Other>(); // error, property return type mismatch
   IEnumerable<IOther> _mOtherList = new List<Other>(); // ok, but cannot use List methods without casting.
}
公共类数据:IData
{
//如果需要,请始终返回相同的引用
public IEnumberable OtherList{get{return{u mOtherList}
//非持久引用是不可取的。
public IEnumerable OtherList{get{return{u mOtherList.Select(x=>x as IOther);}
List _mOtherList=new List();//错误,类型不匹配
List _mOtherList=new List();//错误,属性返回类型不匹配
IEnumerable _mOtherList=new List();//可以,但不能在不强制转换的情况下使用列表方法。
}

在这种情况下,什么是最好的解决方案?

其他类必须实现IOther接口,您不需要强制转换

当您声明mOtherList时,它是IEnumerable,因此不能使用list方法。将其声明为列表

public class Data : IData
{
   List<IOther> _mOtherList = new List<Other>();

   public IEnumberable<IOther> OtherList { get { return _mOtherList } }

   IOther AddOther()
   {
       return null;
   }
   void RemoveOtherData(IOther data){}
}

其他类必须实现IOther接口,您不需要强制转换

当您声明mOtherList时,它是IEnumerable,因此不能使用list方法。将其声明为列表

public class Data : IData
{
   List<IOther> _mOtherList = new List<Other>();

   public IEnumberable<IOther> OtherList { get { return _mOtherList } }

   IOther AddOther()
   {
       return null;
   }
   void RemoveOtherData(IOther data){}
}
公共类数据:IData
{
公共IEnumerable其他列表{get;private set;}
列表_mOtherList=新列表();
公共数据()
{
OtherList=mOtherList.Cast();
}
}
在.net上,4
IEnumerable
是共同变量。i、 e.实现
IEnumerable
的类也会自动实现
IEnumerable
。因此,我们也可以简单地写下:

public class Data : IData
{
   public IEnumerable<IOther> OtherList { get{return mOtherList;} }        
   List<Other> _mOtherList = new List<Other>();
}
公共类数据:IData
{
public IEnumerable OtherList{get{return mOtherList;}}
列表_mOtherList=新列表();
}
但我要避免这种情况,因为它打破了封装,允许外部人员修改您的列表

((List<Other>)MyData.OtherList).Add(...);
((List)MyData.OtherList).添加(…);
公共类数据:IData
{
公共IEnumerable其他列表{get;private set;}
列表_mOtherList=新列表();
公共数据()
{
OtherList=mOtherList.Cast();
}
}
在.net上,4
IEnumerable
是共同变量。i、 e.实现
IEnumerable
的类也会自动实现
IEnumerable
。因此,我们也可以简单地写下:

public class Data : IData
{
   public IEnumerable<IOther> OtherList { get{return mOtherList;} }        
   List<Other> _mOtherList = new List<Other>();
}
公共类数据:IData
{
public IEnumerable OtherList{get{return mOtherList;}}
列表_mOtherList=新列表();
}
但我要避免这种情况,因为它打破了封装,允许外部人员修改您的列表

((List<Other>)MyData.OtherList).Add(...);
((List)MyData.OtherList).添加(…);

由于IEnumerable是协变的,这很好:

    public interface IInterface{}

    public class ClassA : IInterface{}

    public class ClassB
    {
        private readonly List<ClassA> _classAs;

        public IEnumerable<IInterface> Data{ get { return _classAs; } }
    }
公共接口接口{}
公共类ClassA:IInterface{}
公共B类
{
私有只读列表_classAs;
公共IEnumerable数据{get{return\u classAs;}}
}

由于IEnumerable是协变的,这很好:

    public interface IInterface{}

    public class ClassA : IInterface{}

    public class ClassB
    {
        private readonly List<ClassA> _classAs;

        public IEnumerable<IInterface> Data{ get { return _classAs; } }
    }
公共接口接口{}
公共类ClassA:IInterface{}
公共B类
{
私有只读列表_classAs;
公共IEnumerable数据{get{return\u classAs;}}
}


界面中的第一个成员没有名字,因此无法编译。哇。。你很快。我还在编辑。为什么你不能说
\u mOtherList=new List()
?因为在其他服务器上私下处理数据时需要类型转换。我希望能找到一些语义来避免这种情况。@Jake你针对的是.NET Framework的哪个版本?界面中的第一个成员没有名字,因此无法编译。哇。。你很快。我还在编辑。为什么你不能说
\u mOtherList=new List()
?因为在其他服务器上私下处理数据时需要类型转换。我希望寻找一些语义来避免这种情况。@Jake你针对的是.NET Framework的哪个版本?你泄露了对你的私人收藏的引用到消费代码中。如果这是一个问题,你可以查看@CodeInChaos。我可以问一下这里的“泄露引用”是什么吗?@UfukHacıoğulları我的Visual Studio说
List\u mOtherList=new List(); 是一个编译错误,类型不匹配。@Jake
List List=newlist()在.NET4中不起作用。我们指的是
List=newlist()与IEnumerable publiclist=list一起使用。你泄露了你的私人收藏到消费代码的引用。如果这是一个问题,你可以查看@CodeInChaos我可以问一下这里的“泄露引用”是什么吗?@UfukHacıoğulları我的Visual Studio说
List _mOtherList=new List()是一个编译错误,类型不匹配。@Jake
List List=newlist()在.NET4中不起作用。我们指的是
List=newlist()与IEnumerable publiclist=list一起使用+1必须承认,在那次努力之后,这是覆盖所有基础的唯一答案。+1必须承认,在那次努力之后,这是覆盖所有基础的唯一答案。