Tuesday 15 March 2011

c# - Temporary method within method? -


In the method below, there are three conditions: I want to replace them with one method and pass it conditionally.

In addition, the conditional body is almost repeated. Is it possible to make a method that is only present within MyMethod () locally? Then the following code is reduced to:

// Less code

  Public class MyMethod (Class 1 class 1Var, class 2 class 2 wise) { Return local method (class1var.SomeBool & amp;; class2var.SomeBool), false, "this is string1"); Local mode (class1var.SomeBool & Class2var.IsMatch (class2var)) is true; return local mode (class1var.SomeProperty.HasValue & amp; class2Var.SomeBool, incorrect, "this string is 2") ; // ... localMethod () is defined here ...}   

But above, only one should be returned.

// The original code < / P>

  Public Classes MyMethod (class 1 class 1 through class 2 class 2) {if (class 1 by SOMBUL & Class 2 veer. SEOBUL) {New class {return 1 = FAP, string 1 = "This string is 1"};} if (class1var.S OmeBool & Class2Var.IsMatch (class2var)} {Return new class {property1 = true;};} (class1var.SomeProperty.HasValue & Class2Var.SomeBool) {New Class {return} {property1 = false, String 1 = "This string is 2"}}}   

Actually, I would like to create a temporary method within a method.

< Div class = "post-text" itemprop = "text">

You can use Func notation for this. Funx enumerate representative methods < Read the documentation for Code> Fenax . There are inputs for the n common parameter with the n-1 method and the last parameter is the return type, try something like this:

  Public Classes MyMethod (Class 1 Class 1 Category, Class 2 Class 2 VR) {Funk & lt; Bool, bool, string, classz & gt; MyFunc = (bottom, prop1Value, string1Value) = & gt; Sure? New class {property 1 = prop 1 well, string 1 = string1 value}: empty; Return my fawn (class1var.SomeBool & Class2var.SomeBool), wrong, "this is string1") ?? MyFunc ((class1var.SomeBool & Class2var.IsMatch (class2var)), true) ?? MyFunc ((class1var.SomeProperty.HasValue & amp; class2Var.SomeBool), incorrect, "This string is 2"); }   

Here, we take Func in a bool (defined), to set parameters (second Bool and string ), and ClassZ .

Also read more about empty collessing (??? notation), which first offers non-null argument.

The second option is to use verbs , which defines the zero methods. Read about Actions . You can instantize a new class , and then call <3> code <3> on 3, which sets the conditionally passed variables.

  public classz on MyMethod class 1 class 1, class 2 class 2) {Action & lt; Balls, Balls, String, Classes & GT; MyAction = (bottom, prop1Value, string1Value, classZInstance) = & gt; If (poorly) {classZIntance.Property1 = prop1Value; ClassZInstance.String1 = string1Value; } Var myClassZ = new classes (); MyAction (class1var.SomeBool & amp; class2Var.SomeBool), incorrect, "this string1", myClassZ) myAction (class1var.SomeBool & Class2var.IsMatch (class2var)), true, classZ) myAction (( Class1var.SomeProperty.HasValue & amp; class2Var.SomeBool), incorrect, "this string is 2", myClassZ); Return myClassZ; }    

No comments:

Post a Comment