Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 3 Debugging Your Code

Listing 3-8.  Modified Subject Class

[DebuggerDisplay("Code: {SubjectCode, nq}, Subject: {SubjectDescription, nq}")]

public class Subject

{

public int SubjectCode { get; set; }

public string SubjectDescription { get; set; }

}

Start debugging your code again and have a look at your Watch window after adding the DebuggerDisplay attribute. Your item values are more readable as seen in Figure 3-31.

Figure 3-31.  The lstSubjects variable values with DebuggerDisplay

The use of “nq” in the DebuggerDisplay attribute will remove the quotes when the final value is displayed. The “nq” means “no quotes.”

Evaluate Functions Without Side Effects

While debugging an application, we probably do not want the state of the application to change because of an expression we are evaluating. It is, unfortunately, a fact that evaluating some expressions might cause side effects.

To illustrate this, we will need to write some more code. We will be creating a class called Student that contains a List of Subject as seen in Listing 3-9.

188

Chapter 3 Debugging Your Code

Listing 3-9.  The Student Class

public class Student

{

private List<Subject> _subjectList; public Student() { }

public Student(List<Subject> subjects) => _subjectList = subjects; public bool HasSubjects() => _subjectList != null;

public List<Subject> StudentSubjects

{

get

{

if (_subjectList == null)

{

_subjectList = new List<Subject>();

}

return _subjectList;

}

}

}

In this class, we have a HasSubjects() method that simply returns a Boolean indicating if the Student class contains a list of subjects. We also have a property called StudentSubjects that returns the list of subjects. If the list of subjects is null, it creates a new instance of List<Subject>.

It is here that the side effect is caused. If the HasSubjects() method returns false, calling the StudentSubjects property will change the value of HasSubjects().

This is better illustrated in the following screenshots. Create a new instance of Student and place a breakpoint right after that line of code (Figure 3-32).

Figure 3-32.  Place a breakpoint after Student

189

Chapter 3 Debugging Your Code

If we now use the Watch window to look at the value returned by the HasSubjects() method, we will see that it returns false (Figure 3-33).

Figure 3-33.  HasSubjects() method returns false

When we call the StudentSubjects property, we see this side effect come into play in Figure 3-34. As soon as this property is called, the value of the HasSubjects() method changes.

This means that the state of our Student class has changed because of an expression that we ran in the Watch window.

This can cause all sorts of issues further down the debugging path, and sometimes the change might be so subtle that you don’t even notice it. You could end up chasing “bugs” that never really were bugs to begin with.

Figure 3-34.  HasSubjects() method value has changed

190