V
V
Vitaly2016-11-03 15:04:19
Programming
Vitaly, 2016-11-03 15:04:19

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 "большие ботинки"; }
        }

and
public string Test
        {
          return "большие ботинки"
        }

What for in general it is get?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
J
JihadTV, 2016-11-03
@zet694

Either I lagged behind Sharpe, or the second one is not clear at all.
The first is a property that has only a getter.

S
Sanan Yuzb, 2016-11-03
@Sanan07

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.

S
Sergey Zelensky, 2016-11-03
@SergeyZelensky-Rostov

get - a getter (function) that can do something with the data when requested, return will simply return the value

M
Mikhail, 2016-11-03
@dmitrievMV

maybe it was meant in option 2?

public string Test2()
        {
            return "большие ботинки";
        }

then here is the difference in IL Code:
.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 question

Ask a Question

731 491 924 answers to any question