http://www.developer.com/net/csharp/article.php/3801306/C-Tips-and-Tricks.htm
As the title indicates, this article will be about C# tips and tricks. I'll cover some of the C# 3.0 language features, explore productivity gains with the Visual Studio 2008 C# IDE, and mention a couple of handy Visual Studio plug-ins that may be of interest. This deviates from the traditional articles I've presented in the past and will hopefully provide some value in its own way. C# 3.0 was released as a part of the Microsoft .NET 3.5 Framework. The main purpose was to provide a foundation for Language INtegrated Query (LINQ) to provide unified data access capability. Each enhancement also can be used on its own. Here is a list of the features you'll cover and learn some related tips: Automatically implemented properties offer a concise syntax for implementing property accessors that get and set a private field. They create a field backed property without requiring you to actually create a field. The compiler automatically generates it for you at compile time. They can be used only for simple get and set of properties because there is no body and no field. No property body means no breakpoints or validation logic can be used. No field means no default value. Typing "prop [tab][tab]" is the Visual Studio code snippet for producing an automatically implemented property. This can make your code much more concise. A classic example of a field back property might look something like the code below. A simpler example that uses automatically implemented properties for the same field is below. However, it is important not to be too lazy and use this haphazardly. Examine the code below where you define a Customer class. Take note of the CustomerKey property that is defined. Typically, such a key field is a readonly item that is set once and doesn't change. When taking a shortcut using automatically implemented properties, it allows for undesired behavior with the code. You might try refactoring the code to have a private set and use a constructor to control the value. Although this does protect the field from the outside, it could still be altered internally to the class. In reality, an automatically implemented property probably isn't appropriate for the CustomerKey property and the code should look like the example below. The class in question in this example is pretty simple, but it illustrates the point that the automatically implemented properties should be applied liberally, but with caution. There is a prior article that is dedicated solely to the using keyword. Even though this isn't necessarily new, it is worth mentioning and at least pointing to that article for more detail. The main benefit is that it can be used to implement try ... finally behavior and offers an automatic cleanup of disposable types. There is a lot of value to be had by implementing this in your code whenever objects such as DataReader are used to ensure that connections will be properly disposed when complete. Local type inference was also the subject of another prior article, but again is worth mentioning here as it is very handy to have a solid understanding. You define variables with the var keyword and the compiler will figure out the type for you at compile time and replace it. It is great for results from query expressions as show in the example below. It may appear that it is untyped or even give the impression it is a variant. It is not. It is a strongly typed object whose type is set at compile time. Initializers introduce a concise syntax that combines object creation and initialization in a single step. It even allows for parentheses to be optional for paramaterless constructors as you'll see in the first line of the example below. This functionality is great for demos where you are building collections of objects to do things like LINQ queries against. However, I caution you to consider real life initialization of large sets of data and whether code over a database is the proper place to store such information. Initializers work very well with constructors. The example below shows an instance where a parameterized constructor is called along with remaining properties initialized. The constructor should commonly take all of the parameters so it isn't an ideal setup, but it illustrates the point well. Extension methods allow you to create new functionality on existing types. Even if the types are sealed. They are declared like static methods and called like instance methods. They are valid for use on interfaces or constructed types. The trailing sample code illustrates the use of extension methods to add a Triple and Half method on the decimal type. Extension methods can be a very powerful tool. They can also be a very annoying tool if not used with some care. You should consider a separate namespace to allow them to be optional to the other developers that may be working within your codebase. Resist the temptation to put them on all objects by extending the object class. For consistency you should also make them behave like other instance methods. The following code sample illustrates very bad form that should not be practiced. The namespace is System, which means the scope of this extension is any code that comes in contact with it. It extends ALL objects. It also does not behave like a normal instance method. Normally calling an instance method on a null object would result in a NullReferenceException and not a Boolean true or false value. This is a code example of what is not advisable. I'm not going to cover much on LINQ here as those are articles in and of themselves. Here is a LINQ performance tip that I experienced first hand on a project. Consider carefully the use of Table.ToList().Find(). Table.Where() is commonly a better option. ToList().Find() returns all records from a table and then runs the filter in memory. Table.Where() is performed in SQL and returns only the rows requested. An additional tip is there is a handy tool known as LINQPad that allows you to perform interactive queries of databases using LINQ. It is a code snippet IDE that will execute any C# expression or statement block, which is very handy for testing out LINQ. The Visual Studio IDE offers a vast number of productivity gains when you have the IDE configured with a C# developer profile. Visual Studio is chalk full of hundreds of coding shortcuts. I'll call out a few of the ones that I've found to be most helpful. Karen Liu's blog (Microsoft's C# IDE Program Manager) has many, many more useful shortcuts available. Have you experienced a scenario when you have you cut or copied a line of code only to discover another line of code that you also cut or copy? The original code copied is then lost from the copy buffer and you must undo changes to get back to the first change. The clipboard ring can be your new best friend if this is a common occurrence for you. You cut or copy multiple lines of code independently. Ctrl + Shift + V will allow you to cycle through the code in the clipboard ring. If you are going to use the clipboard ring it is a good idea to consider Tools > Options > Text Editor > All Languages > General and set the "Apply Cut or Copy commands to blank lines when there is no selection" option. Code snippets are ready-made task-oriented blocks of code. Visual Studio includes a number of default snippets that are activated by typing a specific sequence of keys followed by [tab][tab] to cause the Visual Studio IDE to engage. In addition to typing the snippet in code, you can right-click at the desired location and choose Insert Snippet or Surround With. Here is a very small sampling of what is available. Microsoft has a number of additional code snippets available for download. Additionally, there is a free Snippet Designer that allows you to easily create or modify your own code snippets. Visual Studio 2008 Service Pack 1 is full of all kinds of goodies that make it worth downloading and installing. The download will take a while, but definitely worth it. Here is a very limited taste of what is included. You have now seen various tips and tricks related to items within C# 3.0 and the Visual Studio 2008 C# IDE. Hopefully this has provided you some useful insight in to ways you can enhance your C# development experience. The topic of the next article is likely to be on the Entity Framework. You'll explore what it is and how it compares to LINQ. If you have something else in particular that you would like to see explained here, you could reach me at mark.strawmyer@crowehorwath.com. Mark Strawmyer is an executive with Crowe Horwath LLP in the Indianapolis office. He can be reached at mark.strawmyer@crowehorwath.com.
C# Tips and Tricks
February 9, 2009
C# 3.0 Background
Automatically Implemented Properties
private int myProperty = 0;public int MyProperty{ get { return this.myProperty; } set { this.myProperty = value; }}public int MyProperty{ get; set;}class Customer{ public string CustomerKey { get; set; } public string ContactName { get; set; } public string City { get; set; }}class Customer{ public Customer(string customerKey) { CustomerKey = customerKey; } public string CustomerKey { get; private set; } public string ContactName { get; set; } public string City { get; set; }}class Customer{ public Customer(string customerKey) { this.CustomerKey = customerKey; } private readonly string customerKey; public string CustomerKey { get return this.customerKey; } public string ContactName { get; set; } public string City { get; set; }}using Keyword
using (IDataReader reader = provider.ExecuteReader(dataCmd)){ // ...}Local Type Inference
var i = 5;int i = 5;string[] words = { "cherry", "apple", "blueberry" };var sortedWords = from w in words orderby wObject and Collection Initializers
Point p = new Point { X=3, Y=99 };Student myStudent = new Student() { Name = "John Doe", Class = "Senior", School = "Carmel" };class Customer{ public Customer(string customerKey) { ... } public string CustomerKey { get { ... } } public string ContactName { get; set; } public string City { get; set; }}var c = new Customer("MARKS"){ ContactName = "Mark Strawmyer", City = "Indianapolis"}Extension Methods
static class ExtensionSample{ public static decimal Triple( this decimal d ) { return d*3; } public static decimal Half( this decimal d ) { return d / 2; }}decimal y = 14M;y = y.Triple().Half();namespace System{ public static class MyExtensions { public static bool IsNull(this object o) { return o == null; } }}while (!myObject.IsNull()) { ... }LINQ Performance Tip
Productivity Gains with the Visual Studio C# IDE
Coding Shortcuts
Clipboard Ring
Code Snippets
Visual Studio 2008 Service Pack 1
Summary
Future Articles
About the Author