Get 20M+ Full-Text Papers For Less Than $1.50/day. Start a 14-Day Trial for You or Your Team.

Learn More →

A Programmer’s Introduction to C# 2.0Defensive Programming

A Programmer’s Introduction to C# 2.0: Defensive Programming CHAPTER 39 ■ ■ ■ The .NET runtime provides a few facilities for making programming less dangerous. You can use conditional methods and tracing to add checks and log code to an application, to catch errors during development, and to diagnose errors in released code. Conditional Methods Conditional methods are typically used to write code that performs operations only when compiled in a certain way. This often takes place in order to add code that’s called only when a debug build is made and not when called in other builds, usually because the additional check is too slow. In C++, you’d do this by using a macro in the include file that changes a function call to nothing if the debug symbol isn’t defined. This doesn’t work in C#, however, because there’s no include file or macro. In C#, you can mark a method with the Conditional attribute to indicate when calls to it should be generated. For example: using System; using System.Diagnostics; class MyClass public MyClass(int i) this.i = i; [Conditional("DEBUG")] public void VerifyState() if (i != 0) Console.WriteLine("Bad State"); 473 474 CH APTER 39 ■ DEFENSIVE PROGRAMMING int i = 0; class Test public static void Main() http://www.deepdyve.com/assets/images/DeepDyve-Logo-lg.png

A Programmer’s Introduction to C# 2.0Defensive Programming

Editors: Gunnerson, Eric; Wienholt, Nick

Loading next page...
 
/lp/springer-journals/a-programmer-s-introduction-to-c-2-0-defensive-programming-riVe1PQMn0
Publisher
Apress
Copyright
© Apress 2005
ISBN
978-1-59059-501-5
Pages
473 –484
DOI
10.1007/978-1-4302-0035-2_39
Publisher site
See Chapter on Publisher Site

Abstract

CHAPTER 39 ■ ■ ■ The .NET runtime provides a few facilities for making programming less dangerous. You can use conditional methods and tracing to add checks and log code to an application, to catch errors during development, and to diagnose errors in released code. Conditional Methods Conditional methods are typically used to write code that performs operations only when compiled in a certain way. This often takes place in order to add code that’s called only when a debug build is made and not when called in other builds, usually because the additional check is too slow. In C++, you’d do this by using a macro in the include file that changes a function call to nothing if the debug symbol isn’t defined. This doesn’t work in C#, however, because there’s no include file or macro. In C#, you can mark a method with the Conditional attribute to indicate when calls to it should be generated. For example: using System; using System.Diagnostics; class MyClass public MyClass(int i) this.i = i; [Conditional("DEBUG")] public void VerifyState() if (i != 0) Console.WriteLine("Bad State"); 473 474 CH APTER 39 ■ DEFENSIVE PROGRAMMING int i = 0; class Test public static void Main()

Published: Jan 1, 2005

There are no references for this article.