惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - Sady

Start my new level SCM Chart How to read the contents of a remote web page rs.open syntax Deal with replacing a string in an NTEXT field Get random records How to debug a vb dll for asp SQL Value Example ASP Class javascript injection attack - Sady 封装ASP Version 3 of OA System The relation chart of Supply Chain Management Version 2 of our oa system Supply Chain Management on Website Away for so long Self-promotion 类的继承 DTD
CSharp中几个关键概念
Sady · 2008-01-09 · via 博客园 - Sady

写了快一年的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();
        }

    }

}

反射

接口