Learn how to use action and func

Source: Internet
Author: User

Idle in the morning, see action OptimizationCodeSmall StructureArticleAnd it is much more readable and scalable than normal if... else or switch... case.

Here, I will use other people's information to learn about it again.

From the initial programming time, the condition may be used as follows:

View code

Public VoidDosomething (StringStr)
{
If(STR ="A"){
Dosomethingfora (STR );
}
Else If(STR ="B"){
Dosomethingforb (STR );
}
Else If(STR ="C"){
Dosomethingforc (STR );
}
}

However, the most familiar Metropolis chooses to use switch for branch. After all, this is much better than if... else:

View code

  Public   Void Dosomething ( String Str)
{
Switch (STR)
{
Case " A " :
Dosomethingfora (STR );
Break ;
Case " B " :
Dosomethingforb (STR );
Break ;
Case " C " :
Dosomethingforc (STR );
Break ;
Default :
Break ;
}
}

After reading that brother's article, I felt that the code quality improved a lot after I used action to refactor the Code:

View code

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Actionfuncapp. Actions
{
Class Actionmethods
{
Private Static Dictionary < String , Action < String > DIC = New Dictionary < String , Action < String >> ();
/// <Summary>
/// Add methods to the dictionary
/// </Summary>
Public Actionmethods ()
{
Dic. Add ( " A " , Dosomethingfora );
Dic. Add ( " B " , Dosomethingforb );
Dic. Add ( " C " , Dosomethingforc );
Dic. Add ( " D " , Dosomethingford );
}

Public Void Dosomethingfora ( String Str)
{
Console. writeline (STR + " --> Call dosomethingfora " );

}

Public Void Dosomethingforb (String Str)
{
Console. writeline (STR + " --> Call dosomethingforb " );
}

Public Void Dosomethingforc ( String Str)
{
Console. writeline (STR + " --> Call dosomethingforc " );
}

Public Void Dosomethingford ( String Str)
{
Console. writeline (STR + " --> Call dosomethingford " );
}

/// <Summary>
/// If you call this method externally, the input parameter is OK.
/// </Summary>
/// <Param name = "str"> </param>
/// <Param name = "strdo"> </param>
Public Void Dosomething ( String STR, String Strdo)
{
If (DIC. containskey (STR ))
{
Action < String > Action = DIC [STR] As Action < String >;
// Use action to call a method, provided that the called parameter type is the same! Without the return value
Action. Invoke (strdo );
}
}
}
}

When calling, a new actionmethods object is required:

View code

Actionfuncapp. Actions. actionmethods actions =NewActions. actionmethods ();

Actions. dosomething ("B","Bbbb");

Console. Readline ();

You can only call methods without return values using action. If return values exist, you need to use FUNC:

View code

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Actionfuncapp. func
{
Class Funcmethods
{
Private Static Dictionary < String , Func < String , String > DIC = New Dictionary < String , Func < String ,String >> ();

Public Funcmethods ()
{
Dic. Add ( " A " , Dosomethingfora );
Dic. Add ( " B " , Dosomethingforb );
Dic. Add ( " C " , Dosomethingforc );
Dic. Add ( " D " , Dosomethingford );
}

Public String Dosomethingfora ( String Str)
{
Console. writeline (STR + " --> Call dosomethingfora " );
Return " Return for " ;
}

Public String Dosomethingforb ( String Str)
{
Console. writeline (STR + " --> Call dosomethingforb " );
Return " Return for B " ;
}

Public String Dosomethingforc ( String Str)
{
Console. writeline (STR + " --> Call dosomethingforc " );
Return " Return for C " ;
}

Public String Dosomethingford ( String Str)
{
Console. writeline (STR + " --> Call dosomethingford " );
Return " Return for d " ;
}

Public String Dosomething ( String STR, String Strdo)
{
String Strreturn = String . Empty;
If (DIC. containskey (STR ))
{
Func < String , String > Action = DIC [STR] As Func < String , String >;
Strreturn = action. Invoke (strdo );
}

Return Strreturn;
}
}
}

The execution is similar to the action. The instance has a func object:

View code

 
Actionfuncapp. func. funcmethods funcs =NewFunc. funcmethods ();

Console. writeline (funcs. dosomething ("C","CCCC"));

Console. Readline ();

Address: http://www.cnblogs.com/zhaohuayang/archive/2011/11/22/2259522.html

 

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.