T
Tomasz Jastrzebski
Hello Developers,
I know it is a stupid question but why C# does not want to automatically
convert (up cast) list item types as it does with arrays? (example below)
I would like to be able to pass lists of MyBaseClass derived classes to
MyFunction. Do I need to implement some conversion? Somehow I never came
across this problem.
Thanks for any pointers.
Thomas
class Program
{
static void Main(string[] args)
{
List<MyDerivedClass> list = new List<MyDerivedClass>();
MyFunction(list);
}
static void MyFunction(List<MyBaseClass> list)
{
}
}
class MyBaseClass { }
class MyDerivedClass : MyBaseClass { }
I know it is a stupid question but why C# does not want to automatically
convert (up cast) list item types as it does with arrays? (example below)
I would like to be able to pass lists of MyBaseClass derived classes to
MyFunction. Do I need to implement some conversion? Somehow I never came
across this problem.
Thanks for any pointers.
Thomas
class Program
{
static void Main(string[] args)
{
List<MyDerivedClass> list = new List<MyDerivedClass>();
MyFunction(list);
}
static void MyFunction(List<MyBaseClass> list)
{
}
}
class MyBaseClass { }
class MyDerivedClass : MyBaseClass { }