“Effective C#” Brendan Duffy ++ And a little VB. dotNET solutions Wagner Effective C# Addison...
-
Upload
baldwin-reeves -
Category
Documents
-
view
220 -
download
0
Transcript of “Effective C#” Brendan Duffy ++ And a little VB. dotNET solutions Wagner Effective C# Addison...
“Effective C#”
Brendan Duffy
++And a little VB
dotNET solutions
Wagner
• Effective C#• Addison Wesley• 300 pages
dotNET solutions
If you're interested in, or currently working with, .NET and are tempted to skip past this book as Just Another C# Reference Book, think again. Wagner's book is a great resource because it covers concepts which run across the entire .NET Framework regardless of which language you're working with. While this book focuses on C#, VB.NET developers can benefit from some of the text within as well.
Excerpts from Jim Holmes review, Dayton .NET Users Group
dotNET solutions
I also think this book speaks to a wide range of readers. Seasoned developers will blow through this content, fine-tuning their coding methods or starting new ones. …New developers also can greatly benefit from this book by using it to properly form development habits early in their careers.
Excerpts from Jim Holmes review, Dayton .NET Users Group
dotNET solutions
Wagner's writing style is clear and concise. He occasionally comes across as brusque … but those instances are few and far between. The rest of the book's voice is terrific. More important is the weight of Wagner's knowledge and experience. One real drawback is a large number of typographical errors, … I would have expected better proofreaders at AW.
Excerpts from Jim Holmes review, Dayton .NET Users Group
dotNET solutions
The book is well organized into six chapters, each hitting a specific area in C#. Within each chapter, Wagner covers six to ten items, each item focusing on one specific "mini topic," as Wagner calls them. Each item includes code snippets to demonstrate recommended approaches.
Excerpts from Jim Holmes review, Dayton .NET Users Group
dotNET solutions
Balena and Dimauro
• Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers
• Microsoft Press• 570 pages
dotNET solutions
This book, which is language agnostic, contains a large number of items, which ranging from a brief few lines to several pages in length. The breadth of the coverage is extensive and allows easy scanning. The guidelines are easy to understand with a limited number that are debateable. Almost all items include a concise paragraph outlining why the practice is recommended. The writing style is clear and avoids the cookbook style, only including code snippets where needed.
dotNET solutions
“Most recommendations are very practical and can easily be applied immediately. In addition many of the suggestions are put in context with a brief discussion over the pros and cons. … The book's tailored towards practicality. It is simply structured, uses basic and direct language and takes clear positions. That makes it easy and fun to read - yet it provides remarkably good information, not firstly on grand concepts, but on many small things that often get overlooked. It's the sort of book you take where ever you go when you just have a couple minutes to read. Open a random page and always get a little ‘aha’”.
Excerpt from an Amazon review
dotNET solutions
Excerpt from an Amazon review
“This is an intermediate level book. It is intended for the programmer who knows the language but is moving up to bigger projects. … Your own experience will likewise lead you to programming methods that may differ from their Guidelines. … On the whole, any programmer reading this book will come away with some guidelines of the ‘Gee, why didn't I think of that’ variety.”
dotNET solutions
Always use _____ instead of accessible data members.
a) Variables
b) Properties
c) Methods
d) Delegates
Effective C#, Wagner, p 1
dotNET solutions
The Option Strict directive.
When should you use Option Strict Off?
Practical Guidelines and Best Practices, Balena & Dimauro, p 10
Projects and Solutions, 23 items
dotNET solutions
Prefer _____ to const.
a) set
b) mutable
c) get
d) readonly
Effective C#, Wagner, p 12
dotNET solutions
Line Wrapping.
What is the shortcut key for toggling word wrapping in VS?
Practical Guidelines and Best Practices, Balena & Dimauro, p 14
Spaces and Indentation, 8 items
dotNET solutions
Prefer the is or as operators to _____ .
a) equivalence
b) assignment
c) casts
d) down casting
Effective C#, Wagner, p 17
dotNET solutions
TODO and HACK comment tokens.
How do you toggle display of these items in the task list window?
Practical Guidelines and Best Practices, Balena & Dimauro, p29
Comments, 19 items
dotNET solutions
Use _____ instead of #if.
a) #define
b) Conditional attributes
c) String formatting
d) Regex expressions
Effective C#, Wagner, p 25
dotNET solutions
CLS compliant assemblies
Does VB play well with this attributte?
Practical Guidelines and Best Practices, Balena & Dimauro, p 33
Assemblies and Resources, 14 items
dotNET solutions
Always provide _____ ( ) .
a) Equals
b) GetHashCode
c) ToString
d) MemberwiseClone
Effective C#, Wagner, p 31
dotNET solutions
First level namespace.
What should you use as a first level namespace?
Practical Guidelines and Best Practices, Balena & Dimauro, p 43
Namespaces, 12 items
dotNET solutions
_____ value and reference types .
a) Convert between
b) Distinguish between
c) Derive from
d) Overload constructors for
Effective C#, Wagner, p 38
dotNET solutions
Nested types.
Should they be public?
Practical Guidelines and Best Practices, Balena & Dimauro, p 53
Types, 24 items
dotNET solutions
Prefer _____ value types.
a) Nullable
b) Transactional
c) Polymorpic Granular
d) Immutable atomic
Effective C#, Wagner, p 44
dotNET solutions
Base classes vs interfaces.
Which should you favour?
Practical Guidelines and Best Practices, Balena & Dimauro, p 59
Inheritance, 21 items
dotNET solutions
Understand the pitfalls of _____ .
a) ToString
b) GetHashCode
c) Equals
d) MemberwiseClone
Effective C#, Wagner, p 63
dotNET solutions
Interface immutability.
Are there exceptions?
Practical Guidelines and Best Practices, Balena & Dimauro, p 77
Interfaces, 19 items
dotNET solutions
Ensure that _____ is a valid state for reference types.
a) 0
b) null
c) NaN
d) Error
Effective C#, Wagner, p 51
dotNET solutions
Equals overload in value types.
Is this a performance issue?
Practical Guidelines and Best Practices, Balena & Dimauro, p 97
Structures, 7 items
dotNET solutions
Prefer _____ loops.
a) do
b) for
c) foreach
d) while
Effective C#, Wagner, p 70
dotNET solutions
Member names in an enum type.
Should they have a common prefix?
Practical Guidelines and Best Practices, Balena & Dimauro, p 102
Enum Types, 13 items
dotNET solutions
Prefer variable _____ to assignment statements.
a) methods
b) properties
c) initializers
d) modifiers
Effective C#, Wagner, p 82
dotNET solutions
Invoking a method through reflection.
What is the most efficient way to achieve this?
Practical Guidelines and Best Practices, Balena & Dimauro, p 114
Attribute Types and Reflection, 12 items
dotNET solutions
Utilize constructor _____ .
a) chaining
b) Return values
c) Default parameters
d) linking
Effective C#, Wagner, p 87
dotNET solutions
Hungarian notation.
What is the real purpose of hungarian notation?
Practical Guidelines and Best Practices, Balena & Dimauro, p 116
Fields and Variables, 27 items
dotNET solutions
Utilize _____ for resource cleanup.
a) destructors
b) dispose
c) using
d) finalizers
Effective C#, Wagner, p 93
dotNET solutions
Properties that return arrays.
How do you make the elements of an array immutable?
Practical Guidelines and Best Practices, Balena & Dimauro, p 136
Properties, 14 items
dotNET solutions
Minimize _____.
a) Garbage
b) Disposes
c) Generations
d) instances
Effective C#, Wagner, p 100
dotNET solutions
Factory method names.
How should you name factory methods?
Practical Guidelines and Best Practices, Balena & Dimauro, p 154
Methods, 35 items
dotNET solutions
Minimize _____.
a) Stack size
b) structs
c) Early-binds
d) boxing
Effective C#, Wagner, p 103
dotNET solutions
Calling virtual methods from inside a constructor.
Will this work?
Practical Guidelines and Best Practices, Balena & Dimauro, p 175
Constructors, 19 items
dotNET solutions
Implement the standard _____ pattern.
a) Dispose
b) Garbage Collection
c) Finalize
d) Resurrection
Effective C#, Wagner, p 109
dotNET solutions
Dispose vs Close method.
When should you use the Close method name instead of Dispose?
Practical Guidelines and Best Practices, Balena & Dimauro, p 180
Dispose and Finalize methods, 11 items
dotNET solutions
Prefer defining and implementing interfaces to _____.
a) inheritance
b) polymorphism
c) encapsulation
d) abstraction
Effective C#, Wagner, p 118
dotNET solutions
Events in serializable classes
Why do you need to Implement ISerializable in this case?
Practical Guidelines and Best Practices, Balena & Dimauro, p 201
Delegate and Events, 23 items
dotNET solutions
Express callbacks with _____ .
a) Unsafe code
b) events
c) Delegates
d) interop
Effective C#, Wagner, p 129
dotNET solutions
Go to keyword.
Where is this OK to use?
Practical Guidelines and Best Practices, Balena & Dimauro, p 214
Execution Flow, 26 items
dotNET solutions
Define outgoing interfaces with _____ .
a) Events
b) delegates
c) MarshallByRef
d) Sinks
Effective C#, Wagner, p 131
dotNET solutions
Rethrowing original exceptions.
How is this done, and where?
Practical Guidelines and Best Practices, Balena & Dimauro, p 230
Exception Handling, 29 items
dotNET solutions
Avoid _____ references to internal class objects.
a) creating
b) returning
c) disposing
d) Calling
Effective C#, Wagner, p 137
dotNET solutions
Conversion of boolean values to integers.
Why should this be avoided?
Practical Guidelines and Best Practices, Balena & Dimauro, p 243
Numeric Types, 17 items
dotNET solutions
Prefer _____ to imperative programming.
a) Procedural
b) Declarative
c) Generic
d) Templated
Effective C#, Wagner, p 142
dotNET solutions
Language-specific string functions. [VB]
Are the any disadvantages to using these?
Practical Guidelines and Best Practices, Balena & Dimauro, p 251
Strings, 25 items
dotNET solutions
Prefer _____ types.
a) Serializable
b) Synchronized
c) Transactional
d) Hydrated
Effective C#, Wagner, p 148
dotNET solutions
Large arrays of boolean values.
What is a more memory-efficient type?
Practical Guidelines and Best Practices, Balena & Dimauro, p 270
Arrays and Collections, 21 items
dotNET solutions
Implement _____ relationships with IComparable and IComparer.
a) ordering
b) predicate
c) logical
d) equivalence
Effective C#, Wagner, p 156
dotNET solutions
Weak reference objects.
How should you use and expose these objects?
Practical Guidelines and Best Practices, Balena & Dimauro, p 288
Memory Usage, 12 items
dotNET solutions
Avoid _____.
a) ISerializable
b) ICloneable
c) IList
d) ISortable
Effective C#, Wagner, p 163
dotNET solutions
DateTime serialization.
What strategies can be used to prevent time-zone shifts?
Practical Guidelines and Best Practices, Balena & Dimauro, p 309
Files, Streams and XML, 14 items
dotNET solutions
Avoid _____ operators.
a) overloaded
b) ternary
c) bitwise
d) conversion
Effective C#, Wagner, p 167
dotNET solutions
Signed interop assembles.
How do you generate a strong-named interop assembly?
Practical Guidelines and Best Practices, Balena & Dimauro, p 314
PInvoke and COM Interop, 19 items
dotNET solutions
Prefer CLS compliant _____.
a) Properties
b) Methods
c) Classes
d) Assemblies
Effective C#, Wagner, p 181
dotNET solutions
Asynchronous delegates.
How are these used?
Practical Guidelines and Best Practices, Balena & Dimauro, p 340
Threading, 23 items
dotNET solutions
Prefer small, simple _____.
a) functions
b) assemblies
c) identifiers
d) comments
Effective C#, Wagner, p 186
dotNET solutions
Global error handlers.
How do you implement these?
Practical Guidelines and Best Practices, Balena & Dimauro, p 367
Windows Forms Applications, 37 items
dotNET solutions
Prefer smaller, cohesive _____.
a) metadata
b) methods
c) assemblies
d) properties
Effective C#, Wagner, p 190
dotNET solutions
Batch commands for SQLServer queries.
How does the SQLDataReader work with a batch?
Practical Guidelines and Best Practices, Balena & Dimauro, p 393
ADO.NET Programming, 47 items
dotNET solutions
Limit _____ of your types.
a) instances
b) lifetime
c) scope
d) visibility
Effective C#, Wagner, p 194
dotNET solutions
Accepting uploaded files from the user.
What do you need to do to have the HTMLInputFile control work as expected?
Practical Guidelines and Best Practices, Balena & Dimauro, p 416
ASP.NET Web Forms Applications, 67 items
dotNET solutions
Create _____ Web APIs.
a) Atomic
b) Granular
c) Large-grain
d) Aggregated
Effective C#, Wagner, p 198
dotNET solutions
Support for proxy servers.
How should you implement this?
Practical Guidelines and Best Practices, Balena & Dimauro, p 468
ASP.NET Web Services, 13 items
dotNET solutions
Use the standard _____ mechanism.
a) exception
b) configuration
c) interop
d) reflection
Effective C#, Wagner, p 213
dotNET solutions
Static members.
Why should you avoid these?
Practical Guidelines and Best Practices, Balena & Dimauro, p 475
Serviced Components, 21 items
dotNET solutions
Utilize and support _____.
a) Validation
b) Standard registry keys
c) Server side cursors
d) Data binding
Effective C#, Wagner, p 217
dotNET solutions
Exchanging data between different time zones.
What is the best way of doing this?
Practical Guidelines and Best Practices, Balena & Dimauro, p 506
Remoting, 20 items
dotNET solutions
Prefer _____ to custom structures.
a) xmldatadocuments
b) xmldocuments
c) datasets
d) classes
Effective C#, Wagner, p 237
dotNET solutions
The AllowPartiallyTrustedCallers attribute.
When should this be used?
Practical Guidelines and Best Practices, Balena & Dimauro, p 510
Security, 27 items
dotNET solutions
Utilize _____ to simplify reflection.
a) dllimport
b) attributes
c) interop
d) lutz
Effective C#, Wagner, p 246
dotNET solutions
Don’t overuse _____.
a) using
b) logging
c) reflection
d) interop
Effective C#, Wagner, p 253
dotNET solutions
Create complete _____ exception classes.
a) Generic, reusable
b) Application specific
c) Demand protected
d) MarshalByRef
Effective C#, Wagner, p 248
dotNET solutions
Prefer the _____ exception guarantee.
a) Focused
b) Weak
c) Global
d) Strong
Effective C#, Wagner, p 265
dotNET solutions
Prefer _____.
a) Unsafe code
b) Safe code
c) interop
d) Pointers
Effective C#, Wagner, p 277
dotNET solutions
C# - new features
• Generics• Nullable Types• Delegate Changes & Anonymous Methods• Accessor Accesibility• Iterators• Static Classes and Static Class Members• Partial Class Definitions• Friend Assemblies• Fixed Size Buffers
dotNET solutions
Generics
• Generic Classclass SortedList<T> : where T:Icomparable<T>{ … }
• Generic Methods void Swap<T>(ref T l, ref T r){
T temp; temp = lhs; lhs = rhs; rhs = temp; } • Generic Interfaces• Generic Delegates
dotNET solutions
Nullable Types
• Used to create value types that can contain an undefined state.int? x = null;
• The HasValue property returns true if the variable is not null
dotNET solutions
Delegate Changes
• You can assign an anonymous method directly to a delegatebutton1.Click += delegate {
MessageBox.Show( "Click!") };
• You can declare a delegate using this simplified syntax: MyDelegate<int> d = fn;
• Delegates now allow – covariant return types– Contravariant parameter types
dotNET solutions
Accessor Accessibility
public string Name
{
get { return name; }
protected set { name = value; }
}
dotNET solutions
Iterators
public class DaysOfTheWeek{ string[] m_Days = {"Su","Mo","Tu”,"We","Th","Fr","Sa"};
public IEnumerator GetEnumerator(){ foreach (string day in m_Days)
yield return day; } }
dotNET solutions
Static Classes & Static Members
• No longer need to use private constructors to prevent instance construction.
dotNET solutions
Partial Class Definitions
• To split a class definition, use the partial keyword modifier public partial class MyClass
{ public void method1() { } }
• For code generators.
• May help where multiple programmers work on a class simultaneously.
dotNET solutions
Friend Assemblies
To expose AssyA to AssyB (only)
• Use the InternalsVisibleTo Attributte
• Must use /out switch on AssyA
• Not transitive or reflective
dotNET solutions
Fixed Size Buffers
• Allows allocation of a fixed buffer on the stack.
public struct MyArray
{
public fixed char pathName[128];
}
• Must appear in an unsafe block
dotNET solutions
VS2005 C# Samples
• Base Class Library
• Data Access
• Web Development
• Windows Forms
• http://msdn.microsoft.com/vs2005
dotNET solutions
Base Class Library
• ACLChange• Compression• Console• DriveInfo• FTP• Generics• Network• RegularExpressions• Stopwatch
dotNET solutions
Data Access• Asynchronous Queries• Attaching a database with your application• Creating and using User Defined Types with SQL Server
2005• DataReader vs. DataSet comparision• DataSet and DataTable Enhancements• Performing Batch Updates and Data Paging• Performing Bulk Updates• Reading and Writing Images from a Database• Using Factory Classes• Using Managed SPs and UDFs with SQL Server 2005• Using Multiple Active Result Sets with SQL Server 2005• Using Notifications with SQL Server 2005• Using the XML data type with SQL Server 2005• XPath and XSLT Transformations Enhancements
dotNET solutions
Web Development
• Caching• DataAccess• DataControls• MasterPages• Membership• MenuAndSiteMapPath• Profiles• Security• TreeView• WebParts
dotNET solutions
Windows Forms• AsynchronousTasks• ClientConfiguration• CreatingMasterDetails• PlayingSounds• UsingBindingNavigator• UsingBindingSource• UsingClickOnce• UsingDataGridView• UsingLayoutPanels• UsingMaskedTextBox• UsingMenusStatusStripsToolStrips• UsingSplitContainer• UsingWebBrowser
dotNET solutions
Access Control List Sample
• The System.Security.AccessControl namespace provides a way to create and modify an ACL.
• ACLChange.exe
dotNET solutions
Compression Sample
The System.IO.Compression namespace
• Allows you to compress and decompress files in the GZip format.
• Compression.exe
dotNET solutions
Console Sample
• Framework 2.0 adds a number of new features.
• ConsoleSample.exe
dotNET solutions
DriveInfo Sample
• Provides an easy way to access the properties of all the drives on a system.
• DriveInfoSample.exe
dotNET solutions
FTP Sample
• The System.Net namespace adds full support for the FTP protocol
• FTPSample.exe
dotNET solutions
Generics Sample
The System.Collections.Generic namespace
• Allows flexible reusable code using generic types.
• Provides generic collection classes for type safe collections.
• GenericsSample.exe
dotNET solutions
NetworkInfo, NetworkChange and Ping Sample
• The new NetworkInfo class provides access to a wealth of network related information
• The NetworkChange class provides notification when the IP address of a network interfaces changes.
• The Ping classes provide a way to determine whether a remote computer is accessible.
• NetworkSample.exe
dotNET solutions
Regular Expressions Sample
• The System.Text.RegularExpressions namespace make it easy to use the regular expression syntax to perform complex find and parse operations on text.
• RegularExpressions.exe
dotNET solutions
Stopwatch Sample
• The System.Diagnostics now includes a stopwatch that can be used for timing operations.
• StopwatchSample.exe