Suppose I have two functions:
Foo(params INotifyPropertyChanged[] items)
{
//do stuff
}
Foo<T>(IEnumerable<T> items) where T : INotifyPropertyChanged
{
Foo(items.ToArray());
}
The second one allows me to call Foo
from a generic class with the constraint where T : INotifyPropertyChanged
, but the second resolves to itself so I get a stack overflow exception.
params
function from a generic class, assuming the generic type's constraints make it a viable option for the params
type?Thanks in advance!
You need to pass a INotifyPropertyChanged[]
, not a T[]
.
For example:
Foo<T>(IEnumerable<T> items) where T : INotifyPropertyChanged
{
Foo(items.Cast<INotifyPropertyChanged>().ToArray());
}
In general, however, it's better to call the IEnumerable
version from the params
version, like this:
Foo(params INotifyPropertyChanged[] items)
{
Foo((IEnumerable<INotifyPropertyChanged>) items);
}
Foo<T>(IEnumerable<T> items) where T : INotifyPropertyChanged
{
//do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With