Ken H

Ken H

  • NA
  • 646
  • 354.6k

About interface repeat inheritance question in c#?

Feb 26 2015 4:36 AM
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();
}
/// <summary>
/// Implement class.
/// </summary>
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");
}
}
/// <summary>
/// Implement class.
/// </summary>
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. :)
 

Answers (2)