Saturday, November 23, 2013

Misconception of Dyanmic type passed to function and type returned

Shout it kick it on
Small post is about miconception related to dynamic type variable supported by the C# langauge. Read more about dynamic keyword over here : Dynamic Types

Check below code
dynamic str = "22/11/2013 10:31:45 +00:01";
var withOffset = DateTimeOffset.Parse(str);

According to written code most of the developer thinks after wiriting above code type of "withOffset" variable is type written by the function "DateTimeOffset.Parse" i.e. "DateTimeOffset".

Devloper of the code thinks that compiler treat "var withOffset" as DateTimeOffset and all the method / property is avaiable which is avaialbe for "DateTimeOffset" type is avaible for "withOffset" variable. But its not true.

So question get raised why the type of not get changed to return type of the function.

Answer is :
When you use dynamic, the entire expression is treated at compile time as a dynamic expression, which causes the compiler to treat everything as dynamic and get run-time binding.

This is explained in 7.2 of the C# Language specification:
When no dynamic expressions are involved, C# defaults to static binding, which means that the compile-time types of constituent expressions are used in the selection process. However, when one of the constituent expressions in the operations listed above is a dynamic expression, the operation is instead dynamically bound.
This basically means that most operations (the types are listed in section 7.2 of the spec) which have any element that is declared as dynamic will be evaluated as dynamic, and the result will be a dynamic.

Since the argument is dynamic, the compiler cannot know which method will be called at runtime. Therefore, it cannot know what the return type will be. We might know that the return type will be DateTimeOffset, but the compiler does not, and cannot, know that.
Reference :

Monday, November 18, 2013

Help Yourself in Debugging (Part-2) using Breakpoint/Tracepoint

Shout it kick it on
Read Part 1 : Help yourself in Debugging by using Call Stack and Immediate Window
In this post I am going to discuss about the “BreakPoint” which is one of the most used feature by developer to debugging application by breaking execution of program runtime at certain point. I am going to describe the feature provided in visual studio for breakpoint and how it’s useful for debugging code easily.

What is Breakpoint?
Breakpoint is feature provided in Microsoft Visual Studio which allows breaking execution of code at runtime when debugging application.

For Example:

When you click on side of the code in visual studio it setup break point in you code as displayed in above image. So when you start execution code it will break execution at this point.

BreakPoint Menu (Option for the Break)

Above image display the context-menu for breakpoint i.e breakpoint menu. Following describe the all the option one by one.
  1. Delete Breakpoint
  2. This option of the breakpoint menu allows removing breakpoint.
  3. Disable Breakpoint
  4. This option of the breakpoint menu allows disabling breakpoint, so the breakpoint just get disable and become gray. Useful when developer just want to execute code without stopping at breakpoint and later on enable when want to use it back.
  5. Location…
  6. This menu option allows setting break point at given location i.e. at given line in source code file.

    When click on the menu it display dialog that provide information about source code file, line number in code and character. This dialog allows changing line and character information.

    For Example:

    Change line number to 26 in the in dialog than it breakpoint change to line number 26.

  7. Condition…
  8. When this option clicked it below dialog get displayed which has two options.
    • (A).   IS true
    • This option allows setting condition for breakpoint, so according to this execution during debugging get stopped at breakpoint i.e. breakpoint get hit when condition satisfied.
      Fox Example:

      As in above image condition set is “i==1000” that means this breakpoint get hit when this condition is true i.e. satisfied.

      This is very helpful when you want to stop execution of the application when certain condition met specially as in example loop having many number of element and want to stop at given location.

      Once condition set breakpoint displayed as below.

      Breakpoint changed to read icon having + sign in it. When mouse get over on breakpoint visual studio display information related to breakpoint with condition in tooltip.
    • (B).   Has changed
    • This option allows breaking execution of code at breakpoint when there is change in the variable or object.
      For Example:

      Over here variable “i” is entered in the dialog i.e. if the value of variable “i” get changed than execution of program get stopped at breakpoint.

      Below image shows the tooltip on breack point once condition set.

      When this option utilize than in breakpoint context menu condition option get check as in below image.

  9. Hit Count…
  10. This option display count number of time breakpoint hit when executing program.

    This dialog box has three options which allow to set hit count and if the hit count matches than program execution get stop at breakpoint. Each option is self-explanatory you can see in following image.

  11. Filter…
  12. This option allow to set condition related to “Machinename and Process” according to which breakpoint get hit. Following image explain more detail about it.

  13. When Hit…
  14. This option allows setting the message than get print to output window when program executions pass the breakpoint.

    This option converts breakpoint to Tracepoint i.e. so the developer can do tracing of the import variable or things by printing message on the output window when code at trace point get executed.
    For Example:

    As in above image to read value of the variable developer created message that get printed on output window each time code in for loop get executed.

    You can read the text in dialog which also provides information about how to provide information in the textbox so that it gets print to output window.

    Once developer press ok by setting the tracing message breakpoint get display like square as in below window.

    So when this program gets executed in visual studio, messages get printed to output window as shown in below image.

  15. Edit Labels…
  16. This option allows setting label for your breakpoint. So it display dialog like this.

    This is helpful when developer exporting breakpoint from visual studio, help to remember how important breakpoint it via label, create of useful breakpoint by using same name etc.
  17. Export…
  18. This option allows exporting breakpoint set by developer.

    This exported breakpoint information get saved as “XML” as show in above image. Exported XML file for breakpoint look as below

    This option is helpful when developer want to export breakpoint and save it as XML than remove it. When there is need to same breakpoint he/she can load same breakpoint by loading XML file using Debug window of Visual studio.
BreakPoint window
Breakpoint/tracepoint set by developer in code is get displayed by this window. This window also displays breakpoint with other information.

Following is way which shows how one can start breakpoint window.

So one can either use debug menu and start breakpoint window or make use of short cut Ctrl+D,B.

Following is image of breakpoint window.

This window also has toolbar which allows following, which is also supported by breakpoint menu discussed above.
  1. Set new breakpoint
  2. Remove breakpoint
  3. Delete all breakpoint
  4. This option allows to delete all breakpoint which is selected in breakpoint grid.
  5. Enable/Disable breakpoint
  6. Export breakpoint
  7. Import breakpoint
  8. This option is not part of the breakpoint menu; it is used for importing breakpoint/tracepoint.

    When this button clicked by developer, visual studio open file browser window to browse xml file for breakpoint / tracepoint.

    So after loading this file breakpoint/tracepoint get set again in the code.

  9. Go to Source Code
  10. Go to Diassembly
  11. Columns
  12. This tool menu allows to set number of column in the below grid i.e. in breakpoint window grid which is below toolbar.

  13. Search
  14. Search allows searching breakpoint in the breakpoint grid of breakpoint window. Following is image of the same. As per the criteria specified breakpoint grid window display searched breakpoints.

Breakpoint Grid

This grid window displays all the breakpoint set by developer in source code. This grid provides information about each breakpoint according to columns set by developer in grid.


Breakpoints/tracepoints are very useful feature provided in visual studio, which is very usefl when you are debugging large amount of code. Visual studio provides flexibility to break execution of the code during debugging at breakpoint which is very helpful to the developer.

Thanks for reading and do post comment if you like it or something is missing or wrong.