Answer the question
In order to leave comments, you need to log in
Difference between get and return (Stupid question)?
Hello, I taught Java, now I switched to c #
, I understand that it's a stupid question, but still
public string Test
{
get { return "большие ботинки"; }
}
public string Test
{
return "большие ботинки"
}
Answer the question
In order to leave comments, you need to log in
Either I lagged behind Sharpe, or the second one is not clear at all.
The first is a property that has only a getter.
Get simply returns the value of the field, while return returns the value that is calculated in the method, i.e. the method has some kind of class logic, and get just shows the value of the variable.
get - a getter (function) that can do something with the data when requested, return will simply return the value
maybe it was meant in option 2?
public string Test2()
{
return "большие ботинки";
}
.method public hidebysig specialname instance string
get_Test() cil managed
{
.maxstack 1
.locals init (
[0] string V_0
)
// [13 17 - 13 18]
IL_0000: nop
// [13 19 - 13 44]
IL_0001: ldstr "большие ботинки"
IL_0006: stloc.0 // V_0
IL_0007: br.s IL_0009
// [13 45 - 13 46]
IL_0009: ldloc.0 // V_0
IL_000a: ret
} // end of method Class1::get_Test
.method public hidebysig instance string
Test2() cil managed
{
.maxstack 1
.locals init (
[0] string V_0
)
// [17 9 - 17 10]
IL_0000: nop
// [18 13 - 18 38]
IL_0001: ldstr "большие ботинки"
IL_0006: stloc.0 // V_0
IL_0007: br.s IL_0009
// [19 9 - 19 10]
IL_0009: ldloc.0 // V_0
IL_000a: ret
} // end of method Class1::Test2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question