I'm learning C# and I need some help to understand variable scopes.
I have 3 different classes, 1 base class and 2 inherited. I want to define a function which creates a new instance of the inherited classes and return it.
My code so far:
class Base
{
public Base(){}
}
class Divided1 : Base
{
public Divided1(){}
}
class Divided2 : Base
{
public Divided2(){}
}
private Base selectType()
{
Base base;
string type = Console.ReadLine();
if (type == "1")
{
base = new Divided1();
}
else if (type == "2")
{
base = new Divided2();
}
return base;
}
I got an error
Use of unassigned local variable 'base'
I'm not sure why is it wrong.