What is the difference between Convert.ToInt32 and (int)?

后端 未结 12 1593
星月不相逢
星月不相逢 2020-11-27 03:11

The following code throws an compile-time error like

Cannot convert type \'string\' to \'int\'

string name = Session[\"name1\"].ToString();
int i = (         


        
12条回答
  •  粉色の甜心
    2020-11-27 03:57

    Convert.ToInt32

        return int.Parse(value, CultureInfo.CurrentCulture);
    

    but (int) is type cast, so (int)"2" will not work since you cannot cast string to int. but you can parse it like Convert.ToInt32 do

提交回复
热议问题