Book Image

Visual Studio 2013 and .NET 4.5 Expert Cookbook

Book Image

Visual Studio 2013 and .NET 4.5 Expert Cookbook

Overview of this book

Table of Contents (14 chapters)
Visual Studio 2013 and .NET 4.5 Expert Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Introduction


Being one of the best IDEs available, it may often seem that our knowledge about Visual Studio falls short when some special circumstances appear. Visual Studio provides hundreds of tools inside it and also some tools as extensions to help programmers develop software in the best possible ways. It incorporates features such as refactoring, code analysis, clone code finder, and so on to let a developer write better code and have the best experience while inside the IDE. Visual Studio has been so popular that it is not constrained to just software development; some people use it to edit documents and images, or even to publish content. It is so user friendly and easy to use that anyone can work on it at a basic level.

Debugging is the most important part of any application development process. People like to start debugging a program while developing code. Debugging is a process that lets you have a quick look at the current state of a program by walking through your code step by step and letting you identify what exactly is happening while the program is running. Developers expect an IDE to give them as much information as possible during a certain state of the program. Visual Studio has taken every step to improve the debugging experience of the IDE and make small tools to let developers find better ways to quickly find the possible problems in code (if any).

After opening the IDE, it is often the case that people start typing some code. But, it is unlikely that developers will always finish writing the code before they start debugging. After some part of the development is done, they tend to start debugging it to have a better understanding of the logic and functionalities. Some people start debugging even before they have written a single line of code. So debugging is not just identifying bugs, it is determining the merits and demerits of the code as well. Debugging inside Visual Studio is perhaps the best selling point of the IDE.

Visual Studio separates the environment layout completely while debugging. After writing your code, when you press F5 on the keyboard or go to Debug | Start Debugging, Visual Studio adjusts the whole layout of the window and creates an environment that is best suited for debugging; for instance, when you are in the debug mode, generally the Solution Explorer, Toolbox, the Properties window, and so on get hidden. Instead, windows such as IntelliTrace, Autos, Locals, Watch, and Call Stack automatically open inside the IDE and get docked/adjusted to the layout inside the IDE. The IDE is smart enough to store the layout information of all the opened windows inside it when the debugging session is closed by the user for a particular project. Therefore, when you start debugging again, the IDE provides you an environment that is exactly adjusted to the left-hand side when the previous debug session is closed.

In this chapter, we will try to demonstrate some of the important debugging tools available in the IDE. Let's start with a simple class that consists of a few properties and methods:

public class TestClass
{
  public int Start { get; set; }
  public int End { get; set; }

  public TestClass(int start, int end)
  {
    this.Start = start;
    this.End = end;
  }
  public int GetSum(int start, int end)
  {
    var sum = 0;
    for (var i = start; i <= end; i++)
      sum += i;
    return sum;
  }

  public string GetMessage()
  {
    var sum = this.GetSum(this.Start, this.End);
    return string.Format("Sum of all numbers between {0} and {1}  is {2}", this.Start, this.End, sum);
  }
  public void PrintMessage()
    {
      var message = this.GetMessage();
      Console.WriteLine(message);
      Console.ReadKey(true); 
    }

  }
  class Program
  {
    static void Main(string[] args)
    {
      var tclass = new TestClass(20, 30);
      tclass.PrintMessage();
    }
  }
}

The dummy code that we are using here has a TestClass, which takes two arguments and stores data in the data members Start and End. GetSum is a method that sums up all the integers between Start and End. The GetMessage returns a message after calling Sum with Start and End, and finally PrintMessage prints the message on the console.

The program creates an object of TestClass and calls PrintMessage with 20, 30. If you run the program, it shows 275 on the console.