Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

An Interesting Aside: The System.

Environment class
The System.Environment class allows us to obtain a number of details regarding the operating system currently hosting our .NET application. The following program illustrates how to retrieve the details regarding .NET development machine.

namespace Environ { class Program { static void Main(string[] args) { Console.WriteLine("OS: {0}", Environment.OSVersion); Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory); Console.WriteLine("Number of processors: {0}",Environment.ProcessorCount); Console.WriteLine(".NET Version: {0}",Environment.Version); string[] d = Environment.GetLogicalDrives(); for (int i = 0; i < d.Length; i++) Console.WriteLine("Drive {0} : {1}", i, d[i]); Console.Read(); } } }

Output:
OS: Microsoft Windows NT 5.1.2600 Service Pack 2 Current Directory: E:\work\C#\C#-programs\Environ\bin\Debug Number of processors: 1 .NET Version: 2.0.50727.42 Drive 0 : A:\ Drive 1 : C:\ Drive 2 : D:\ Drive 3 : E:\ Drive 4 : F:\ Drive 5 : G:\ Drive 6 : H:\ Drive 7 : I:\ Drive 8 : L:\ Drive 9 : M:\

C# Preprocessor Directives
Like many other languages in the C family, C# supports the use of various symbols that allow us to interact with the compilation process. In C#, there is no separate preprocessing step. Rather, preprocessing directives are processed as part of the lexical analysis phase of the compiler. In any case, the syntax of the C# preprocessor directives is very similar to that of the other members of the C family, in that the directives are always prefixed with the pound sign (#) Table given below defines some of the more commonly used directives. Table: Common C# Preprocessor Directives Directives Meaning in Life #region, Used to mark sections of collapsible source code #endregion Used to define and undefine conditional compilation #define, #undef symbols #if, #elif, #else, Used to conditionally skip sections of source code #endif (based on specified compilation symbols) #error, #warning #line Used to Issue errors and warnings for the current build Used to control the line numbers emitted for errors and warnings

Specifying Code Regions Perhaps some of the most useful of all preprocessor directives are #region and #endregion. Using these tags, we are able to specify a block of code that may be hidden from view and identified by a friendly textual marker. Use of regions can help keep lengthy *.cs files more manageable. For example, we could create one region for a types constructors, another for type properties, and so forth: class Car { private string petName; private int currSp; #region Constructors public Car() { ... } public Car (int currSp, string petName) { ... } #endregion #region Properties public int Speed { ... } public string Name { ... } #endregion }

When we place our mouse cursor over a collapsed region, we are provided with a snapshot of the code lurking behind.

Conditional Code Compilation The next batch of preprocessor directives (#if, #elif, #else, #endif) allows us to conditionally compile a block of code, based on predefined symbols. The classic use of these directives is to identify a block of code that is compiled only under a debug (rather than a release) build: class Program { static void Main(string[] args) { #region Print machine info under DEBUG build // This code will only execute if the project is // compiled as a debug build. #if DEBUG Console.WriteLine("App directory: {0}", Environment.CurrentDirectory); Console.WriteLine("Box: {0}", Environment.MachineName); Console.WriteLine("OS: {0}", Environment.OSVersion); Console.WriteLine(".NET Version: {0}", Environment.Version); #endif #endregion } } Here, you are checking for a symbol named DEBUG. If it is present, we dump out a number of interesting statistics using some static members of the System.Environment class. If the DEBUG symbol is not defined, the code placed between #if and #endif will not be compiled into the resulting assembly, and it will be effectively ignored. We can also define our own custom preprocessor symbols. Using #define, we can define a symbol named MONO_BUILD. The #define directive must be listed before anything else in the *.cs code file. #define MONO_BUILD using System; namespace PreprocessorDirectives { class Program { static void Main(string[] args) { #if MONO_BUILD Console.WriteLine("Compiling under Mono!"); #else

Console.WriteLine("Compiling under Microsoft .NET"); #endif } } } To create a project-wide symbol, make use of the Conditional compilation symbols text box located on the Build tab of your projects Properties page

You might also like