I have created an object and implemented the interface IEnumerable<Options>
.
If I try to loop through my object it is working fine but the variables aren't casted to a Options
but object
.
public class MyClass : IEnumerable<Options>
{
// I just cut the non-relevant code. The real class is over 500 lines ...
public List<Options> options = new List<Options>();
IEnumerator<Options> IEnumerable<Options>.GetEnumerator()
{
return options.GetEnumerator();
}
public IEnumerator GetEnumerator()
{
return options.GetEnumerator();
}
}
How I loop through:
foreach (var option in myClass)
{
// Do something
}
typeof(option)
returns object
. But since I implemented the generic interface I want it to be returned as Options
. It is indeed an Options
object I can explicitly cast it with (Options)option
but it should be automatically casted.
foreach (Options option in myClass)
{
// I want it to be casted automatically when using 'var'
}