c#基础知识第六节

 

异常

当程序出现异常时,如果不处理程序就可能会崩溃。

class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入一个数字");
int i = Convert.ToInt32(Console.ReadLine());
int s = 100 / i;
Console.WriteLine(s);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

}
}

装箱和拆箱(该类型可以存储任何类型的数据)

c#中提供了一个特殊类型object类型。

装箱就是将值类型转化为object类型,拆箱就是把object类型转化为值类型。

class Program
{
static void Main(string[] args)
{
int i = 100;
object o = i;//装箱
Console.WriteLine(o);

i = (int)o;//拆箱
Console.WriteLine(i);
}
}

枚举(enum type)

是将可能出现的值一一列举出来。

enum Color
{
Red,
Green,
Blue
}
class Program
{
static void Main(string[] args)
{
Color c = Color.Red;
if(c==Color.Red)
{
Console.WriteLine("红色");
}
else
{
Console.WriteLine("不是红色");
}
}
}

 

posted @ 2017-10-10 09:20  你的斗志并没有失去  阅读(85)  评论(0编辑  收藏  举报