###### 漠漠星的专栏

有水、有草、有星星，日月星辰，天下之大；以技术求生存，以市场求发展；学以至用，开拓创新；达技术之颠峰，至市场之广阔！...

###### C# 2.0学习之--匿名代理

 csc AnonymousDelegates.cs AnonymousDelegates

AnonymousDelegates.cs :

using System;
using System.Collections.Generic;
using System.Text;

namespace AnonymousDelegate_Sample
{

// Define the delegate method.
delegate decimal CalculateBonus(decimal sales);

// Define an Employee type.
class Employee
{
public string name;
public decimal sales;
public decimal bonus;
public CalculateBonus calculation_algorithm;
}

class Program
{

// This class will define two delegates that perform a calculation.
// The first will be a named method, the second an anonymous delegate.

// This is the named method.
// It defines one possible implementation of the Bonus Calculation algorithm.

static decimal CalculateStandardBonus(decimal sales)
{
return sales / 10;
}

static void Main(string[] args)
{

// A value used in the calculation of the bonus.
// Note: This local variable will become a "captured outer variable".
decimal multiplier = 2;

// This delegate is defined as a named method.
CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus);

// This delegate is anonymous - there is no named method.
// It defines an alternative bonus calculation algorithm.
CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; };

// Declare some Employee objects.
Employee[] staff = new Employee[5];

// Populate the array of Employees.
for (int i = 0; i < 5; i++)
staff[i] = new Employee();

staff[0].name = "Mr Apple";
staff[0].sales = 100;
staff[0].calculation_algorithm = standard_bonus;

staff[1].name = "Ms Banana";
staff[1].sales = 200;
staff[1].calculation_algorithm = standard_bonus;

staff[2].name = "Mr Cherry";
staff[2].sales = 300;
staff[2].calculation_algorithm = standard_bonus;

staff[3].name = "Mr Date";
staff[3].sales = 100;
staff[3].calculation_algorithm = enhanced_bonus;

staff[4].name = "Ms Elderberry";
staff[4].sales = 250;
staff[4].calculation_algorithm = enhanced_bonus;

// Calculate bonus for all Employees
foreach (Employee person in staff)
PerformBonusCalculation(person);

// Display the details of all Employees
foreach (Employee person in staff)
DisplayPersonDetails(person);

}

public static void PerformBonusCalculation(Employee person)
{

// This method uses the delegate stored in the person object
// to perform the calculation.
// Note: This method knows about the multiplier local variable, even though
// that variable is outside the scope of this method.
// The multipler varaible is a "captured outer variable".
person.bonus = person.calculation_algorithm(person.sales);
}

public static void DisplayPersonDetails(Employee person)
{
Console.WriteLine(person.name);
Console.WriteLine(person.bonus);
Console.WriteLine("---------------");
}
}
}

2010-09-06 17:14:00

#### 匿名代理的使用小例子

2009年05月30日 22KB 下载

#### 透明，匿名，高匿代理的一些说明

2016-09-28 19:30:40

#### .Net 2.0 新功能：匿名方法（Anonymous Methods）

2007-09-03 15:55:00

#### C#如何绕过代理服务器获得用户真实的IP地址呢？

2015-03-07 16:14:47

#### 一键获取免费真实的匿名代理

2017-11-28 00:00:00

#### 高度匿名代理、普通匿名代理和透明

2013-11-11 08:21:58

#### 用Python搭建匿名代理池

2016-12-18 14:28:08

#### 透明代理、匿名代理、混淆代理、高匿代理有什么区别？

2015-07-30 10:56:55

#### 检测代理IP匿名程度的方法(附代理基本知识)

2011年09月04日 122KB 下载

C# 2.0学习之--匿名代理