Sady Home

Note my coding life

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
写了快一年的ASP.NET程序,只知实现,却不知道其原理和真正意义,惭愧
最近为了让自己“死”得明白,特地系统的学了一遍C#,受益匪浅
现将几个关键概念的资料收集整理出来,以备日后查阅加固:

委托
委托是一种类型,可以把引用存储为函数,像函数的指针
例如:
using System;
using System.Text;

namespace CSharpLearning.Delegating.TestSimple
{
  
public delegate string TestDelegate(string s1,string s2); //delegate

  
public class Test
  
{
    
public string UnionString(string s1,string s2){return s1 + s2;} //method
  }


  
class Program
  
{
    
static void Main(string[] args)
    
{
      Test t 
= new Test();
      TestDelegate td 
= new TestDelegate(t.UnionString);
      Console.WriteLine(td(
"Hello"," World!"));
    }

  }

}
注意:其返回的类型必须和委托的类型一致,且其签名与委托的签名一致(即参数列表)
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpLearning.Delegating.TestGeneric
{
  
public delegate string TestDelegate<T,S>(T s1, S s2);

  
public class Test
  
{
    
public string UnionString(string s1, string s2){return s1 + s2;}
  }


  
class Program
  
{
    
static void Main(string[] args)
    
{
      Test t 
= new Test();
      TestDelegate
<string,string> td = new TestDelegate<string,string>(t.UnionString);
      Console.WriteLine(td(
"Hello"," World!"));
    }

  }

}
泛型委托,使得应用更灵活。

事件
using System;
using System.Text;

namespace CSharpLearning.Delegating.TestEvent
{
    
public delegate void ProDelegate(object sender, EventArgs e);
    
public class Test
    
{
        
private string s1;
        
private string s2;
        
public string S1
        
{
            
get return s1; }
            
set { s1 = value; }
        }

        
public string S2
        
{
            
get return s2;}
            
set { s2 = value; }
        }


        
public event ProDelegate ProcessEvent;
        
void Test_ProcessEvent(object sender, EventArgs e)
        
{
            
throw new Exception("The method or operation is not implemented.");
        }


        
void ProcessAction(object sender, EventArgs e)
        
{
            
if (ProcessEvent == null) ProcessEvent += new ProDelegate(Test_ProcessEvent);
            ProcessEvent(sender, e);
        }

 
        
public string Process()
        
{
            ProcessAction(
this, EventArgs.Empty);
            
return s1 + s2;
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Test t 
= new Test();
            t.ProcessEvent 
+= new ProDelegate(Test_ProcessEvent);
            Console.WriteLine(t.Process());
            Console.ReadKey();
        }


        
static void Test_ProcessEvent(object sender, EventArgs e)
        
{
            Test t 
= (Test)sender;
            t.S1 
= "Hello";
            t.S2 
= " world";
        }

    }

}
using System;
using System.Text;

namespace CSharpLearning.Delegating.TestDelegate
{
    
public class Heater
    
{
        
public delegate void BoilHandler(int param);
        
public event BoilHandler BoilEvent;
        
public void BoilWater()
        
{
            
for (int i = 0; i <= 100; i++)
            
{
                  
if (BoilEvent != null) BoilEvent(i);
            }

        }

    }


    
public class Alarm
    
{
        
public void MakeAlert(int param)
        
{
            
if (param > 95)
                Console.WriteLine(
"Alarm: The temperature is {0}.", param);
        }

    }


    
public class Display
    
{
        
public static void ShowMsg(int param)
        
{
            Console.WriteLine(
"The temperature is {0}.", param);
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Heater h 
= new Heater();
            Alarm a 
= new Alarm();

            h.BoilEvent 
+= a.MakeAlert;
            h.BoilEvent 
+= Display.ShowMsg;

            h.BoilWater();
            Console.ReadKey();
        }

    }

}
(都是从网上看到的好例子) 还需继续理解。

泛型

函数回调???
using System;
using System.Text;

namespace CSharpLearning.Delegating.TestDelegate
{
    
public delegate string ProDelegate(string s1, string s2);
    
public class Test
    

        
public string Process(string s1,string s2, ProDelegate process)
        
{
            
return process(s1,s2);
        }


        
public string UnionString(string s1, string s2)return s1 + s2;}
    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Test t 
= new Test();
            Console.WriteLine(t.Process(
"Hello"," world.",new ProDelegate(t.UnionString)));
            Console.ReadKey();
        }

    }

}


反射

接口
posted on 2008-01-09 10:07  Sady  阅读(362)  评论(0编辑  收藏  举报
凭飞堂