问题
I've noticed that a List<T>
cannot be passed to methods such as:
Foo<T>(ref ICollection<T> bar)
Foo<T>(out ICollection<T> bar)
Taking into account that, on the other side, Foo<T>(ICollection<T> bar)
does accept a List<T>
as argument, could anyone please explain the described behavior to me?
Complete example:
public class AuxClass { }
public class Test
{
private void NonRefFoo<T>(ICollection<T> intCollection) {; }
private void RefFoo<T>(ref ICollection<T> intCollection) {; }
private void OutFoo<T>(out ICollection<T> intCollection) { intCollection = null; }
public void Foo()
{
{
List<int> list = new List<int>() { 0, 1, 2, 3, 5, 8, 13 };
List<AuxClass> classList = new List<AuxClass>() { new AuxClass() };
NonRefFoo<int>(list);
RefFoo<int>(ref list); // Invalid
OutFoo<AuxClass>(out List<int> outListInt); // Invalid
NonRefFoo<AuxClass>(classList);
RefFoo<AuxClass>(ref classList); // Invalid
OutFoo<AuxClass>(out List<AuxClass> outListClass); // Invalid
}
{
ICollection<int> collection = new List<int>() { 0, 1, 2, 3, 5, 8, 13 };
ICollection<AuxClass> classCollection = new List<AuxClass>() { new AuxClass() };
NonRefFoo<int>(collection);
OutFoo<int>(out ICollection<int> outCollectionInt);
RefFoo<int>(ref collection);
NonRefFoo<AuxClass>(classCollection);
RefFoo<AuxClass>(ref classCollection);
OutFoo<AuxClass>(out ICollection<AuxClass> outCollectionClass);
}
}
}
Thanks in advance
回答1:
With the ref there is a possibility (and with the out a certainty) that you will assign a new object to property you pass in. In this case anything of type ICollection<T>
. If you pass in an object of type List<T>
you might end up assigning an object of a unrelated type that implements ICollection<T>
. The c# type system doesn't allow that and the compiler knows this and so stops you doing it
来源:https://stackoverflow.com/questions/48351097/passing-listt-by-reference-to-a-method-accepting-ref-icollectiont