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

推荐订阅源

V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Vercel News
Vercel News
C
Check Point Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
P
Proofpoint News Feed
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
IT之家
IT之家
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
Y
Y Combinator Blog
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
T
Tenable Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
A
Arctic Wolf
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
雷峰网
雷峰网
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence

博客园 - 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();
        }

    }

}

反射

接口