Does anyone have a suggestion to make this code more type safe? Ideally I'd like to get a compile error on the foreach below, forcing myself to use the interface instead of the class. Maybe this isn't possible but I'm open to suggestions to get the type safety accomplished.
public class ManagementEntity : IManagementEntity
{
}
public static void Delete(IEnumerable %26lt;IManagementEntity%26gt; list)
{
....Guard.ArgumentNotNull (list, "list");
....foreach (ManagementEntity item in list) // %26lt;- use the interface instead!
....{
....}
}
Thanks!
C# - foreach type safety?
If your list is created using generics, then the list can only hold one type.
Reply:Which interface would you like to use (you didn't specify)?
If you are enumerating a collection of type Object like this:
foreach (Control c in myObjectArray)
The foreach loop will skip objects in myObjectArray that are not of type Control.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment