How To Code .NET - Tips and Tricks For Coding .NET 1.1 and .NET 2.0 Applications Effectively
How To Code .NET - Tips and Tricks For Coding .NET 1.1 and .NET 2.0 Applications Effectively
MAGENTA BLACK
PANTONE 123 CV
How to
Using .NET 2.0 Patterns tects, or developers strive to do their best, and if given the choice they will
always do something correctly. Of course, this begs the question: Why do we
have so many bugs in our code?
I think the main reason for buggy code is that we are all short on time. We
don’t have the luxury of investigating new Framework features fully or exploring
innovative new techniques as thoroughly as we would like, because we’re all
watching the clock. That means our code has bugs—the new Framework feature
we implemented doesn’t work quite as expected, and the new best practice we
put in place doesn’t seem to work the same way for every input. These bugs are
Code .NET
frustrating and can often be very difficult to solve.
This book is a response to that problem. In it I have investigated and recorded
my experiences of a wide range of .NET Framework features. They’re arranged
in simple, bite-sized sections dedicated to problem solving, informing you of
little-known functionality and keeping you up to date with the latest design
thinking. It’s a road map to your more effective use of the .NET Framework.
For example, the .NET Framework 2.0 introduced the yield keyword. On the
face of it, this is a really cool new piece of functionality that we’d all like to use.
But what’s it really like? Is it buggy? Is it going to be the future of all iterators?
This book digs into these questions and more to provide you with the answers
that you need.
Christian Gross
Tips and Tricks for Coding .NET 1.1
and .NET 2.0 Applications Effectively
forums.apress.com
SOURCE CODE ONLINE
Companion eBook FOR PROFESSIONALS BY PROFESSIONALS ™
www.apress.com
this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY
7443CH01.qxd 9/12/06 10:15 PM Page 1
CHAPTER 1
T his book will introduce a series of techniques for how to write code in .NET. The best way to
learn is to run a series of tests, and thus most of the examples in this book use a utility called
NUnit1 because I want to implement test-driven development (TDD). In a nutshell the idea
behind TDD is to architect, develop, and test your code at the same time. Additionally, it helps
me write pieces of code that you can verify. In general development, the benefit of testing and
developing code at the same time is that when your code is finished you know that it is tested
enough to make you reasonably sure that stupid errors will not happen. The quality assurance
department at your company will not complain about the lack of quality.
This chapter will take you through the steps for writing your own test routines. You’ll learn
about publicly available testing tools and their uses. Then you’ll move on to topics such as
how to define tests, why contexts are important, and how to implement mock objects.
• Code and tests that are written simultaneously expose integration, data, and logic
problems early.
• Code is broken down into smaller chunks, making it easier to maintain an overview of
the contained logic.
1. http://www.nunit.org/ 1
7443CH02.qxd 9/14/06 11:12 PM Page 31
CHAPTER 2
T he solutions covered in this chapter relate to the .NET runtime and are not particular to any
specific language. Of course, it is not advisable to present the bulk of the solutions using
Microsoft Intermediate Language (MSIL), and a language must be used. For the scope of this
chapter and book, I’ve chosen C# as the language.
31
7443CH03.qxd 9/21/06 4:36 PM Page 85
CHAPTER 3
Text-Related Solutions
T here are many countries, cultures, and languages on this planet we call home. People in
each country may speak a single language or multiple languages, and each country may host
cultures. In the early days of computing, you could choose any language to use—so long as it
was American English. As time progressed we became able to use software in multiple lan-
guages and for multiple cultures. .NET raises the bar; it lets you mix and match cultures,
languages, and countries in a single compiled application. This chapter is about processing
text in a multiculture/multicountry/multilanguage situation.
Source: /Volume01/LibVolume01/StringToBufViceVersa.cs
[Test]
public void SimpleAsciiConversion() {
String initialString = "My Text";
byte[] myArray =
System.Text.Encoding.ASCII.GetBytes(
initialString);
String myString =
System.Text.Encoding.ASCII.GetString(
myArray);
Assert.AreEqual( initialString, myString);
}
In the example the string initialString contains the text "My Text". Using the predefined
instance System.Text.Encoding.ASCII and method GetBytes, the string is converted into a
byte array. The byte array myArray will contain seven elements (77, 121, 32, 84, 101, 120, 116)
that represent the string. The individual numbers correspond to representations of the letter
from the ASCII1 table. Byte arrays are examples of lookup tables, where the value of a byte
1. http://www.lookuptables.com/ 85
7443CH04.qxd 9/17/06 1:37 PM Page 117
CHAPTER 4
C# Coding Solutions
I n the previous chapters, the focus was on how to use the .NET API. All of the examples were
illustrated using C#, but the examples did not use any particular feature of C#. The examples
could have been implemented with VB.NET or any other .NET language. That changes in this
chapter, as the focus is on the C# programming language. Specific features of the language will
be dissected and analyzed. Sometimes patterns will be used, and other times not. In the over-
all scheme of this chapter, the idea is to give you a better understanding of what C# is capable
of and not capable of.
117