none
When enabling c# optimize code cause issue RRS feed

  • Question

  • I have two floating variable with value (value1 = 44.35 and value2 = 44.35).

    If(value1 > value2)

    //Do some operation

    Without enabling optimize code check box in visual studion this condition check failed and nothing will executed.

    When i enable this property then execution started even its failed (show as false when checking using mouseover). May i know the reason for these?

    Tuesday, December 20, 2016 10:06 AM

All replies

  • Hi Vijay_RM,

    The /optimize option enables or  disables optimizations performed by  the compiler to make your output file  smaller, faster, and more efficient.

    Here I referred from MSDN document.

    The reason optimization is disabled by default for Debug configurations is that it makes certain debugging features impossible. A few notable ones:

    • Perhaps most crucially, the Edit and Continue feature is disabled - i.e. no modifying code during execution.
    • Breaking execution often means the wrong line of code is highlighted (usually the one after the expected one).
    • Unused local variables aren't actually assigned or even declared.

    The default options for optimization never ought to be changed. Having the option off for debugging is highly useful, while having it on for Release mode is equally wise.

    Best regards,

    Kristin

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 21, 2016 1:31 AM
  • Hi Vijay_RM,

    Any updates?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 22, 2016 6:14 AM
  • Hi Kristin,

    Thank you for your information.

    Still i am not clear, why mentioned issue raised. Can you please explain me why the code executed even the if condition faile?

    Regards,

    Vijay

    Friday, December 23, 2016 5:29 AM
  • Hi  Vijay,

    I want to know what configuration mode is. Debug or Release?

    By the way, Could you also show me your test code and write a detailed steps here? And do you set a breakpoint? I will test on my side.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 23, 2016 5:38 AM
  • Hi Kristin,

    Thank you for your update.

    I face same issue both in Debug and release mode. Difference is whether optimize code check box enabled or not. If enabled it cause issue otherwise it did not.

    For test code:

    Sorry unfortunately i can't provide code snippet because my project is too big. Also i can't able to reproduce the same issue while testing in simple applications.

    Scenario:

    RectanglF rect - bottom value is 44.35

    float argumentPassed=44.35

    If(rect.bottom > argumentPassed) //It failed when seeing in intellisence but inner code was executed.

    { //Do operation }

    Is this information enough?

    Regards,

    Vijay

    Friday, December 23, 2016 9:41 AM
  • Hi Vijay,

    >>I face same issue both in Debug and release mode. Difference is whether optimize code check box enabled or not. If enabled it cause issue otherwise it did not.

    If you are using release mode,  optimize code check box will be enabled automatically.

    When the compiler optimizes code, it repositions and reorganizes instructions. This results in more efficient compiled code. Because of this rearrangement, the debugger cannot always identify the source code that corresponds to a set of instructions.

    Optimization can affect:

    • Local variables, which can be removed by the optimizer or moved to locations the debugger does not understand.

    • Positions inside a function, which are changed when the optimizer merges blocks of code.

    • Function names for frames on the call stack, which might be wrong if the optimizer merges two functions.

    In another aspect, none of breakpoints are hit in the Release build!

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Thursday, December 29, 2016 9:38 AM
    Monday, December 26, 2016 9:41 AM