Cast.Cast<int?> applied on generic enum collection results in invalid cast exception

前端 未结 2 499
情书的邮戳
情书的邮戳 2021-01-12 13:11
enum Gender { Male, Female }

var k = new[] { Gender.Male }.Cast().ToList().Cast().ToList(); //alright

var p = new[] { Gender.Male }.Cast<         


        
2条回答
  •  心在旅途
    2021-01-12 13:34

    This is due to deferred execution. The latter statement actually tries to cast Gender.Male to int?. In contrast, the first statement actually performs the cast operation to an int and gets a List before deferring the execution to cast those to int?; clearly int to int? has an implicity conversion defined already.

提交回复
热议问题