比较C#中的委托和Ruby中的block对象
2006-05-23 15:37
405 查看
Ruby中,所谓带块(block)的方法是指对控制结构的抽象,最初设计是用于对循环进行抽象,所以又称迭代器。
语法:method_name do...end 或 method_name {}
1#块调用
2def foo
3 print yield(5)
4end
5
6foo {|a|
7 if a > 0
8 "positive"
9 elsif a < 0
"negative"
else
"zero"
end
}
foo()
#使用了proc(过程对象)的块调用
quux = proc {|a|
if a > 0
"positive"
elsif a < 0
"negative"
else
"zero"
end
}
def foo(p)
print p.call(5)
end
foo(quux)
1//传统委托
2public delegate string fooDelegate(int a);
3public string foo(int a)
4fooDelegate myD1 = new fooDelegate(foo);
Console.WriteLine(myD1(5));
//匿名委托 C# 2.0
public delegate string fooDelegate(int a);
fooDelegate myD1 = delegate(int a)
Console.WriteLine(myD1(5));
语法:method_name do...end 或 method_name {}
1#块调用
2def foo
3 print yield(5)
4end
5
6foo {|a|
7 if a > 0
8 "positive"
9 elsif a < 0
"negative"
else
"zero"
end
}
foo()
#使用了proc(过程对象)的块调用
quux = proc {|a|
if a > 0
"positive"
elsif a < 0
"negative"
else
"zero"
end
}
def foo(p)
print p.call(5)
end
foo(quux)
1//传统委托
2public delegate string fooDelegate(int a);
3public string foo(int a)
4fooDelegate myD1 = new fooDelegate(foo);
Console.WriteLine(myD1(5));
//匿名委托 C# 2.0
public delegate string fooDelegate(int a);
fooDelegate myD1 = delegate(int a)
Console.WriteLine(myD1(5));
// Action/Func委托 + lambda表达式 C# 3.0 Action<Func<int, string>> foo = (func)=>{ Console.WriteLine(func(5)); }; Action qoo = () => { foo((a) => { if(a > 0) return "positive"; else if(a < 0) return "negative"; return "zero"; }); }; qoo(); Func<int, string> quux = (a) =>{ if(a > 0) return "positive"; else if(a < 0) return "negative"; return "zero"; }; Action<Func<int, string>> foo = (func)=>{ Console.WriteLine(func(5)); }; foo(quux);
相关文章推荐
- C#中三种定时器对象的比较
- C#基础之数组排序,对象大小比较
- C#中比较两个对象的地址是否相同(也是引用计数的问题,和Java一样)
- C#成员函数直接调用和反射+委托的性能比较
- C#中三种定时器对象的比较
- C#中三种定时器对象的比较 【转】
- C#源代码—声明委托,通过委托对象调用所指向的方法
- C#对象的比较
- C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序
- [Unity&C#&委托事件&未实例化]使用委托事件的时候事件对象未实例化
- Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换
- C#中比较两个对象的地址是否相同
- C#的对象相等比较问题
- C#:请向C++传递全局委托对象以用于回调
- C# 委托的比较 delegate、Action、Func、predicate
- [C#] C#中对象相等性比较方法的区别
- C# 对象比较(值类型、引用类型)
- ruby 对象的比较总结
- C# 构造可比较的对象
- C#中的委托(与Ruby中的类似,方法看作一个类型)