Use this: Int32.Parse Method Converts the string representation of a number to its 32-bit signed integer equivalentpublic static int Parse( string s ); Exception Type Condition : ArgumentNullException s is a null reference (Nothing in Visual Basic). FormatException s does not consist solely of an optional negative sign followed by a sequence of digits ranging from 0 to 9. OverflowException s represents a number less than MinValue or greater than MaxValue. 比如 try { int a=Int32.Parse("1234"); } catch(上面列举的某个异常) { //handle exception }
string tmpStr = Console.ReadLine(); int index = Convert.ToInt16(tmpStr); 注意c#区分大小写,还有tmpStr既然已经是string类型,也就不需要在tmpStr.ToString()了
string tmpStr=Console.ReadLine();
int index=Convert.ToInt32(tmpStr.ToString());
c#的预定以引用类型包括object和string。object类型是其他所有类型的最基本的类型。string类型被用来声明符合统一字符编码标准的字符串值。
预定义类型都有预定义转换。C#可区别两种类型的转换:显示和隐示转换。
用object对象的ToString方法可以转化为string类型。
具体的内容你可以看看MSDN和有关的帮助。
对不起,刚才写错了。
Int32.Parse Method
Converts the string representation of a number to its 32-bit signed integer equivalentpublic static int Parse(
string s
);
Exception Type Condition :
ArgumentNullException s is a null reference (Nothing in Visual Basic). FormatException s does not consist solely of an optional negative sign followed by a sequence of digits ranging from 0 to 9. OverflowException s represents a number less than MinValue or greater than MaxValue. 比如
try
{
int a=Int32.Parse("1234");
}
catch(上面列举的某个异常)
{
//handle exception
}
int index = Convert.ToInt16(tmpStr);
注意c#区分大小写,还有tmpStr既然已经是string类型,也就不需要在tmpStr.ToString()了