
Objectives:
• Best practices for numerical / string / array / constant data
• Best practices for creating interface, structure and enum types
• Best practices regarding memory management
• Best practices regarding assembly development
• Discover the numerous versions of Visual Studio 2005
• Work with the VS 2005 visual designers
• Examine VS 2005 refactoring support
• Understand the role of code snippets / surround with technology
• Understand the OTB (Object Test Bench)
Chapter Overview
Although software development is a very creative activity, we can all benefit by agreeing upon a set of time
tested rules (a.k.a. best practices). This chapter will walk you through a number of common best practices
regarding the construction of C# and VB 2005 code.
Other chapters in the class will address best practices for specific technology sets (ASP.NET, ADO.NET,
Windows Forms, etc).
The Role of Best Practices and FxCop
• Best practices provide a solid foundation for building software within an application framework, and
the .NET platform is no different.
• Over the years, hundreds of ‘official’ best practices have come into being.
• Many of them are commonsensical in nature (don’t name your member variables xxxUUD3), while
others are truly enlightening (e.g. unsubscribe from events a.s.a.p!).
• It would be impractical to enumerate over the details of every .NET best practice.
• Some BPs only make sense if you are working with a specific programming language or technology set.
• Others (such as naming conventions) may be abandoned altogether in favor of in-house BPs.
• The point of this chapter is to illustrate several BPs which target building solid code design /
performance concerns for C# and VB 2005.
• Later chapters will examine best practices for specific technology sets (ASP.NET, ADO.NET,
Windows Forms, etc).
• The .NET Framework SDK Documentation has a dedicated section specifically for describing the
official Microsoft coding best practices.
• This area of the help system will complement and extend the information presented here.
• The topic "Design Guidelines for Developing Class Libraries" can be found under .NET Development | .
NET Framework SDK | General Reference.
• Perhaps the ‘best best practice’ is to make use of fxcop.exe during the development of your assemblies:
• This free Microsoft utility allows you to load in any number of .NET assemblies into the UI-shell, and
test them against all of the MS best-practices.
• Fxcop.exe can also be run at the command line, which is helpful for automated builds via nant or
msbuild.
• This tool can be downloaded from numerous locations such as www.gotdotnet.com.
• To use fxcop.exe, begin by creating a new project using the File | New Project menu item.
• You can then add the assembly to analyze using drag-drop techniques or via the Projects | Add File…
menu item.
• The Targets tab allows you to select the namespaces, types and members to check.
• The Rules tab allows you to select which rules you wish to check against.
• Rule sets are bundled into .NET assemblies.
• Using fxcop.exe you can also author your own (company specific) rule assemblies.
• Here are some core categories:
FxCop Rule Category Meaning in Life
ComRules Tests assembly against COM interoperability rules.
DesignRules Tests assembly against design best practices.
Globalization Rules Tests assembly against best practices for building international software.
Naming Rules Tests assembly against suggest .NET naming conventions.
Performance Rules Tests against various performance best practices.
Security Rules Security best practices!
Usage Rules Tests against ‘all other’ best practices that do not necessary fall into the previous categories.
• Notice how you can selectively choose which rule sets (and which rules):
• Once you click the Analyze button, you will be given a breakdown as to how your assembly stacks up.
• Make the use of fxcop.exe a standard part of your development process.
• In fact, the new Team edition of Visual Studio 2005 has integrated the functionality of fxcop.exe
directly within the IDE.
• Simply access the Code Analysis tab of the project’s property page.
• Violations will be reported under the warning tab of the output window.
• You can suppress violations via right clicking.
• This will update your code with attributes, informing the compiler to ignore the offending code.
• For example, suppressing the CLS compliant violation results in the following assembly level attribute.
• Any assembly level suppressions are stored in a GlobalSupressions.* file.
• Suppressions for a given type or member results in attributes being applied at the code point.
[assembly:
System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
"CA1014:MarkAssembliesWithClsCompliant")]
.NET Architecture Tutorial
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials