There is no implicit reference conversion from 'System.Collections.Generic.List' to 'T'

前端 未结 3 822
孤街浪徒
孤街浪徒 2021-01-03 21:25
class Class1
{
    public virtual void Update(T entity)
    {
        Update(new List() { entity }); //It\'s failed
    }

    public virtual void          


        
3条回答
  •  鱼传尺愫
    2021-01-03 22:05

    You are essentially asking why the compiler is not creating an implicit cast from List to IEnumerable. The reason is that the C# team made a deliberate design decision that cases of potential ambiguity must be resolved by the programmer, not the compiler. (Note that the VB.NET team made a different decision, to always attempt something sensible that is consistent with the perceived programmer intent.)

    The advantages in a case such as this are that surprise is minimized - nothing unexpected can happen under the covers; the disadvantage is the occasional need for more verbose code.

提交回复
热议问题