Answer the question
In order to leave comments, you need to log in
What's the difference: method return?
Is there a difference?
public object GetSomething()
{
var key = ToDoSomething();
return key;
}
public object GetSomething()
{
return ToDoSomething();
}
public Tuple<object,object> GetSomething()
{
return new Tuple<object,object>( SomeDo(), SOmeDoing);
}
public object GetSomething(ref object k)
{
k = SomeDo();
return SomeDoing();
}
public object GetSomething(out int k)
{
k = SomeDo();
return SomeDoing();
}
Answer the question
In order to leave comments, you need to log in
On the first question: the compiler will lead everything to the first form, at the MSIL level in both cases there will be a register (variable), so there is no difference when executing, but when writing the first code is more perfect (expandable), between var
and return
you can do something else with key.
According to UPD: completely different pieces of code, a lot of differences, I don’t understand what the question is.
I think that none. Most likely the compiler will optimize everything and bring everything to the second option.
Solely for readability - the name of a variable can emphasize the semantics of the call (nothing can be said in such a simplified form). But for the algorithm there is no difference.
The C# compiler does not optimize the code, what you write is what you get in the end, if it is difficult to pick MSIL, you can check it with decompilers, they restore variables in such methods. Optimization can already be done in the JIT compiler. But still, it is better not to write unnecessary code without the need.
If I'm not confused, ref and out are hints to the compiler, in fact the compiler starts generating code that works with links and nothing more. In terms of the generated code, there will be no difference, but the behavior of the compiler itself will differ in terms of finding errors.
In fact, if you really want to know what, how and why, it is better to read Richter.
I tried to test the code
here is what happened
/*ILCODE
.method private hidebysig static string GetString() cil managed
{
// Размер кода: 11 (0xb)
.maxstack 1
.locals init ([0] string V_0)
IL_0000: nop
IL_0001: ldstr "MyString"
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
} // end of method Program::GetString*/
/* OILCODE
.method private hidebysig static string GetString() cil managed
{
// Размер кода: 6 (0x6)
.maxstack 8
IL_0000: ldstr "MyString"
IL_0005: ret
} // end of method Program::GetString
*/
private static string GetString()
{
return "MyString";
}
/*ILCODE
.method private hidebysig static string GetStringVar() cil managed
{
// Размер кода: 13 (0xd)
.maxstack 1
.locals init ([0] string str,
[1] string V_1)
IL_0000: nop
IL_0001: ldstr "MyString"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: stloc.1
IL_0009: br.s IL_000b
IL_000b: ldloc.1
IL_000c: ret
} // end of method Program::GetStringVar
*/
/*OILCODE
.method private hidebysig static string GetStringVar() cil managed
{
// Размер кода: 8 (0x8)
.maxstack 1
.locals init ([0] string str)
IL_0000: ldstr "MyString"
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ret
} // end of method Program::GetStringVar
*/
private static string GetStringVar()
{
var str = "MyString";
return str;
}
/*ILCODE
.method private hidebysig static object GetObject() cil managed
{
// Размер кода: 11 (0xb)
.maxstack 1
.locals init ([0] object V_0)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.Object::.ctor()
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret
} // end of method Program::GetObject
*/
/*OILCODE
.method private hidebysig static object GetObject() cil managed
{
// Размер кода: 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.Object::.ctor()
IL_0005: ret
} // end of method Program::GetObject
*/
private static object GetObject()
{
return new object();
}
/*ILCODE
.method private hidebysig static object GetObjectVar() cil managed
{
// Размер кода: 13 (0xd)
.maxstack 1
.locals init ([0] object b,
[1] object V_1)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.Object::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: stloc.1
IL_0009: br.s IL_000b
IL_000b: ldloc.1
IL_000c: ret
} // end of method Program::GetObjectVar
*/
/*OILCODE
.method private hidebysig static object GetObjectVar() cil managed
{
// Размер кода: 8 (0x8)
.maxstack 1
.locals init ([0] object b)
IL_0000: newobj instance void [mscorlib]System.Object::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ret
} // end of method Program::GetObjectVar
*/
private static object GetObjectVar()
{
var b = new object();
return b;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question