Hello friend,
My Codes:
public interface IEnumerable2{
// Some members ...void MethodOfIEnumerable2();
}
public interface ICollection2 : IEnumerable2{
// Some members ...void MethodOfICollection2();
}
public interface IList2 : ICollection2, IEnumerable2{
// Some members ...void MethodOfIIList2();
}
///
/// Implement class.
///
public class ArrayList2:IList2,ICollection2,IEnumerable2{
// Some members ...
public void MethodOfIEnumerable2(){Console.WriteLine("IEnumerable2 In ArrayList2");}public void MethodOfICollection2(){Console.WriteLine("ICollection2 In ArrayList2");}public void MethodOfIIList2() {Console.WriteLine("IList2 In ArrayList2");}
}
///
/// Implement class.
///
public class ArrayList3 : IList2{ // Also correct.
// Some members ...
public void MethodOfIEnumerable2(){Console.WriteLine("IEnumerable2 In ArrayList3");}public void MethodOfICollection2() {Console.WriteLine("ICollection2 In ArrayList3");}public void MethodOfIIList2(){Console.WriteLine("IList2 In ArrayList3");}
}
class Program{
static void Main(string[] args) {
ArrayList2 al2 = new ArrayList2();ArrayList3 al3 = new ArrayList3();al2.MethodOfIEnumerable2();al2.MethodOfICollection2();al2.MethodOfIIList2();Console.WriteLine();al3.MethodOfIEnumerable2();al3.MethodOfICollection2();al3.MethodOfIIList2();Console.ReadKey(true);
}
}
Now, My question is:
Because ICollection 2 inherited IEnumerable 2, so IList2 indirectly inherited IEnumerable 2, but in a statement IList2, we repeat inherited IEnumerable2. Seem to repeat inherited? The c# compiler team members do so for what purpose?
Thank in advance. :)

VulpesPosted Feb 26, 2015, 6:45 AM
The reason it's done is to make it immediately clear to the programmer which interfaces an interface inherits or a class/struct implements. So, if you don't happen to know that ICollection2 inherits from IEnumerable2, then including the latter in the interface or class's 'inheritance list' makes it clear that it does.
There was a blog post by Eric Lippert (a former C# design team member) on this topic a while back which you might like to read:
http://blogs.msdn.com/b/ericlippert/archive/2011/04/04/so-many-interfaces.aspx
Ken HPosted Feb 26, 2015, 8:47 PM
Thank for Vulpes. :)