Compare delegate in C # With block objects in ruby

Source: Internet
Author: User

In Ruby, the so-called block-based method refers to the abstraction of the control structure. It was initially designed to abstract loops, so it is also called an iterator.
Syntax: method_name do... end or method_name {}

 

1 # Block call
2 Def Foo
3 Print   Yield ( 5 )
4 End
5
6 Foo { | A |
7 If A > 0
8 " Positive "
9 Elsif < 0
10 " Negative "
11 Else
12 " Zero "
13 End
14 }
15
16 Foo ()
17
18 # Use proc (process object) block call
19 Quux = Proc { | A |
20 If A > 0
21 " Positive "
22 Elsif < 0
23 " Negative "
24 Else
25 " Zero "
26 End
27 }
28
29 Def Foo (P)
30 Print P. Call ( 5 )
31 End
32
33 Foo (quux)

 

1 // Traditional Delegation
2 Public   Delegate   String Foodelegate ( Int A );
3 Public   String Foo ( Int A)
4 {
5 If ( >   0 )
6 Return   " Positive " ;
7 Else   If ( <   0 )
8 Return   " Negative " ;
9 Return   " Zero " ;
10 }
11 Foodelegate myd1 =   New Foodelegate (FOO );
12 Console. writeline (myd1 ( 5 ));
13
14 // Anonymous Delegation
15 Public   Delegate   String Foodelegate ( Int A );
16 Foodelegate myd1 =   Delegate ( Int A)
17 {
18 If ( >   0 )
19 Return   " Positive " ;
20 Else   If ( <   0 )
21 Return   " Negative " ;
22 Return   " Zero " ;
23 }
24 Console. writeline (myd1 ( 5 ));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.