Casting object to int throws InvalidCastException in C#

前端 未结 3 635
天涯浪人
天涯浪人 2020-12-03 22:54

I have this method:

private static Dossier PrepareDossier(List> rawDossier)
{
    return new Dossier((int)rawDossier[0][0]);
}
         


        
3条回答
  •  孤城傲影
    2020-12-03 23:02

    I would guess this is because the object in your list is not an int.

    Convert.ToInt32 will convert other non-int types so works.

    Check what is being passed in to the method.

提交回复
热议问题