C # Function Encoding Guide

I need to pass values ​​from one class to another and ask me what is the best way. What do you think?

and)

public string getValue(){
   string returnValue;
   ClassA myClass = new ClassA();
   returnValue= myClass.getValue();
   return returnValue;
}

b)

public string getValue(){
   return new ClassA().getValue(); 
}

I don't know if there is a problem with b as a bad programming style ...

Thank!

+3
source share
2 answers

In release mode, the compiler will almost certainly reduce "a" to "b".

There is a case for adding intermediate variables if you most likely want to debug using breakpoints at that location. But IMO "a" adds very little. I would use "b".

, , new ClassA() , . , ClassA.GetValue() .

+5

, ClassA - (, ..), . - ClassA , :

public static ClassA{
  public static string GetValue(){
   //magic happens here
 }
}

:

var myReturnValue = ClassA.GetValue();
+1

All Articles