本文目录
如何充分利用C#匿名方法的平台优势
在C# 里 声明和使用委托要求你有委托和一个在委托被触发时具有匹配签名的能够执行的方法 以及一个将命名方法与委托关联的分配语句 作为C# 的新特性 匿名方法基本上能够提供与先前命名方法相同的功能 但是它已经不再需要一个在关联到委托之前就明确创建的方法了
你可以把匿名方法想象为一个实现与委托进行关联这项功能的便捷途径 如果同时看一下匿名方法实现和命名方法实现所取得IL结果 你会发现这两者之间的差别非常小 当编译器碰到匿名方法的时候 它会在类里面创建一个命名方法 并将它与委托进行关联 所以匿名方法在运行期间与命名方法的性能非常类似——性能的增加体现在开发人员的生产效率上 而不是运行期间的执行上
如何使用匿名方法
多用代码少用说教来解释和理解匿名方法会容易一些 下面的例子应该能够说明你自己可以如何利用匿名方法的优势
例 ——基础知识
使用匿名方法很简单 你只需要把匿名方法放在你通常放命名方法的关联语句里 在下面这个例子里 我把匿名方法和 示例 的委托关联在一起
示例列表
#region Simple example Example privatedelegatevoidExample (); privatevoid btnExample _Click(object sender EventArgs e) { //Declare an instance of the Example delegate // You can see where I m using the anonymous // method in place of a named method it follows // the delegate() keyword Example example = newExample ( delegate() { MessageBox Show( Example ); }); example(); } #endregion
例 ——变量范围
任何在匿名方法里声明的变量的范围都不会超出匿名方法的代码块 但是 匿名方法确实具有访问它们代码块之外的变量的能力 只要这些变量在匿名方法所使用的范围里 这些变量被微软称为外部变量 下面示 例 显示了匿名方法如何引用外部变量
示例列表
#region Variable scope example Example privatedelegatevoidExample (); privatevoid btnExample _Click(object sender EventArgs e) { //Setup our parameters string firstName = Zach ; string lastName = Smith ; //Create an instance of the Example delegate with an // anonymous method Example example = newExample ( delegate() { MessageBox Show(firstName + + lastName); }); //Execute the delegate example(); } #endregion
要注意的是 根据MSDN的文档 匿名方法里的 ref 和 out 参数无法被访问到
例 ——参数的传递
你可以将参数传递给匿名方法 方式就和你处理引用命名方法参数的委托一样 下面的 示例 说明这种类型的功能
示例列表
#region Parameter example Example privatedelegatevoidExample (string firstName string lastName); privatevoid btnExample _Click(object sender EventArgs e) { //Setup our parameters string parameter = Zach ; string parameter = Smith ; //Create an instance of the Example delegate with an // anonymous method Example example = newExample ( delegate(string firstName string lastName) { MessageBox Show( Example : + firstName + + lastName); }); //Execute the delegate example(parameter parameter ); } #endregion
例 ——多个方法的关联
就和命名方法一样 将多个匿名方法与同一个委托进行关联是可能的 这在很多情况下会非常有用——首先想到的是把一个简单的处理程序添加给按钮的点击事件 下面的代码( 示例 )显示了一个委托 它同时带有与之相关联一个匿名方法和一个命名方法
示例列表
#region Multiple method association (stacking) Example privatedelegatevoidExample (string firstName string lastName); privatevoid btnExample _Click(object sender EventArgs e) { //Setup our parameters string parameter = Zach ; string parameter = Smith ; //Create an instance of the Example delegate with an // anonymous method Example example = newExample ( delegate(string firstName string lastName) { MessageBox Show( Example : + firstName + + lastName); }); //Add another method to the delegate this time // a named method example += newExample (Example NamedMethod); //Execute the delegate example(parameter parameter ); } privatevoid Example NamedMethod(string firstName string lastName) { MessageBox Show( Example Method: + firstName + + lastName); } #endregion
例 ——将匿名方法作为参数传递
就和命名方法一样 将匿名方法作为参数传递给函数是可能的 这并不是一个我认为会通常使用的特性 但是我敢肯定未来会有这种需要 下面的代码( 例 )说明了这种类型的功能 它将一个命名方法作为参数传递给了函数
示例列表
#region Passing anonymous methods Example privatedelegatevoidExample (string firstName string lastName); privatevoid btnExample _Click(object sender EventArgs e) { //Execute Passit and pass the anonymous method Passit((Example )delegate(string firstName string lastName) { MessageBox Show( Example : + firstName + + lastName); }); //Execute Passit with the named method Passit(Example NamedMethod); } privatevoid Example NamedMethod(string firstName string lastName) { MessageBox Show( Example Method: + firstName + + lastName); } privatevoid Passit(Example example) { example( Zach Smith ); } #endregion
例 —-访问类成员
这是对上面 例 的变量范围的扩展 但是 这个例子( 例 )说明了匿名参数还能够在它们的代码块之外执行命名方法
示例列表
#region Accessing class members Example privatedelegatevoidExample (); privateint _customerId; privatestring _customerCode; publicint CustomerID { get { return _customerId; } set { _customerId = value; } } publicstring CustomerCode { get { return _customerCode; } set { _customerCode = value; } } privatevoid btnExample _Click(object sender EventArgs e) { //Populate out properties this CustomerID = ; this CustomerCode = HK ; //Setup the delegate/anonymous method Example example = newExample ( delegate { this ShowCustomer(this CustomerID this CustomerCode); }); //Execute the delegate example(); //Change the properties this CustomerID = ; this CustomerCode = L M ; //Execute the delegate again // Notice that the new values are reflected example(); } privatevoid ShowCustomer(int customerId string customerCode) { MessageBox Show( String Format( CustomerID: Customer Code: customerId customerCode)); } #endregion
要注意的是 我两次调用了与匿名方法相关联的委托 你可能会发现一个很有趣的事情 在这些调用中 方法会输出两组不同的值 这是因为用在匿名方法里的外部变量在创建匿名方法的时候被引用 这意味着对这些变量的任何更改都会在匿名函数访问变量的时候被反映出来
你可能还注意到在这个实例里委托关键字后面没有括号 当匿名方法不需要带参数的时候 后面的括号是可选的
评论
我希望本文已经说明如何使用匿名方法 虽然它们还不是革命性的 但是它们是C#演化成为一门程序员友好的语言过程中的一个重要步骤
lishixinzhi/Article/program/net/201311/13844
ENVI不好用layer stacking 求好心人帮忙解决 万分感谢
你这个是在运行过程中出错了吗?那很大的可能性是,你的硬盘满了,合文件的时候你这4个文件生成的文件大小至少在4倍以上,只会大不会小,且你必须保证这4个文件投影等信息一致。
C#和IDL二次开发的时候,我想传递一个简单的一维数组,但结果总是不是我想要的,关键代码如下:
数组在C#不能直接用+号连接,你可以考虑一下将数组拼接一下,也就是string IdlCommand = "layer_stacking,’" + inputfiles + "’,’" + String.Join(",",inputfiles)+ "’";