Question: I have following class:

```
public class TestClass
{
    public IEnumerable<Expression<Func<object, object>>> TestField { get; set; }
}

```

I tried to create a TestClass object like this:

```
new TestClass 
{
    TestField = new Expression<Func<OtherClass, object>>[]
    {
        otherClass => otherClass.FieldX,
        otherClass => otherClass.FieldY
    }
}

```

But I get following IntelliSense error:
**Cannot implicitly convert type 'OtherClass' to 'object'**

How do I implement this with keeping my `TestClass` definition generic so I can create a `TestClass` object with e.g. `OtherClass`, `AnotherClass`, or any other class?

Answer: 
You cannot cast a generic class of one type to a generic class of a derived type. Just because the generic type of the classes are connected by inheritance doesn't mean the class itself is. For example, you cannot cast `List<Apple>` to `List<Fruit>`, because as far as the program is concerned, those two lists are completely different classes.

In your case, you can solve your generics issue by making `TestClass` itself generic:

```
public class TestClass<T>
{
    public IEnumerable<Expression<Func<T, object>>> TestField { get; set; }
}

```

Then you can create an instance of `TestClass` like so:

```
new TestClass<OtherClass>
{
    TestField = new Expression<Func<OtherClass, object>>[]
    {
        otherClass => otherClass.FieldX,
        otherClass => otherClass.FieldY
    }
}

```
Like this?

```
new TestClass
{
    TestField = new Expression<Func<object, object>>[]
    {
            otherClass => ((OtherClass)otherClass).FieldX,
            otherClass => ((OtherClass)otherClass).FieldY
    }
};

```