1.区分类、接口、类的引用、接口的引用
static void Main(string[] args)
{
#region 2
//对象qqq
myclass qqq = new myclass();
Console.WriteLine(qqq.GetHashCode().ToString());
Console.WriteLine("*****************");
//接口myInterface的引用
myInterface ia = qqq;
Console.WriteLine(ia.GetHashCode().ToString());
Console.WriteLine(ia.a);
Console.WriteLine(ia.b);
Console.WriteLine("*****************");
//接口myInterface2的引用
myInterface2 ia2 = qqq;
Console.WriteLine(ia2.GetHashCode().ToString());
Console.WriteLine(ia2.c);
Console.WriteLine(ia2.d);
Console.WriteLine("*****************");
//引用qqq
myclass aaa = qqq;
Console.WriteLine(aaa.GetHashCode().ToString());
Console.WriteLine("*****************");
Console.WriteLine(qqq.Equals(aaa).ToString());
Console.WriteLine(qqq.Equals(ia).ToString());
Console.WriteLine(qqq.Equals(ia2).ToString());
Console.Read();
#endregion
}
delegate long mydelegate(int q, int b);
//AsyncResult
interface myInterface
{
int a { get; set; }
int b { get; set; }
}
interface myInterface2
{
int c { get; set; }
int d { get; set; }
}
class myclass:myInterface,myInterface2
{
public myclass()
{
a = 10;
b = 11;
c = 12;
d = 13;
}
public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d {
运行结果:
2.异步编程
protected static long Sum(int x, int y)
{
Thread.Sleep(1000);
Console.WriteLine("Inside sum");
return (x + y);
}
delegate long mydelegate(int q, int b);
static void Main(string[] args)
{
#region 1
mydelegate myDel = Sum;
Console.WriteLine("before ");
IAsyncResult iar = myDel.BeginInvoke(3, 5, mycallback, "haha");
Console.WriteLine("After ");
Console.WriteLine("Do");
Console.ReadLine();
#endregion
}
static void mycallback(IAsyncResult iar)
{
AsyncResult ar = (AsyncResult)iar;
mydelegate newdelegate = (mydelegate)ar.AsyncDelegate; //获取委托对象
long temp = newdelegate.EndInvoke(iar);
string strtemp = iar.AsyncState as string;
Console.WriteLine("{0}",temp);
Console.WriteLine(strtemp);
}
AsyncResult实现了IA sync Result接口, AsyncResult和AsyncState分别保存了委托对象和BeginInvoke 方法调用的最后一个参数对象
运行结果: