1

In the example below, the function ICollection<Foo> Cast(List<Foo> stuff) does not return null:

public class Foo
{
    public string test { get; set; }
}

[TestClass]
public class Test
{
    private List<Foo> Foos = new List<Foo>();

    private ICollection<Foo> Cast(List<Foo> stuff)
    {
        return stuff as ICollection<Foo>;
    }

    [TestMethod]
    public void TestCast()
    {
        ICollection<Foo> stuff = Cast(Foos);
        Assert.IsTrue(stuff != null);
    }
}

But in the next example, it does return null. Why is this?

public interface IFoo { }

public class Foo : IFoo
{
    public string test { get; set; }
}

[TestClass]
public class Test
{
    private List<Foo> Foos = new List<Foo>();

    private ICollection<IFoo> Cast(List<Foo> stuff)
    {
        return stuff as ICollection<IFoo>;
    }

    [TestMethod]
    public void TestCast()
    {
        ICollection<IFoo> stuff = Cast(Foos);
        Assert.IsTrue(stuff != null);
    }
}
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42

0 Answers0