一、运算符重载(申明方式:public static 返回值类型 operator 运算符)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
OperatorTest OA = new OperatorTest(5);
OperatorTest OB = new OperatorTest(6);
int Add = OA + OB;
int Cheng = OA * OB;
Console.WriteLine(Add + " " + Cheng);
}
}
public class OperatorTest
{
public int Test;
public OperatorTest(int test)
{
this.Test = test;
}
public static int operator + (OperatorTest TA, OperatorTest TB)
{
return TA.Test + TB.Test;
}
public static int operator *(OperatorTest TA, OperatorTest TB)
{
return TA.Test * TB.Test;
}
}
}
二、强类型转换
(1)implicit 隐式转换 (申明方式:public static implicit operator 返回值类型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
OperatorTest OA = new OperatorTest(5);
int a = OA;
Console.WriteLine(a);
int b = 6;
OperatorTest OB = b;
Console.WriteLine(OB.Test);
}
}
public class OperatorTest
{
public int Test;
public OperatorTest(int test)
{
this.Test = test;
}
public static implicit operator int(OperatorTest TA)
{
return TA.Test;
}
public static implicit operator OperatorTest(int test)
{
return new OperatorTest(test);
}
}
}
(2)explicit 显式转换 (申明方式:public static explicit operator 返回值类型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
OperatorTest OA = new OperatorTest(5);
int a = (int)OA;
Console.WriteLine(a);
int b = 6;
OperatorTest OB = (OperatorTest)b;
Console.WriteLine(OB.Test);
}
}
public class OperatorTest
{
public int Test;
public OperatorTest(int test)
{
this.Test = test;
}
public static explicit operator int(OperatorTest TA)
{
return TA.Test;
}
public static explicit operator OperatorTest(int test)
{
return new OperatorTest(test);
}
}
}
三、委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
delegate void Test();
static void Main(string[] args)
{
Test t = new Test(TestShiXian);
t.Invoke();
Test tt = TestShiXian;
tt.Invoke();
}
public static void TestShiXian()
{
Console.WriteLine("Test");
}
}
}
四、事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
tc.Test += new TestClass.TestHandler(tc_Test);
tc.OnTest();
}
static void tc_Test()
{
Console.WriteLine("123");
}
}
public class TestClass
{
public delegate void TestHandler();
public event TestHandler Test;
public void OnTest()
{
if (Test != null)
{
Console.WriteLine("OnTest");
Test();
}
}
}
}