term for c#

Does anyone know the term below

when a property is non-virtual and contains only a small amount of code, the execution environment may replace calls to accessors with the actual code of the accessor.

Thank you for help.



Answer this question

term for c#

  • jackinthegreen

    There is another benefit of inlining, potentially much greater than merely avoiding the call/return.

    It allows the optimizer to evaluate the inlined method within the enviroment of the calling method. To demostrate, using your example, if we'd written:

    this.MyProperty = "123456";
    string foo = this.MyProperty;

    This way, the compile can know, unquestionably that m_myproperty is neither null nor less than 3 characters, it can generate code like this:

    this.myproperty = "123456";
    string foo = this.myproperty.Substring(0,3);

    THeortically, It could generate:

    this.myproperty = "123456";
    string foo = "456";

    but that would require the compiler knowing the effect of a particular method (although for a common one like String.Substring() it might be added as a special case --- but that would be in some distant future version of the compile)



  • michael olson

    Which term "Property," "virtual," "accessor," or something else entirely Please clarify your question.

  • Avi29

    Mr.Mario,

    Thank you for the tip.

    It helped me a lot. I appreciate it!!


  • sdelmas

    You are referring to a process known as inlining.

    What it says is that the accessors (i.e. get and set) of a property with very little code will be inlined in your code. In a nutshell, say you have the following (rather silly) property:

    private string m_myproperty;
    public string MyProperty {
    get {
    if ((m_myproperty != null) && (m_myproperty.Length > 3))
    return m_myproperty.Substring (0, 3);
    else
    return "";
    }
    }

    and somewhere in your code you access the property.

    string foo = this.MyProperty;

    Now, this would generally be compiled as a call... forget the IL, it will look like a call to a method.

    string foo = this.get_MyProperty ();

    According to your quote, the compiler might decide to inline the code, so that it will be compiled as if it were written:

    string foo;
    if ((m_myproperty != null) && (m_myproperty.Length > 3))
    foo = this.m_myproperty.Substring (0, 3);
    else
    foo = "";

    Inlining obviosuly expands your code, but it will run faster as it saves a call. The compiler will decide when the benefit surpasses the size increase.

    Even more interesting what happens with a set accessor: in that case you also save pushing the parameter on the stack to call the method...

    As for the "non-virtual" part, if the property were virtual, all this could not happen as the compiler cannot decide at compile time which method will be called.

    FYI, properties are not the only constructs that are candidates for inlining: methods will also be considered. Since the size limit is very low, though (should be 32 bytes of IL), it is less likely that a method will fit.

    HTH
    --mc


  • term for c#