C # tips for using anonymous delegation and anonymous methods

Source: Internet
Author: User

Preface: anonymous delegation refers to an anonymous method or Lambda expression.

A friend gave me a question last night:

In the following code, how can I output "Hello World"

1 If (--- enter the code ---)
2 {
3 console. Write ("hello ");
4}
5 else
6 {
7 console. Write ("world ");
8}

Of course, he said that it can also be done in Java, but it can only be done in one sentence.

I prefer. net, so I tried C # first.

First, we instinctively think that only one if-else statement block can be executed (except for using a thread in a specific situation or using "true") else if (^ !) ^ !" ). Therefore, we naturally think that the hello output must be made when the if condition is executed. So I thought of the anonymous method (NOTE: Refer to the http://baike.baidu.com/view/2308725.htm (anonymous method Baidu encyclopedia) http://baike.baidu.com/view/2761370.htm (anonymous commissioned Baidu encyclopedia) http://msdn.microsoft.com/zh-cn/library/0yw3tz5k.aspx (anonymous method (C # programming guide ))).

So I wrote the following code:

1 using system;
2
3 namespace helloworldoutputtest1
4 {
5 // define Delegation
6 Public Delegate bool mydelfortest1 ();
7
8 public class test1
9 {
10 private mydelfortest1 _ test1; // declare the delegate variable
11
12 public static void main (string [] ARGs)
13 {
14 // You can also write the following code:
15 // If (New test1 () {_ test1 = delegate () {console. Write ("hello"); Return false ;}}. _ test1 ())
16 if (New test1 () {_ test1 = new mydelfortest1 (delegate () {console. Write ("hello"); Return false ;})}. _ test1 ())
17 {
18 console. Write ("hello ");
19}
20 else
21 {
22 console. Write ("world ");
23}
24}
25}
26}

Friends who are familiar with delegation must be familiar with this writing method. This writing method is correct and implemented using the anonymous method, but there is still no solution in one sentence !?

Later, the friend told me his statement:

 1 using System;
2
3 namespace HelloWorldOutPutTest2
4 {
5 public class Test2
6 {
7 public static void Main(string[] args)
8 {
9 if (new Func<bool>(() => { Console.Write("Hello "); return false; }).Invoke())
10 {
11 Console.Write("Hello ");
12 }
13 else
14 {
15 Console.Write("World");
16 }
17 }
18 }
19 }

This is the final solution. Use the gorgeous code (NOTE: Refer to http://technet.microsoft.com/zh-cn/magazine/bb534960 (vs.90 ). aspx (func <(of <(tresult>) Commission) http://technet.microsoft.com/zh-cn/magazine/bb397687 (vs.90 ). aspx (lambda expressions (C # programming guide ))).

C # How about Java?

The teacher gave us a solution:

 1 package HelloWorldOutPutTest3;
2
3 public class Test3 {
4
5 public static void main(String[] args) {
6 if(new Test3(){ public boolean test() { System.out.print("Hello ");return false;} }.test()) {
7 System.out.print("Hello ");
8 }
9 else {
10 System.out.println("world");
11 }
12 }
13 }

It seems that the syntax difference between C # and Java is quite large ......

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.