//program.cs
using System;
namespace CJ_test{
class Program{
//整体感觉看下来与C++十分类似,独有的特性是托管吧!
static void Main(string[] args){
//Class1 c1 = new Class1();
//c1.func1();
//c1.Acceptdetails();
//c1.Display();
//Console.ReadLine(); //输入一行
//c1.fun2();
//int add1 = c1.Add(1, 2);
//int add2 = c1.Add(1, 2, 3);
//Console.WriteLine("add1 :" + add1);
//Console.WriteLine("add2 :" + add2);
//Class2.Message("hi");
//Class2.OldMethod();
Class3.c3_main();
Class4.c4_main();
Class5.c5_main();
Class6.c6_main();
Class7.c7_main();
}
}
}
//Class1.cs
using System;
namespace CJ_test{
class Class1 {
public void func1(){
Console.WriteLine("Hello World!");
Console.ReadKey(); //按任意键
}
double length;
double width;
public void Acceptdetails(){
length = 4.5;
width = 3.5;
}
public double GetArea(){
return length * width;
}
public void Display(){
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
public void fun2()
{
int i = 75;
float f = 53.005f;
double d = 2345.7652;
bool b = true;
Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b.ToString());
Console.ReadKey();
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
}
//Class2.cs
#define DEBUG
using System;
using System.Diagnostics;
namespace CJ_test{
class Class2{
[Conditional("DEBUG")]
public static void Message(string msg){
Console.WriteLine(msg);
}
[Obsolete("Don't use OldMethod, use NewMethod instead", true)]
public static void OldMethod(){
Console.WriteLine("It is the old method");
}
}
}
//Class3.cs
using System;
delegate int NumberChanger(int n);
namespace CJ_test{
class Class3{
static int num = 10;
public static int AddNum(int p){
num += p;
return num;
}
public static int MultNum(int q){
num *= q;
return num;
}
public static int getNum(){
return num;
}
public static void c3_main(){
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
nc1(25);//nc1受NumberChanger(AddNum)委托,相当于执行AddNum(25)
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
//Class4.cs
using System;
namespace CJ_test{
public class EventTest{
private int value;
public delegate void NumManipulationHandler();
public event NumManipulationHandler ChangeNum;
protected virtual void OnNumChanged(){
if (ChangeNum != null){
ChangeNum(); /* 事件被触发 */
}
else{
Console.WriteLine("event not fire");
Console.ReadKey(); /* 回车继续 */
}
}
public EventTest(){
int n = 5;
SetValue(n);
}
public void SetValue(int n){
if (value != n){
value = n;
OnNumChanged();
}
}
}
/***********订阅器类***********/
public class subscribEvent{
public void printf(){
Console.WriteLine("event fire");
Console.ReadKey(); /* 回车继续 */
}
}
/***********触发***********/
public class Class4{
public static void c4_main(){
EventTest e = new EventTest(); /* 实例化对象,第一次没有触发事件 */
subscribEvent v = new subscribEvent(); /* 实例化对象 */
e.ChangeNum += new EventTest.NumManipulationHandler(v.printf); /* 注册 */
e.SetValue(7);
e.SetValue(11);
}
}
}
//Class5.cs
using System;
namespace CJ_test{
public class Class5{
static void Swap<T>(ref T lhs, ref T rhs){
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
public static void c5_main(){
int a, b;
char c, d;
a = 10;
b = 20;
c = 'I';
d = 'V';
// 在交换之前显示值
Console.WriteLine("Int values before calling swap:");
Console.WriteLine("a = {0}, b = {1}", a, b);
Console.WriteLine("Char values before calling swap:");
Console.WriteLine("c = {0}, d = {1}", c, d);
// 调用 swap
Swap<int>(ref a, ref b);
Swap<char>(ref c, ref d);
// 在交换之后显示值
Console.WriteLine("Int values after calling swap:");
Console.WriteLine("a = {0}, b = {1}", a, b);
Console.WriteLine("Char values after calling swap:");
Console.WriteLine("c = {0}, d = {1}", c, d);
Console.ReadKey();
}
}
}
//Class6.cs
using System;
delegate void NumberChanger2(int n);
namespace CJ_test{
public class Class6{
static int num = 10;
public static void AddNum(int p){
num += p;
Console.WriteLine("Named Method: {0}", num);
}
public static void MultNum(int q){
num *= q;
Console.WriteLine("Named Method: {0}", num);
}
public static void c6_main(){
// 使用匿名方法创建委托实例
NumberChanger2 nc = delegate (int x){
Console.WriteLine("Anonymous Method: {0}", x);
};
// 使用匿名方法调用委托
nc(10);
// 使用命名方法实例化委托
nc = new NumberChanger2(AddNum);
// 使用命名方法调用委托
nc(5);
// 使用另一个命名方法实例化委托
nc = new NumberChanger2(MultNum);
// 使用命名方法调用委托
nc(2);
Console.ReadKey();
}
}
}
//Class7.cs
using System;
using System.Threading;
namespace CJ_test{
class Class7{
public static void CallToChildThread(){
try{
Console.WriteLine("Child thread starts");
// 计数到 10
for (int counter = 0; counter <= 10; counter++){
Thread.Sleep(500);
Console.WriteLine(counter);
}
Console.WriteLine("Child Thread Completed");
}
catch (ThreadAbortException e){
Console.WriteLine("Thread Abort Exception");
}
finally{
Console.WriteLine("Couldn't catch the Thread Exception");
}
}
public static void c7_main(){
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
// 停止主线程一段时间
Thread.Sleep(2000);
// 现在中止子线程
Console.WriteLine("In Main: Aborting the Child thread");
childThread.Abort();
Console.ReadKey();
}
}
}