- class Program
- {
- static void Main(string[] args)
- {
- Person r1 = new Person();
- r1.Name = "苟于伟";
- r1.Age = 18;
- r1.Pr("2");
- r1.Pr1(17, "tom");
- Console.ReadKey();
- }
- }
- class Person
- {
- public string name;//这是字段
- private int age;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- public string Name//这是属性
- {
- get { return name; }
- set { name = value; }
- }
- public void Pr(string a)
- {
-
- try
- {
- int i = Convert.ToInt32(a);//把i定义在tyrj中时
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.WriteLine("你好!{0}{1}",name,i);//不能访问到这个i
- }
- public void Pr1(int a, string b)
- {
- Console.WriteLine("我叫{0},今年{1}岁{2}{3}",name,age,a,b);
- }
-
复制代码 如果定义在try外面可要赋一个初值为0,万一转换失败不也就等于0了,怎么样让它转换失败时i为空。
|