It is not very clear your problem but I believe you can use keyword new
instead of overwriting the method:
namespace ClassLibrary1
{
public interface IObjetosBase
{
IList<IObjetosBase> get();
}
public class ObjetosBase : IObjetosBase
{
public virtual IList<IObjetosBase> get() { return new List<IObjetosBase>(); }
}
public class Aluno : ObjetosBase
{
public new IList<Aluno> get()
{
return new List<Aluno>();
}
}
}
I couldn’t test the code now for lack of time, but I also believe the class Aluno
should not return a list of Aluno
, Perhaps a Class Turma
yes should return a list of Aluno
or a repository Alunos
.
Edit:
Or You can popular the list of IObjetosBase
with objects of the type Aluno
:
public class Aluno : ObjetosBase
{
public override IList<IObjetosBase> get()
{
List<IObjetosBase> lista = new List<IObjetosBase>();
Aluno obj;
for (int i = 0; i < 10; i++)
{
obj = new Aluno();
lista.Add(obj);
}
return lista;
}
}
And then to go through the objects of the type Aluno
:
public void Processo()
{
IList<IObjetosBase> lista = this.get();
//Se for necessário checar o tipo do objeto:
foreach (var obj in lista)
{
if (obj is Aluno)
{
Aluno a = obj as Aluno;
}
}
//Se não for necessário checar o tipo do objeto:
foreach (Aluno obj in lista)
{
}
}
The
new
does not generate polymorphism and it seems that he wants it.– Maniero
Yeah, I put in to have an extra option for the colleague, his code is not very clear what the need is most always try to help. I edited and put another way that I believe, can help you
– Rovann Linhalis