首页 >> 退房须知

如何充分利用匿名方法的平台优势的

2021-09-06 来源:开平租房网

例3——参数的传递

你可以将参数传递给匿名方法,方式就和你处理引用命名方法参数的委托一样。下面的示例3说明这种类型的功能:

示例列表3

#region Parameter example - Example3

privatedelegatevoidExample3(string firstName, string lastName);

privatevoid btnExample3_Click(object sender, EventArgs e)

{

//Setup our parameters.

string parameter1 = \"Zach\";

string parameter2 = \"Smith\";

//Create an instance of the Example3 delegate with an

// anonymous method.

Example3 example =

newExample3(

delegate(string firstName, string lastName)

{

ow(\"Example3: \" + firstName + \" \" + lastName);

});

//Execute the delegate.

example(parameter1, parameter2);

}

#endregion

例4——多个方法的关联

就和命名方法一样,将多个匿名方法与同一个委托进行关联是可能的。这在很多情况下会非常有用——首先想到的是把一个简单的处理程序添加给按钮的点击事件。下面的代码(示例4)显示了一个委托,它同时带有与之相关联一个匿名方法和一个命名方法:

示例列表4

#region Multiple method association (stacking) - Example4

privatedelegatevoidExample4(string firstName, string lastName);

privatevoid btnExample4_Click(object sender, EventArgs e)

{

//Setup our parameters.

string parameter1 = \"Zach\";

string parameter2 = \"Smith\";

//Create an instance of the Example4 delegate with an

// anonymous method.

Example4 example =

newExample4(

delegate(string firstName, string lastName)

{

ow(\"Example4: \" + firstName + \" \" + lastName);

});

//Add another method to the delegate - this time

// a named method.

example += newExample4(Example4NamedMethod);

//Execute the delegate.

example(parameter1, parameter2);

}

privatevoid Example4NamedMethod(string firstName, string lastName)

{

ow(\"Example4Method: \" + firstName + \" \" + lastName);

}

#endregion

例5——将匿名方法作为参数传递

就和命名方法一样,将匿名方法作为参数传递给函数是可能的。这并不是一个我认为会通常使用的特性,但是我敢肯定未来会有这种需要。下面的代码(例5)说明了这种类型的功能,它将一个命名方法作为参数传递给了函数:

示例列表5

#region Passing anonymous methods - Example5

privatedelegatevoidExample5(string firstName, string lastName);

privatevoid btnExample5_Click(object sender, EventArgs e)

{

//Execute Passit and pass the anonymous method.

操作人员可以快速地调整燃烧过程 Passit((Example5)delegate(string firstName, string lastName)

{

ow(\"Example5: \" + firstName + \" \" + lastName);

});

//Execute Passit with the named method.

Passit(Example5NamedMethod);

}

privatevoid Example5NamedMethod(string firstName, string lastName)

{

ow(\"Example5Method: \" + firstName + \" \" + lastName);

}

privatevoid Passit(Example5 example)

{

example(\"Zach\", \"Smith\");

}

#endregion

例6—-访问类成员

这是对上面例2的变量范围的扩展。但是,这个例子(例6)说明了匿名参数还能够在它们的代码块之外执行命名方法:

示例列表6

#region Accessing class members - Example6

privatedelegatevoidExample6();

privateint _customerId;

privatestring _customerCode;

publicint CustomerID

{

get { return _customerId; }

set { _customerId = value; }

}

publicstring CustomerCode

{

get { return _customerCode; }

set { _customerCode = value; }

}

privatevoid btnExample6_Click(object sender, EventArgs e)

{

//Populate out properties.

stomerID = 90;

stomerCode = \"1337HK\";

//Setup the delegate/anonymous method.

Example6 example =

newExample6(

delegate

{

owCustomer(stomerID, stomerCode);

});

//Execute the delegate.

example();

//Change the properties.

stomerID = 54;

stomerCode = \"L4M3\";

//Execute the delegate again.

// Notice that the new values are reflected.

example();

}

privatevoid ShowCustomer(int customerId, string customerCode)

{

ow(

rmat(\"CustomerID: {0} Customer Code: {1}\",

customerId, customerCode));

}

#endregion

要注意的是,我两次调用了与匿名方法相关联的委托。你可能会发现一个很有趣的事情:在这些调用中,方法会输出两组不同的值。这是因为用在匿名方法里的外部变量在创建匿名方法的时候被引用。这意味着对这些变量的任何更改都会在匿名函数访问变量的时候被反映出来。

你可能还注意到在这个实例里委托关键字后面没有括号。当匿名方法不需要带参数的时候,后面的括号是可选的。

评论

我希望本文已经说明如何使用匿名方法。虽然它们还不是革命性的,但是它们是C#演化成为一门程序员友好的语言过程中的一个重要步骤。

(文/ZachSmith,)

:张琎

查看本文的国际来源

沈阳治疗包皮包茎多少钱
体质虚弱食补怎么补
佛山治疗白癜风的医院