C#学习笔记(一):C#基础语法千行(含实例)

// C#程序结构
using System; //导入命名空间
namespace HelloCSharpApplication // 声明命名空间
{
	class HelloCSharp // 声明类
	{
		static void Main(string[] args) // 声明方法 Main方法是入口点 
		{
			Console.WriteLine("HelloCSharp");  // 控制台打印
		}
	}
}

// C#基本语法
using System;
namespace RectangleApplication
{
	class Rectangle
	{
		// 成员变量
		double length;
		double width;
		// 成员赋值
		public void Acceptdetails()
		{
			length = 4.5;
			width = 3.5;
		}
		public double GetArea()
		{
			return length * width;
		}
		public void Display()
		{
			Console.WriteLine("Area :{0}", GetArea());
		}
	}

	class ExecuteRectangle
	{
		static void Main(string[] args)
		{
			// 类的实例化 类里调用其他类
			Rectangle r = new Rectangle();
			// 调用类的方法
			r.Acceptdetails();
			r.Display();
			Console.ReadLine();
		}
	}
}

// C#数据类型 bool byte char decimal double float int long sbyte short uint ulong ushort
// sizepof(type)产生以字节为单位的存储尺寸
// 字符串类型的转义方法

string str = @"C:\Windows";
string str = "C:\\Windows";

// 类型转换方法
// 装箱 值类型转换为对象类型
int val = 8;
object obj = val;

// 拆箱 对象类型转换为值对象
int val = 8;
object obj = val;
int nval = (int) obj;

// C# 类型转换
using System;
namespace TypeConversionApplication
{
	class ExplicitConversion
	{
		static void Main(string[] args)
		{
			double d = 777.777
			int i

			// double -> int
			i = (int) d;
			Console.WriteLine(i);
			Console.RedaKey();
		}
	}
}

// string -> int
using System;
namespace StudyCSharpTest
{
	class StudyCSharp
	{
		static void Main(string[] args)
		{
			string losctr = 123.ToString();
			int i = Convert.ToInt16(losctr);
			int ii = int.Parse(losctr);
			Console.WriteLine(i);
			Console.WriteLine(ii);
		}
	}
}

// int.TryParse(string s,out int i)
// 该方式也是将数字内容的字符串转换为int类型,但是该方式比int.Parse(string s) 好一些,它不会出现异常,最后一个参数result是输出值,如果转换成功则输出相应的值,转换失败则输出0。
using System;
namespace StudyCSharpTest
{
	class StudyCSharp
	{
		static void Main(string[] args)
		{
	        string s1="abcd";
	        string s2="1234";
	        int a,b;
	        bool bo1=int.TryParse(s1,out a);
	        Console.WriteLine(s1+" "+bo1+" "+a);
	        bool bo2=int.TryParse(s2,out b);
	        Console.WriteLine(s2+" "+bo2+" "+b);
	    }
	}
}

// C#变量
//  
int i, j, k;
char c, ch;
float f;
double d;

// 变量初始化
  = value;
int i = 100, f = 5;
char x = 'x';

using System;
namespace VariableDefinition
{
	class Program
	{
		static void Main(string[] args)
		{
			short a;
			int b;
			double c;

			// 初始化变量
			a = 10;
			b = 15;
			c = a + b;
			Console.WriteLine("c = {0}", c);
		}
	}
}


// C# 中所有可用的算术运算符
using System;

namespace OperatorApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            int b = 10;
            int c;

            c = a + b;
            c = a - b;
            c = a * b;
            c = a / b;
            c = a % b;
            c = ++a;
            c = --a;
            c = a;
            c += a;
            c -= a;
            c *= a;
            c /= a;
            c %= a;
            c <<= 2; //二进制左移
            c >>= 2; //二进制右移
            c &= 2;
            c ^= 2;
            c |= 2;
        }
    }
}


c = a++: 先将 a 赋值给 c,再对 a 进行自增运算。
c = ++a: 先将 a 进行自增运算,再将 a 赋值给 c 。
c = a--: 先将 a 赋值给 c,再对 a 进行自减运算。
c = --a: 先将 a 进行自减运算,再将 a 赋值给 c 。

using System;

namespace OperatorApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b;

            // a++ 先赋值再进行自增运算
            b = a++;  //b = 1, a = 2
            // ++a 先自增运算再进行赋值
            b = ++a;  //b = 2, a = 2
            // a-- 先赋值再进行自减运算
            b = a--;  //b = 1, a = 0
            // --a 先自减运算再进行赋值
            b = --a;  //b = 0, a = 0

        }
    }
}

// C#判断
//Exp1 ? Exp2 : Exp3; Exp1为真 取Exp2 否则去 Exp3

// if语句
using System;
namespace StudyCSharpTest
{
	class StudyCSharp
	{
		static void Main(string[] args)
		{
			int a = 6;
			if (a < 5)
			{
				Console.WriteLine("{0}",a+5);
			}
			else if (a < 10 & a >=5 )
			{
				Console.WriteLine("{0}",a+5);
			}
			else
			{
				Console.WriteLine(a);
			}
		}
	}
}

// switch 语句
using System;
namespace StudyCSharpTest
{
	class StudyCSharp
	{
		static void Main(string[] args)
		{
			int a = 5;
			int b = 10;
			switch(a)
			{
				case 5:
					switch(b)
					{
						case 10:
							Console.WriteLine("1");
							break;
						default:
							Console.WriteLine("0");
							break;
					}
					break;
				default:
					Console.WriteLine("0");
					break;
			}
		}
	}
}

// C# 循环
// while 循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 10;
			// while 循环
			while (a < 20)
			{
				Console.WriteLine(a);
				a++;
			}
		}
	}
}

// for/foreach 循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			// for循环 for(参数(已声明变量不用再声明); 条件; 参数处理)

			for (int a = 10; a < 20; a = a + 1)
			// a = 10;
			// for (; a < 20; a += 1)
			{
				Console.WriteLine(a);
			}
			Console.ReadLine();
		}
	}
}

// foreach 循环
using System;
namespace StudyCSharpTest
{
	class StudyCSharp
	{
		static void Main(string[] args)
		{
			int a = 5;
			int[] Intarray = new int[] {1,1,2,3,8,2,1,3};
			foreach (int i in Intarray) // 依次迭代数组
			{
				Console.WriteLine(i); // 每次循环执行的内容
			}
		}
	}
}

// do...while循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 10;

			// do...while循环
			do
			{
				Console.WriteLine(a);
				a += 1;
			}while(a < 20); // 返回false,退出循环
		}
	}
}

// 嵌套循环
// 嵌套 for 循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			int i, j;
			for (i = 2; i < 100; i++)
			{
				for (j = 2; j <= (i / j); j++)
					if((i % j) == 0) break;
				if (j > (i / j))
					Console.WriteLine("质数{0}",i)
			}
		}
	}
}

// 嵌套 while循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 10;
			int b = 20;
			while(a < 20)
			{
				Console.WriteLine("a:{0}",a);
				a++;
				while(b < 25)
				{
					Console.WriteLine("b:{0}",b);
					b++;
				}
			}
		}
	}
}

// 嵌套 do...while循环
using System;
namespace Loops
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 10;
			int b = 20;
			// do...while 先执行后判断 所以当 a:10 b:20 21 22 23 24之后 a:11 b:25 a:12 b:26...
			do
			{
				Console.WriteLine("a:{0}",a);
				a++;
				do
				{
					Console.WriteLine("b:{0}",b);
					b++;
				}while(b < 25);
			}while(a < 20);
		}
	}
}

// C#封装
// Public 公有成员 不同类可以访问
using System;
namespace RectangleApplication
{
	class Rectangle
	{
		// 成员变量
		public double length;
		public double width;

		public double GetArea()
		{
			return length * width;
		}
		public void Display()
		{
			Console.WriteLine(GetArea());
		}
	}

	class ExecuteRectangle
	{
		static void Main(string[] args)
		{
			Rectangle r = new Rectangle();
			r.length = 3.5;
			r.width = 2.5
			r.Display();
		}
	}
}

// Private 私有成员 同一个类可以访问
using System;

namespace RectangleApplication
{
    class Rectangle
    {
        //成员变量
        private double length = 5;
        private double width = 4;

        public void Acceptdetails()
        {
			length = 4;
			width = 3;
        }
        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长度: {0}", length);
            Console.WriteLine("宽度: {0}", width);
            Console.WriteLine("面积: {0}", GetArea());
        }
    }//end class Rectangle    
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Acceptdetails();
            r.Display();
            Console.ReadLine();
        }
    }
} // 结果 length 4  width 3 area 12;

//C#方法
//   (Parameter List)
// {
//	Method Body
// }
/*
Access Specifier:访问修饰符,这个决定了变量或方法对于另一个类的可见性。
Return type:返回类型,一个方法可以返回一个值。返回类型是方法返回的值的数据类型。如果方法不返回任何值,则返回类型为 void。
Method name:方法名称,是一个唯一的标识符,且是大小写敏感的。它不能与类中声明的其他标识符相同。
Parameter list:参数列表,使用圆括号括起来,该参数是用来传递和接收方法的数据。参数列表是指方法的参数类型、顺序和数量。参数是可选的,也就是说,一个方法可能不包含参数。
Method body:方法主体,包含了完成任务所需的指令集。
*/

// C#调用方法
using System;
namespace CalculatorApplication
{
	class NumberManipulator
	{
		public int FindMax(int num1, int num2)
		{
			// 局部变量
			int result;

			if (num1 > num2)
				result = num1;
			else
				result = num2;
			return result;
		}
		static void Main(string[] args)
		{
			// 局部变量
			int a = 100;
			int b = 200;
			int ret;
			NumberManipulator n = new NumberManipulator();

			// 调用方法
			ret = n.FindMax(a, b);
			Console.WriteLine("最大值是:{0}",ret);
			Console.ReadLine();
		}
	}
}

// 递归方法调用
using System;
namespace CalculatorApplication
{
	class NumberManipulator
	{
		public int factorial(int num)
		{
			int result;
			if (num == 1)
			{
				return 1;
			}
			else
			{
				result = factorial(num -1) * num;
				return result;
			}
		}

		static void Main(string[] args)
		{
			NumberManipulator n = new NumberManipulator();
			Console.WriteLine(n.factorial(8));
		}
	}
}

// C# 可空类型 ?
//  ?  = null;
using System;
namespace CalculatorApplication
{
	class NullablesAtShow
	{
		static void Main(string[] args)
		{
			int? num1 = null;
			double? num2 = new double?();
			// double? num2 = null;
			bool? boolval = new bool?();
		}
	}
}

// C# 合并运算符 ??
using System;
namespace CalculatorApplication
{
	class NullablesAtShow
	{
		static void Main(string[] args)
		{
			double? num1 = null;
			double? num2 = 3.1415;
			double num3;
			num3 = num1 ?? 7.77; // 7.77
			num3 = num2 ?? 7.77; // 3.1415
			Console.WriteLine(num3);
		}
	}
}

// C# 数组
// 创建数组
double[] balance = new double[10];
double[] balance = {2340.0, 777.7, 555.55};
int[] marks = new int[5] {1, 3, 4, 6, 9};
int[] marks = new int[] {1, 3, 4, 6, 9};
// 数组赋值
double[0] = 4500.0;
// 赋值一个数组变量到另一个目标数组变量中
int[] marks = new int[] {1, 3, 4, 6, 9};
int[] score = marks;

// 访问数组元素
double salary = balance[9];

// for循环遍历数组
using System;
namespace ArrayApplication
{
	class MyArray
	{
		static void Main(string[] args)
		{
			int[] n = new int[10];
			int i;

			for (i = 0; i < 10; i++)
			{
				n[i] = i + 777;
				Console.WriteLine(i, n[i]);
			}
		}
	}
}

// foreach循环遍历数组
using System;

namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n 是一个带有 10 个整数的数组 */


         /* 初始化数组 n 中的元素 */         
         for ( int i = 0; i < 10; i++ )
         {
            n[i] = i + 100;
         }

         /* 输出每个数组元素的值 */
         foreach (int j in n )
         {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
         }
         Console.ReadKey();
      }
   }
}

// C# 字符串
using System;
namespace StringApplication
{
	class Program
	{
		static void Main(sring[] args)
		{
			// 字符串,字符串连接
			string fname, lname;
			fname = "xiao"
			lname = "car"

			string fullname = fname + lname;
			Console.WriteLine("Full name:{0}", fullname);

			// 通过使用string构造函数
			char[] letters = { 'H', 'e', 'l', 'l','o' };
			string greetings = new string(letters);
			Console.WriteLine("greetings:{0}", greetings);

			// 方法返回字符串
			string[] sarray = { "Hello", "From", "Tutorials", "Point" };
			string message = String.Join(" ", sarray);
			Console.WriteLine(message);

			// 用于转化值的格式化方法
			DateTime waiting = new DateTime(2019, 05, 16, 14, 50, 09);
			string chat = String.Format("message sent at {0:t} on {0:D}", waiting);
			Console.WriteLine("message:{0}", chat);

			// 判断字符串是否相同
			string str1 = "this is test";
			string str2 = "this is text";

			if (string.Compare(str1, str2) == 0)
			{
				Console.WriteLine(str1 + "and" + str2 + "are equal.");
			}
			else
			{
				Console.WriteLine(str1 + "and" + str2 + "are not equal");
			}

			// 判断字符串包含
			if (str.Contains("test"))
			{
				Console.WriteLine("The sequence 'test' was found");
			}

			// 字符串截取 Substring(x) 从第x位开始往后
			string substr = str.Substring(4);
			Console.WriteLine(substr);
		}
	}
}

// C# 结构体
using System;
using System.Text;

struct Books
{
	public string title;
	public string author;
	public string subject;
	public int book_id;
}

public class testStructure
{
	public static void Main(string[] args)
	{
	    Books Book1;        /* 声明 Book1,类型为 Book */
	    Books Book2;        /* 声明 Book2,类型为 Book */

	    Book1.title = "python从入门到放弃";
	    Book1.author = "xiaohuoche";
	    Book1.subject = "python";
	    Book1.book_id = 1;

	   	Book1.title = "PHP从入门到放弃";
	    Book1.author = "xiaohuoche";
	    Book1.subject = "PHP";
	    Book1.book_id = 2;
	      /* 打印 Book1 信息 */
		Console.WriteLine( "Book 1 title : {0}", Book1.title);
		Console.WriteLine("Book 1 author : {0}", Book1.author);
		Console.WriteLine("Book 1 subject : {0}", Book1.subject);
		Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);

		      /* 打印 Book2 信息 */
		Console.WriteLine("Book 2 title : {0}", Book2.title);
		Console.WriteLine("Book 2 author : {0}", Book2.author);
		Console.WriteLine("Book 2 subject : {0}", Book2.subject);
		Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);  
	}
}

// 结构中不能实例属性或字段初始值设定
using System;
using System.text;

struct Books
{
	private string title;
	private string author;
	private string subject;
	private int book_id;
	public void GetValues(string t, string a, string s, int id)
	{
		title = t;
		author = a;
		subject = s;
		book_id = id;
	}
	public void Display()
	{
		Console.WriteLine("Title : {0}", title);
		Console.WriteLine("Author : {0}", author);
		Console.WriteLine("Subject : {0}", subject);
		Console.WriteLine("Book_id :{0}", book_id);
	}
}

public class testStructure
{
	public static void Main(string[] args)
	{
		Books Book1 = new Books();
		Books Book2 = new Books();

		Book1.GetValues("111", "222", "333", 777);
		Book2.GetValues("111", "222", "333", 777);

		Book1.Display();
		Book2.Display();
	}
}

// C#枚举
// 枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。C# 枚举是值类型。换句话说,枚举包含自己的值,且不能继承或传递继承。
using System;

public class EnumTest
{
	// 创建枚举
	enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
	// 枚举值默认0,1,2,3... 可自定义
	// 	enum Day { Sun = 11, Mon = 22, Tue = 33 , Wed = 55, Thu = 66, Fri = 77, Sat = 22};

	static void Main()
	{
		int x = (int)Day.Sun;
		int y = (int)Day.Fri;
		Console.WriteLine("Sun = {0}", x);
		Console.WriteLine("Fri = {0}", y);
	}
}

// C#类
/*
 class class_name
{
	// member variables
	  variable;
	...
	
	// member methods
	  method(parameter_list)
	{
		// method body
	} 
	...
}
*/

using System;
namespace BoxApplication
{
	class Box
	{
		public double length;
		public double width;
		public double height;
	}
	class Boxtester
	{
		static void Main(string[] args)
		{
			Box Box1 = new Box();
			Box Box2 = new Box();
			double volume = 0.0;

			// Box1 详述
			Box1.height = 5.0;
			Box1.length = 6.0;
			Box1.width = 7.0;

			// Box2 详述
			Box2.height = 10.0;
			Box2.length = 12.0;
			Box2.width = 13.0;

			// Box1 的体积
			volume = Box1.height * Box1.length * Box1.width;
			Console.WriteLine(volume);

			// Box2 的体积
			volume = Box2.height * Box2.length * Box2.width;
			Console.WriteLine(volume);
		}
	}
}

// 成员函数和封装
using System;
namespace BoxApplication
{
	class Box
	{
		private double length;
		private double width;
		private double height;
		public void SetValue(double l, double w, double h)
		{
			length = l;
			width = w;
			height = h;
		}
		public void GetVolume()
		{
			return length * width * height;
		}
	}

	class Boxtester
	{
		static void Main(string[] args)
		{
			Box Box1 = new Box();
			Box Box2 = new Box();
			double volume;

			Box1.SetValue(1, 3, 5);
			Box2.SetValue(5, 7, 9);

			volume = Box1.GetVolume();
			Console.WriteLine(volume);
			volume = Box2.GetVolume();
			Console.WriteLine(volume);
		}
	}
}

// 构造函数 析构函数
using System;
namespace LineApplication
{
	class Line
	{
		private double length;
		public line() // 构造函数
		{
			Console.WriteLine("对象已创建");
		}
		~Line() // 析构函数
		{
			Console.WriteLine("对象已删除");
		}

		public void setLength(double len)
		{
			length = len;
		}
		public double getLength()
		{
			return length;
		}

		static void Main(string[] args)
		{
			Line line = new Line();
			// 设置线条长度
			line.setLength(6.0);
			Console.WriteLine(line.getLength());
		}
	}
}

// C# 继承
/*
 class 
{
	...
}
class  : 
{
	...
}
*/
using System;
namespace InheritanceApplication
{
	class Shape
	{
		public int width;
		public int height;
		public void SetValue(int w, int h)
		{
			width = w;
			height = h;
		}
	}

	// 派生类
	class Rectangle : Shape
	{
		public int getArea()
		{
			return (width * height);
		}
	}

	class RectangleTester
	{
		static void Main(string[] args)
		{
			Rectangle Rect = new Rectangle();
			Rect.SetValue(5, 7);
			Console.WriteLine(Rect.getArea());
		}
	}
}

// 多重继承
using System;
namespace InheritanceApplication
{
	class Shape
	{
		public void SetValue(int w, int h)
		{
			width = w;
			height = h;
		}
		protected int width;
		protected int height;
	}

	// 基类PaintCost
	public interface PaintCost
	{
		int getCost(int area);
	}

	// 派生类
	class Rectangle : Shape, PaintCost
	{
		public int getArea()
		{
			return (width * height);
		}
		public int getCost(int area)
		{
			return area * 70;
		}
	}

	class RectangleTester
	{
		static void Main(string[] args)
		{
			Rectangle Rect = new Rectangle();
			int area;
			Rect.SetValue(5, 7);
			Console.WriteLine(Rect.getArea());
			Console.WriteLine(Rect.getCost(Rect.getArea()));
		}
	}
}

// C# 多态性
// 函数重载
using System;
namespace PolymorphismApplication
{
	class Prindata
	{
		void print(int i)
		{
			Console.WriteLine(i);
		}

		void print(double f)
		{
			Console.WriteLine(f);
		}

		void print(string s)
		{
			Console.WriteLine(s);
		}
		static void Main(string[] args)
		{
			Prindata p = new Prindata();
			// 调用 print 来打印整数
			p.print(5);
			// 调用 print 来打印浮点数
			p.print(500.263);
			// 调用 print 来打印字符串
			p.print("Hello C++");
		}
	}
}

// 动态多态性
using System;
namespace PolymorphismApplication
{
	abstract class Shape
	{
		abstract public int area();
	}
	class Rectangle : Shape
	{
		private int length;
		private int width;
		public Rectangle(int a = 0, int b = 0)
		{
			length = a;
			width = b;
		}
		public override int area()
		{
			Console.WriteLine("Rectangle 类的面积");
			return (width * length);
		}
	}

	class RectangleTester
	{
		static void Main(string[] args)
		{
			Rectangle r = new Rectangle(10, 7)
			double a = r.area();
			Console.WriteLine("面积:{0}", a);
		}
	}
}

// C# 运算符重载
using System;
namespace OperatorOvlApplication
{
	class Box
	{
		private double length;
		private double width;
		private double height;

		public double GetVolume()
		{
			return length * width * height
		}
		public void SetValue()
		{
			length = l;
			width = w;
			height = h;
		}
		public static Box operator+ (Box a, Box b)
		{
			Box box = new Box();
			box.length = a.length + b.length;
			box.width = a.width + b.width;
			box.height = a.height + b.height;
			return box;
		}
	}

	class Tester
	{
		static void Main(string[] args)
		{
			Box Box1 = new Box();
			Box Box2 = new Box();
			Box Box3 = new Box();
			double volume = 0.0;

			Box1.SetValue(5, 6, 7);
			Box2.SetValue(7, 8, 9);

			volume = Box1.GetVolume();
			Console.WriteLine("Box1的体积:{0}", volume);

			volume = Box2.GetVolume();
			Console.WriteLine("Box1的体积:{0}", volume);

			// 把两个对象相加
			Box3 = Box1 + Box2;

			// Box3 的体积
			volume = Box3.GetVolume();
			Console.WriteLine("Box3的体积:{0}", volume);
		}
	}
}

// C# 接口
using System;
interface IMyInterface
{
	// 接口成员
	void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
	static void Main()
	{
		InterfaceImplementer iImp = new InterfaceImplementer();
		iImp.MethodToImplement();
	}

	public void MethodToImplement()
	{
		Console.WriteLine("MethodToImplement() called.")
	}
}

// C# 命名空间
/*
namespace namespace_name
{
	//代码声明
}
*/
using System;
namespace first_space
{
	class namespace_cl
	{
		public void func()
		{
			Console.WriteLine("Inside first_space");
		}
	}
}
namespace second_space
{
	class namespace_cl
	{
		public void func()
		{
			Console.WriteLine("Inside second_space");
		}
	}
}

class TestClass
{
	static void Main(string[] args)
	{
		first_space.namespace_cl fc = new first_space.namespace_cl();
		second_space.namespace_cl sc = new second_space.namespace_cl();
		fc.func();
		sc.func();
	}
}

// 嵌套命名空间
using System;
using SomeNameSpace;
using SomeNameSpace.Nested;
namespace SomeNameSpace
{
	public class MyClass
	{
		static void Main()
		{
			Console.WriteLine("In SomeNameSpace");
			Nested.NestedNameSpaceClass.SayHello();
		}
	}

	// 内嵌命名空间
	namespace Nested
	{
		public class NestedNameSpaceClass
		{
			public static void SayHello()
			{
				Console.WriteLine("In Nested");
			}
		}
	}
}

// C# 预处理器指令
#define DEBUG
#define VC_V10
using System;
public class TestClass
{
	public static void Main()
	{
		# if (DEBUG && !VC_V10)
			Console.WriteLine("DEBUG is define");
		# elif (!DEBUG && VC_V10)
			Console.WriteLine("VC_V10 is define");
		# elif (DEBUG && VC_V10)
			Console.WriteLine("DEBUG and VC_V10 are defined");
		# else
			Console.WriteLine("DEBUG and VC_V10 are not defined");
		# endif
	}
}

// C# 正则表达式
using System;
using System.Text.RegularExpressions;
public class Example
{
	public static void Main()
	{
		string input = "1995 1996 1855 0156 1970";
		string pattern = @"(?<=19)\d{2}\b";

		foreach(Match match in Regex.Matches(input, pattern))
			Console.WriteLine(match.Value);
	}
}

// C# 异常处理
/*
try
{
	// 测试语句
}
catch(ExceptionName e1)
{
	// 错误处理代码
}
catch(ExceptionName e2)
{
	// 错误处理代码
}
finally
{
	// 要执行的语句
}
*/

// 异常处理
using System;
namespace ErrorHandlingApplication
{
	class DivNumbers
	{
		int result;
		DivNumbers()
		{
			result = 0;
		}
		public void division(int num1, int num2)
		{
			try
			{
				result = num1 / num2;
			}
			catch (DivideByZeroException e)
			{
				Console.WriteLine("Exception caugt: {0}", e);
			}
			finally
			{
				Console.WriteLine("Result: {0}",result);
			}
		}
		static void Main(string[] args)
		{
			DivNumbers d = new DivNumbers();
			d.division(25, 0);
			d.division(25, 2);
		}
	}
}

 

你可能感兴趣的:(C#)