找回密码
 注册
搜索
热搜: java php web
查看: 528|回复: 0

Effective C#: 50 Specific Ways to Improve Your C#

[复制链接]
发表于 2009-10-24 23:28:09 | 显示全部楼层 |阅读模式


作者:Bill Wagner
出版日期:December 13, 2004
出版社:Addison Wesley
页数:336
ISBN:ISBN-10: 0321245660 ISBN-13: 978-0321245663
文件格式:CHM

From the Back Cover
“This book really demonstrates Bill’s strengths as a writer andprogrammer. In a very short amount of time, he is able to present anissue, fix it and conclude it; each chapter is tight, succinct, and tothe point.”
—Josh Holmes, Independent Contractor
“The book provides a good introduction to the C# language elementsfrom a pragmatic point of view, identifying best practices along theway, and following a clear and logical progression from the basicsyntax to creating components to improving your code writing skills.Since each topic is covered in short entries, it is very easy to readand you’ll quickly realize the benefits of the book.”
—Tomas Restrepo, Microsoft MVP
“The book covers the basics well, especially with respect to thedecisions needed when deriving classes from System.Object. It is easyto read with examples that are clear, concise and solid. I think itwill bring good value to most readers.”
—Rob Steel, Central Region Integration COE & Lead Architect, Microsoft
Effective C# provides the C# developer with the tools theyneed to rapidly grow their experience in Visual C# 2003 while alsoproviding insight into the many improvements to the language that willbe hitting a desktop near you in the form of Visual C# 2005.”
—Doug Holland, Precision Objects
“Part of the point of the .NET Framework—and the C# Language, inparticular—is to let the developer focus solving customer problems anddeliver product, rather than spending hours (or even weeks) writingplumbing code. Bill Wagner’s Effective C#, not only shows youwhat’s going on behind the scenes, but shows you how to take advantageof particular C# code constructs. Written in a dispassionate style thatfocuses on the facts—and just the facts—of writing effective C# code,Wagner’s book drills down into practices that will let you write C#applications and components that are easier to maintain as well asfaster to run. I’m recommending Effective C# to all students of my .NET BootCamp and other C#-related courses.”
—Richard Hale Shaw, www.RichardHaleShawGroup.com
C#’s resemblances to C++, Java, and C make it easier to learn, butthere’s a downside: C# programmers often continue to use oldertechniques when far better alternatives are available. In Effective C#,respected .NET expert Bill Wagner identifies fifty ways you can startleveraging the full power of C# in order to write faster, moreefficient, and more reliable software.
Effective C# follows the format that made Effective C++ (Addison-Wesley, 1998) and Effective Java(Addison-Wesley, 2001) indispensable to hundreds of thousands ofdevelopers: clear, practical explanations, expert tips, and plenty ofrealistic code examples. Drawing on his unsurpassed C# experience,Wagner addresses everything from value types to assemblies, exceptionsto reflection. Along the way, he shows exactly how to avoid dozens ofcommon C# performance and reliability pitfalls. You’ll learn how to:
  • Use both types of C# constants for efficiency and maintainability, see item 2
  • Use immutable data types to eliminate unnecessary error checking, see item 7
  • Avoid the C# function that’ll practically always get you in trouble, see item 10
  • Minimize garbage collection, boxing, and unboxing, see items 16 and 17
  • Take full advantage of interfaces and delegates, see items 19 though 22
  • Create CLS compliant assemblies that use noncompliant C# language features, see item 30
  • Improve reliability and maintainability by creating small, cohesive assemblies, see item 32
  • Leverage the full power of .NET’s runtime diagnostics, see item 36
  • Know when—and when not—to use reflection, see items 42 and 43
  • Preview the major enhancements in C# 2.0, see item 49
  • You’re already a successful C# programmer—this book can help you become an outstanding one.
Bill Wagner is co-founder of and .NET consultant for SRT Solutions.A nationally recognized independent expert on .NET, he has been aregular contributor to ASP.NET Pro Magazine, Visual Studio Magazine,and the .NET Insight newsletter. In addition to being a MicrosoftRegional Director, he is also active in the Southeast Michigan .NETUser Group and the Ann Arbor Computing Society. He is author of The C# Core Language Little Black Book (The Coriolis Group, 2002).
© Copyright Pearson Education. All rights reserved.
Excerpt. © Reprinted by permission. All rights reserved.
This book is designed to offer practical advice for the programmeron how to improve productivity when using the C# language and the .NETlibraries. In it, I have comprised 50 key items, or minitopics, relatedto the most-frequently-asked questions that I (and other C#consultants) have encountered while working with the C# community.
I started using C# after more than 10 years of C++ development, andit seems that many C# developers are following suit. Throughout thebook, I discuss where following C++ practices may cause problems inusing C#. Other C# developers are coming to the language with a strongJava background; they may find some of these passages rather obvious.Because some of the best practices change from Java to C#, I encourageJava developers to pay special attention to the discussions on valuetypes (see Chapter 1, “C# Language Elements”). In addition, the .NETGarbage Collector behaves differently than the JVM Garbage Collector(see Chapter 2, “.NET Resource Management”).
The items in this book are the collection of recommendations that Imost often give developers. Although not all items are universal, mostof the items can be easily applied to everyday programming scenarios.These include discussions on Properties (Item 1), ConditionalCompilation (Item 4), Immutable Types (Item 7), Equality (Item 9),ICloneable (Item 27), and the new Modifier (Item 29). It has been myexperience that, in most situations, decreasing development time andwriting good code are the primary goals of the programmer. Certainscientific and engineering applications may place the highest premiumon the overall performance of the system. Still, for others, it’s allabout the scalability. Depending on your goals, you might findparticular information more (or less) relevant under certaincircumstances. To address this, I have tried to explain the goals indetail. My discussions on readonly and const (Item 2), SerializableTypes (Item 25), CLS Compliance (Item 31), Web Methods (Item 34), andDataSets (Item 41) assume certain design goals. Those goals are spelledout in these items, so that you can decide what is most applicable foryou in your given situation.
Although each item in Effective C# stands alone, it isimportant to understand that the items have been organized around majortopics, such as C# language syntax, resource management, and object andcomponent design. This is no accident. My goal is to maximize learningthe material covered in the book by leveraging and building each itemon earlier items. Don’t let that keep you from using it as a reference,though. If you have specific questions, this book functions well as theideal “ask-me” tool. Please keep in mind that this is not a tutorial ora guide to the language, nor is it going to teach you C# syntax orstructure. My goal is to provide guidance on the best constructs to usein different situations.
Who Should Read this Book?Effective C# is written for professional developers, thoseprogrammers who use C# in their daily work lives. It assumes that youhave some experience with object-oriented programming and at least onelanguage in the C family: C, C++, C#, or Java. Developers with a VisualBasic 6 background should be familiar with both the C# syntax andobject-oriented design before reading this book. Additionally, youshould have some experience with the major areas of .NET: Web Services,ADO.NET, Web forms, and Windows Forms. I reference these conceptsthroughout the book.
To fully take advantage of this book, you should understand the waythe .NET environment handles assemblies, the Microsoft IntermediateLanguage (MSIL), and executable code. The C# compiler producesassemblies that contain MSIL, which I often abbreviate as IL. When anassembly is loaded, the Just In Time (JIT) Compiler converts that MSILinto machine-executable code. The C# compiler does perform someoptimizations, but the JIT compiler is responsible for many moreeffective optimizations, such as inlining. Throughout the book, I’veexplained which process is involved in which optimizations. Thistwo-step compilation process has a significant effect on whichconstructs perform best in different situations.
About the ContentChapter 1,”C# Language Elements,” discusses the C# syntax elementsand the core methods of System.Object that are part of every type youwrite. These are the topics that you must remember every day when youwrite C# code: declarations, statements, algorithms, and theSystem.Object interface. In addition, all the items that directlyrelate to the distinction between value types and reference types arein this chapter. Many items have some differences, depending on whetheryou are using a reference type (class) or a value type (struct). Istrongly encourage you to read the discussions on value and referencetypes (Items 6 through  before reading deeper into the book.
Chapter 2, “.NET Resource Management,” covers resource managementwith C# and .NET. You’ll learn how to optimize your resource allocationand usage patterns for the .NET managed execution environment. Yes, the.NET Garbage Collector makes your life much simpler. Memory managementis the environment’s responsibility, not yours. But, your actions canhave a big impact on how well the Garbage Collector performs for yourapplication. And even if memory is not your problem, nonmemoryresources are still your responsibility; they can be handled throughIDisposable. You’ll learn the best practices for resource management in.NET here.
Chapter 3, “Expressing Designs with C#,” covers object-orienteddesign from a C# perspective. C# provides a rich palette of tools foryour use. Sometimes, the same problems can be solved in many ways:using interfaces, delegates, events, or attributes and reflection.Which one you pick will have a huge impact on the futuremaintainability of your system. Choosing the best representation ofyour design will help to make it easier for the programmers using yourtypes. The most natural representation will make your intent clearer.Your types will be easier to use and harder to misuse. The items inChapter 3 focus on the design decisions you will make and when each C#idiom is most appropriate.
Chapter 4, “Creating Binary Components,” covers components andlanguage interoperability. You’ll learn how to write components thatcan be consumed by other .NET languages, without sacrificing yourfavorite C# features. You’ll also learn how to subdivide your classesinto components in order to upgrade pieces of your application. Youshould be able to release new versions of a component withoutredistributing the entire application.
Chapter 5, “Working with the Framework,” covers underutilizedportions of the .NET Framework. I see a strong desire in manydevelopers to create their own software rather than use what’s alreadybeen built. Maybe it’s the size of the .NET Framework that causes this;maybe it’s that the framework is completely new. These items cover theparts of the framework where I have seen developers reinvent the wheelrather than use what they’ve been given. Save yourself the time bylearning how to use the framework more efficiently.
Chapter 6, “Miscellaneous,” finishes with items that did not fit inthe other categories and with a look forward. Look here for C# 2.0information, standards information, exception-safe code, security, andinterop.
A Word About the ItemsMy vision for these items is to provide you with clear and succinctadvice for writing C# software. Some guidelines in the book areuniversal because they affect the correctness of a program, such asinitializing data members properly (see Chapter 2). Others are not soobvious and have generated much debate in the .NET community, such aswhether to use ADO.NET DataSets. While I personally believe that usingthem is a great timesaver (see Item 41), other professionalprogrammers, whom I highly respect, disagree. It really depends on whatyou’re building. My position comes from a timesaving stance. For otherswho write a great deal of software that transfer information between.NET- and Java-based systems, DataSets are a bad idea. Throughout thebook, I support and have given justification for all the suggestions Imake. If the justification does not apply to your situation, neitherdoes the advice. When the advice is universal, I usually omit theobvious justification, which is this: Your program won’t work otherwise.
Regarding C# 2.0I say little about the upcoming C# 2.0 release; there are tworeasons for this. First and foremost, most of the advice in this bookapplies just as well for C# 2.0 as it does for the current version.Although C# 2.0 is a significant upgrade, it is built on C# 1.0 anddoes not invalidate most of today’s advice. Where the best practiceswill likely change, I’ve noted that in the text.
The second reason is that it’s too early to write the most effectiveuses of the new C# 2.0 features. This book is based on the experienceI’ve had–and the experience my colleagues have had–using C# 1.0. Noneof us has enough experience with the new features in C# 2.0 to know thebest ways to incorporate them into our daily tasks. I’d rather notmislead you when the simple fact is that the time to cover the new C#2.0 features in an Effective book has not yet arrived.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-11-23 00:05

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表