Hello,
I am getting a compiler error, which seems illogical to me.
It would take longer to describe the problem, than to reproduce it.
Just insert, please, a slash after the very first slash in the code below, and the compiler will make the problem obvious.
Could somebody tell me if this is a bug in the compiler or I am missing something, and the compiler works as designed?
using System;
using System.Collections;
using System.Collections.Generic;
namespace TestConsoleApp {
class CEnumerable<ElemTypeBase> : IEnumerable<ElemTypeBase> where ElemTypeBase : class {
public IEnumerator<ElemTypeBase> GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
}
class CElemTypeBase { }
class CElemTypeDerived : CElemTypeBase { }
class Program {
/* // To see the problem, insert a slash after the very first slash in this line and compile.
// Why the line below causes problems,
static public void MethodElemEnumArg(CEnumerable<CElemTypeBase> enumArg) { } // Problematic signature.
/*/ // while the next line does not?
static public void MethodElemEnumArg(IEnumerable<CElemTypeBase> enumArg) { }
//*/
static public void MethodElemArg(CElemTypeBase elemArg) { }
static void Main() {
CEnumerable<CElemTypeBase> enumBase = new CEnumerable<CElemTypeBase>();
CEnumerable<CElemTypeDerived> enumDerived = new CEnumerable<CElemTypeDerived>();
CElemTypeBase argBase = new CElemTypeBase();
CElemTypeBase argDerived = new CElemTypeDerived();
MethodElemArg(argBase);
MethodElemArg(argDerived); // Surely works.
MethodElemEnumArg(enumBase);
MethodElemEnumArg(enumDerived); // Produces compiler error with the problematic signature.
}
}
}