22. #line 45 "Program1.cs" //---treated as line 22---
23. Single s; //---treated as line 45---
24. Console.WriteLine("Line 5"); //---treated as line 46---
25. Console.ReadLine(); //---treated as line 47---
26. }
27. }
28. }
The line numbers are for illustration purposes and are not part of the program.
The four highlighted lines are numbered 13, 14, 20, and 23. When you build the program in Visual Studio 2008, the lines reported are 25, 26, 20, and 45 (see Figure 3-18).
Figure 3-18
Let's take a look at the #line
directives in the example program:
□ #line 25
means that you want to modify the line number to use the specified line number (25 in this case) instead of the actual line number of the statement in error. This is useful if you need to assign a fixed line number to a particular part of the code so that you can trace it easily. Interestingly, the next line will continue from 25, that is, the next line is now line 26. This is evident from the warning message for the char c;
line.
□ #line default
means that the compiler will report the actual line number.
□ #line 45 "Program1.cs"
means that you want to fix the line number at 45 and specify the name of the file in error ( Program1.cs
in this case). An example usage of it would be that the statement in error might be a call to an external DLL and by specifying the filename of the DLL here, it is clearer that the mistake might be from that DLL.
What about the #line hidden
statement? That preprocessor directive indicates to the debugger to skip the block of code beginning with the #line hidden
preprocessor directive. The debugger will skip the line(s) until the next #line
preprocessor directive is found. This is useful for skipping over method calls that you are not interested in (such as those not written by you).
Interestingly, you can replace the #line hidden
preprocessor directive with #line 16707566
(0xFeeFee) and it will still work correctly.
The #region
and #region
preprocessor directives are used in conjunction with Visual Studio's Code Editor. Let's work with the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
static void Main(string[] args) {
//---implement ions here---
}
private void Method1() {
//---implementations here---
}
private void Method2() {
//---implementations here---
}
private void Method3() {
//---implementations here---
}
}
}
Often, you have many functions that perform specific tasks. In such cases, it is often good to organize them into regions so that they can be collapsed and expanded as and when needed. Using this example, you can group all the methods — Method1()
, Method2()
, and Method3()
— into a region using the #region and #region preprocessor directives:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
static void Main(string[] args) {}
#region "Helper functions"
private void Method1() {
//---implementations here---
}
private void Method2() {
//---implementations here---
}
private void Method3() {
//---implementations here---
}
#endregion
}
}
In Visual Studio 2008, you can now collapse all the methods into a group called "Helper functions"
. Figure 3-19 shows the Code Editor before and after the region is collapsed.
Figure 3-19
The #region
and #region
preprocessor directives do not affect the logic of your code. They are used purely in Visual Studio 2008 to better organize your code.
The #pragma warning
directive enables or disables compiler warning messages. For example, consider the following program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
In this program, the variable num
is defined but never used. When you compile the application, the C# compiler will show a warning message (see Figure 3-20).
Figure 3-20
To suppress the warning message, you can use the #pragma warning
directive together with the warning number of the message that you want to suppress:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#pragma warning disable 414
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
This example suppresses warning message number 414 ("The private field 'field' is assigned but its value is never used"). With the #pragma warning
directive, the compiler will now suppress the warning message (see Figure 3-21).
Figure 3-21
You can suppress multiple warning messages by separating the message numbers with a comma (,) like this:
#pragma warning disable 414, 3021, 1959
In this chapter, you explored the basic syntax of the C# language and saw how to use Visual Studio 2008 to compile and run a working C# application. You examined the different data types available in the .NET Framework and how you can perform type conversion from one type to another. You have also seen the various ways to perform looping, and the various processor directives with which you can change the way your program is compiled.
Chapter 4
Classes and Objects
One of the most important topics in C# programming — in fact, the cornerstone of .NET development — is classes and objects.
Classes are essentially templates from which you create objects. In C# .NET programming, everything you deal with involves classes and objects. This chapter assumes that you already have a basic grasp of object-oriented programming. It tackles:
Читать дальше