一点一点学C#2
2016-07-08 21:54
387 查看
关于不同的命名空间:不同的命名空间里相同的类名不产生冲突
C#预处理指令:指导编译器在实际编译前开始对信息进行处理
格式:①以#开头;②只有空格可以出现在预处理指令之前,且一个指令在一行;③不以;结尾
C#异常处理:
委托 delegate
using System; namespace test1 { class test { public void func() { Console.WriteLine("in test1"); } } } namespace test2 { class test { public void func() { Console.WriteLine("in test2"); } } } class testClass { public static void Main(String[] args) { test1.test aa = new test1.test(); //命名空间名.类名 test2.test bb = new test2.test(); aa.func(); bb.func(); Console.ReadLine(); } }
C#预处理指令:指导编译器在实际编译前开始对信息进行处理
格式:①以#开头;②只有空格可以出现在预处理指令之前,且一个指令在一行;③不以;结尾
预处理器指令 | 描述 |
---|---|
#define | 它用于定义一系列成为符号的字符。 |
#undef | 它用于取消定义符号。 |
#if | 它用于测试符号是否为真。 |
#else | 它用于创建复合条件指令,与 #if 一起使用。 |
#elif | 它用于创建复合条件指令。 |
#endif | 指定一个条件指令的结束。 |
#line | 它可以让您修改编译器的行数以及(可选地)输出错误和警告的文件名。 |
#error | 它允许从代码的指定位置生成一个错误。 |
#warning | 它允许从代码的指定位置生成一级警告。 |
#region | 它可以让您在使用 Visual Studio Code Editor 的大纲特性时,指定一个可展开或折叠的代码块。 |
#endregion | 它标识着 #region 块的结束。 |
#define PI #define val using System; namespace test1 { class test { public static void Main(String [] args) { #if(PI && val) Console.WriteLine("PI and val are defined"); #elif(!PI && val) Console.WriteLine(" val is defined"); #elif(!val && PI) Console.WriteLine("PI is defined"); #else Console.WriteLine("PI and val are not defined"); #endif Console.ReadKey(); } } }
C#异常处理:
异常类 | 描述 |
---|---|
System.IO.IOException | 处理 I/O 错误。 |
System.IndexOutOfRangeException | 处理当方法指向超出范围的数组索引时生成的错误。 |
System.ArrayTypeMismatchException | 处理当数组类型不匹配时生成的错误。 |
System.NullReferenceException | 处理当依从一个空对象时生成的错误。 |
System.DivideByZeroException | 处理当除以零时生成的错误。 |
System.InvalidCastException | 处理在类型转换期间生成的错误。 |
System.OutOfMemoryException | 处理空闲内存不足生成的错误。 |
System.StackOverflowException | 处理栈溢出生成的错误。 |
using System; namespace test1 { class test1 { public static void Main(String [] args) { int[] A = new int[5] {1,2,3,4,5}; try //将被激活的特定的异常代码块 { Console.WriteLine(A[5]); } catch(IndexOutOfRangeException e) //捕获异常 { Console.WriteLine("Excption caught:{0}",e); //throw e; //抛出异常 } finally //不管异常是否抛出都会执行 { Console.WriteLine(A[0]); } Console.ReadLine(); } } }
委托 delegate
using System; namespace test1 { delegate int fun(int n); //委托 class test1 { static int num = 6; public static int AddNum(int x) { num += x; return num; } public static int MultNum(int x) { num *= x; return num; } public static int getNum() { return num; } public static void Main(String [] args) { fun f1 = new fun(AddNum); fun f2 = new fun(MultNum); fun f = f1 + f2; //多播 Console.WriteLine("Add:{0}",f1(2)); Console.WriteLine("Mult:{0}",f2(2)); Console.WriteLine("Mul:{0}",f(2)); Console.ReadLine(); } } }
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法