Entries tagged 'csharp'

Book: More Effective C#

Wednesday, January 28 2009         No Comments

MoreEffectiveCSharp More Effective C# by Bill Wagner is a follow-up to the 2005 book Effective C# and includes 50 additional ways to improve your C# code.

More Effective C# was published in 2008 and is current through C# 3.0 (.NET 3.5) including sections on the C# 3.0 language enhancements and LINQ.

This book should be on your desk, right next to Effective C#! As the back cover says, "You're already a successful C# programmer - this book can help you become an outstanding one".

Read more....

Book: Effective C#

Wednesday, January 28 2009         No Comments

EffectiveCSharp Effective C# by Bill Wagner is a great book which "... identifies fifty ways you can start leveraging the full power of C# in order to write faster, more efficient, and more reliable software".

Originally published in 2005, Effective C# covers tips applicable to C# 1.0 (.NET 1.x), with a few brief glimpses at .NET 2.0.

More Effective C#, released in 2008, includes 50 more tips and is current for C# 3.0 (.NET 3.5).

You will learn a lot about C# by reading this book and truly understanding the reasoning behind each of the tips - some are as simple as "prefer ForEach loops" to the more complex "prefer declarative to imperative programming".

Every serious C# programmer should have a copy of this book!

Read more....

Book: C# in Depth

Tuesday, January 06 2009         No Comments

C# in Depth cover C# in Depth, by Jon Skeet is an excellent book for existing C# developers who want to take their skills to the next level.

This book is a great resource for experienced beginners who want to gain a deeper understanding of the "hows" and "whys" of some of the more advanced features of the language and even proficient developers will learn many of the finer nuances of C#.

Part I of the book provides some background and history on the initial development of C# and also reviews some of the key features of the language.

Part II discusses the enhancements made in C# 2.0 (which was released along with Visual Studio 2005 and .NET 2.0). These features are introduced in such a way that shows the natural evolution of the language and how the features solved issues or eased development compared to C# 1.0.

Part III then applies the same approach to introducing the many new language features in C# 3.0 (which was released along with Visual Studio 2008 and .NET 3.5).

What I really like about this book is how it presents the features of C# as an evolution and explains not just how to use the features, but why the were added in the first place. For those of us who have been working with C# since the beginning, it is very refreshing to take a step back and look at the big picture and see how we got to where we are. For the beginning developer, it's always helpful to understand the context in which a feature is introduced and what types of problems it was intended to solve.

- Ken

Implicit Typing - The C# var Keyword

Monday, January 05 2009         No Comments

Introduced with C# 3.0 (used in Visual Studio 2008, .NET 3.5 and later), the C# var keyword allows for the implicit typing of local variables.

For example, if I wanted to create a generic dictionary without implicit typing:

image

With implicit typing, I simply replace the variable type with the var keyword:

image

This simply instructs the C# compiler to figure out the type of the "parameters" variable based on what it is assigned to.

It's important to note that this does not imply a variant type or that "parameters" is not strongly typed. The two chunks of code above are equivalent, it's just that in the second one I'm letting the compiler do the work and saving myself some keystrokes.

Restrictions

Since the compiler is doing the work of determining the type of the variable being declared, it's not valid to use var without providing for the initialization of the variable:

image 

Another restriction on using var are that it can only be used on local variables. Variables scoped at the class level cannot use var even if they are initialized when declared:

image

Purpose

The main purpose of the var keyword is to enable the use of anonymous types. Anonymous types are types whose definition is generated by the C# compiler - so you as the programmer don't have access to the name of the class! Therefore you can't create a variable to reference an instance of the class without using var. For example:

image

In this case I don't have to know the specific type returned by my query - and in this case I couldn't specify it anyway because the compiler is generating an anonymous type for me that holds CompanyName and ContactName. So I must use var in this case to refer to my result, and the compiler does the rest.

Guidelines

Use of var is always optional, unless you are dealing with anonymous types. But many developers are using it where they can to simplify and reduce the amount of code they have to write. The key here is to keep the readability of your code in mind. If usage of var in a situation introduces ambiguity to the code - don't use it in that situation!

- Ken