blink
blink

Reputation: 163

how do i cast an abstract list to it's concrete type

i have the following setup;

public abstract class StageObject
{
}
public class StageImage : StageObject
{
    public int Image;
}
public class StageStrip : StageObject
{
    public int Strip;
}

i do the following;

StageList = new List<StageObject>();
StageList.Add(new StageStrip());
StageList.Add(new StageImage());

i would love to be able to cast the array element as it's concrete type like this but i get an error;

(StageStrip) StageList[0].Strip = 2;

the only way i can do this is to create a temporary variable and cast it to the array. is there a way i can do it without creating the temporary variable?

Upvotes: 0

Views: 457

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

((StageStrip)StageList[0]).Strip = 2;

Upvotes: 8

Related Questions