Saturday, December 29, 2012

Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition

Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition
The Intermediate Language Disassembler utility (ildasm.exe), which ships with the .NET Framework 3.5 SDK, allows you to load up any .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata. To load ildasm.exe, open a Visual Studio command prompt (using Start ➤ All Programs ➤ Microsoft Visual Studio 2008 ➤ Visual Studio Tools), type ildasm and press the Enter key.

 if you deploy an assembly to a computer that does not have .NET installed, it will fail to run. For this reason, Microsoft provides a setup package named dotnetfx3setup.exe that can be freely shipped and installed along with your .NET software. This installation program can be freely downloaded from Microsoft from their .NET download area (http://msdn.microsoft.com/netframework). Once dotNetFx35setup.exe is installed, the target machine will now contain the .NET base class libraries, .NET runtime (mscoree.dll), and additional .NET infrastructure (such as the GAC).
The Vista operating system is preconfigured with the necessary .NET runtime infrastructure. However, if you are deploying your application to Windows XP or Windows Server 2003, you will want to ensure the target machine has the .NET runtime environment installed and configured.

To conclude this chapter, I would like to point out a number of .NET development tools that complement the functionality provided by your IDE of choice. Many of the tools mentioned here are open source, and all of them are free of charge. While I don't have the space to cover the details of these utilities, Table 2-3 lists a number of the tools I have found to be extremely helpful as well as URLs you can visit to find more information about them.
Table 2-3: Select .NET Development Tools 

Tool
Meaning in Life
URL
FxCop
This is a must-have for any .NET developer interested in .NET best practices. FxCop will test any .NET assembly against the official Microsoft .NET best-practice coding guidelines.
Lutz Roeder's Reflector
This advanced .NET decompiler/object browser allows you to view the .NET implementation of any .NET type using a variety of languages.
NAnt
NAnt is the .NET equivalent of Ant, the popular Java automated build tool. NAnt allows you to define and execute detailed build scripts using an XML-based syntax.
NDoc
NDoc is a tool that will generate code documentation files for C# code (or a compiled .NET assembly) in a variety of popular formats (MSDN's *.chm, XML, HTML, Javadoc, and LaTeX).
NUnit
NUnit is the .NET equivalent of the Java-centric JUnit unit testing tool. Using NUnit, you are able to facilitate the testing of your managed code.

The Main() method may also be defined as public as opposed to private, which is assumed if you do not supply a specific access modifier. Visual Studio 2008 automatically defines a program's Main() method as implicitly private. Doing so ensures other applications cannot directly invoke the entry point of another.


On the Windows operating system, an application's return value is stored within a system environment variable named %ERRORLEVEL%. If you were to create an application that programmatically launches another executable (a topic examined in Chapter 17), you can obtain the value of %ERRORLEVEL% using the staticSystem.Diagnostics.Process.ExitCode property.

C# Parameter Modifiers 
Parameter Modifier
Meaning in Life
(None)
If a parameter is not marked with a parameter modifier, it is assumed to be passed by value, meaning the called method receives a copy of the original data.
out
Output parameters must be assigned by the method being called (and therefore are passed by reference). If the called method fails to assign output parameters, you are issued a compiler error.
ref
The value is initially assigned by the caller and may be optionally reassigned by the called method (as the data is also passed by reference). No compiler error is generated if the called method fails to assign a ref parameter.
params
This parameter modifier allows you to send in a variable number of arguments as a single logical parameter. A method can have only a single params modifier, and it must be the final parameter of the method.
 this technique is nothing more than a convenience for the caller.

Note the distinction between output and reference parameters:
  • Output parameters do not need to be initialized before they passed to the method. The reason for this? The method must assign output parameters before exiting.
  • Reference parameters must be initialized before they are passed to the method. The reason for this? You are passing a reference to an existing variable. If you don't assign it to an initial value, that would be the equivalent of operating on an unassigned local variable.
Select Members of System.Array 
Member of Array Class
Meaning in Life
Clear()
This static method sets a range of elements in the array to empty values (0 for value items, static for object references).
CopyTo()
This method is used to copy elements from the source array into the destination array.
GetEnumerator()
This method returns the IEnumerator interface for a given array. I address interfaces in Chapter 9, but for the time being, keep in mind that this interface is required by the foreach construct.
Length
This property returns the number of items within the array.
Rank
This property returns the number of dimensions of the current array.
Reverse()
This static method reverses the contents of a one-dimensional array.
Sort()
This static method sorts a one-dimensional array of intrinsic types. If the elements in the array implement the IComparer interface, you can also sort your custom types (see Chapter 9).


To define a nullable variable type, the question mark symbol (?) is suffixed to the underlying data type. Do note that this syntax is only legal when applied to value types. If you attempt to create a nullable reference type (including strings), you are issued a compile-time error. Like a nonnullable variable, local nullable variables must be assigned an initial value:
static void LocalNullableVariables() { // Define some local nullable types. int? nullableInt = 10; double? nullableDouble = 3.14; bool? nullableBool = null; char? nullableChar = 'a'; int?[] arrayOfNullableInts = new int?[10]; // Error! Strings are reference types! // string? s = "oops"; }

The ?? Operator

The final aspect of nullable types to be aware of is that they can make use of the C# ?? operator. This operator allows you to assign a value to a nullable type if the retrieved value is in fact null.

No comments:

Post a Comment