I came across the following code, which is creating an instance of base class (AbstractClassType
):
AbstractClassType myVariable = someClassTypeArg.anotherClassTypeVar.EnumTypeVariable switch
{
AnotherEnumType.enum1 => new DerivedClass_1_FromAbstractClassType(),
AnotherEnumType.enum2 => new DerivedClass_2_FromAbstractClassType(),
_ => throw new Exception("The provided enum is not valid"),
};
var computeThisVariable = myVariable.GetKeyData(something, anotherThing);
I am quite confused with the above code. Why the Switch
statement is used (which C# concept is it...)?
abstract
so it can't be instantiated. What this is doing is instantiating one of the derived classes and returning a base class reference. – madreflectionDerivedClass_1_FromAbstractClassType
orDerivedClass_2_FromAbstractClassType
has to be created based on an enum value. – Olivier Jacot-DescombesAbstractClassType myVariable
? Is the variablemyVariable
an instance ofAbstractClassType
or an instance of theDerivedClass
? If I say thatmyVariable
is an instance of the classDerivedClass
, wouldn't it be misleading too? – skm