Monday, April 7, 2008

Resource Files

A resource file is a sort of container in which you can add things that will get compiled into your dll when you build your project. Once you add an item to the resource file, a static property is created on the resource file so you can access it in your code.
Using a Resource File:
1. Right-click on your project and select Add > New Item... .
2. Select "Resource File" in the dialog box and click OK.
3. Add a string to the resource file.
4. Now you can reference this new string in code like this:
command.CommandText = string.Format(Resource1.PeopleQuery, "'VT'");

More Info: MSDN: Adding and Editing Resources

Sunday, April 6, 2008

Class and Collection Initializers

Initializers allow you to set properties of a class or populate a collection in the same line that you instantiate your class/collection.

Classes
public class Person 
{
     public string First { get; set; }
     public string Last { get; set; } 
}
Before:
Person person = new Person(); 
person.First = "Mark"; 
person.Last = "Moeykens"; 
After:
Person person = new Person(){Last="Moeykens", First="Mark"};
(Notice the properties do not have to be in any order.)

Collections
Before:
List people = new List(); 
people.Add("Moeykens"); 
people.Add("Major"); 
people.Add("Hayek"); 
After:
List<string> people = new List<string>() {"Moeykens", "Major", "Hayek"};
Collection of Classes
Before:
List people = new List(); 
people.Add(new Person() {First="Mark", Last="Moeykens"}); 
people.Add(new Person() {First="Dean", Last="Major"}); 
people.Add(new Person() {First="George", Last="Hayek"}); 
After:
List people = new List() 
{
     new Person() {First="Mark", Last="Moeykens"},
     new Person() {First="Dean", Last="Major"},
     new Person() {First="George", Last="Hayek"} 
}; 
More Info: MSDN: Object and Collection Initializers

Saturday, April 5, 2008

Generics

Generics allows you to make an object a specific type. This allows intellisense to work on the object; which will also do type checking at compile time (meaning it won't build if you are trying to do something like assign an int to a string).

Before:
ArrayList people = new ArrayList();
people.Add("Moeykens");

string person = (string)people[0];
// Note: Without casting to string, you 
// will get error: "Cannot convert source 
// type 'object' to target type 'string'"
After:
List<string> people = new List<string>(); 
people.Add("Moeykens"); 
string person = people[0]; 

Compare the tooltips for each Add method.
Before:

After:


More Info:MSDN: Generics

Friday, April 4, 2008

Anonymous Types

Anonymous types (classes) allow you to create a class with members without actually defining a class first. You saw a small example of this under the var Variable post.

Before:
public class Person() 
{     
     // Auto Implemented Properties
     public string FirstName { get; set; }
     public string LastName { get; set; } 
} 

Person person = new Person(); 
person.FirstName = "Mark"; 
person.LastName = "Moeykens";
After:
var person = new {
                     FirstName="Mark", 
                     LastName="Moeykens"
                 }; 

// The FirstName member will appear in intellisense
string firstName = person.FirstName; 
Notice "FirstName" and "LastName" are being initialized within the braces. This is a new feature of C# 3.0 called Object Initializing. The variable 'person' will also have full intellisense.

More Info: MSDN: Anonymous Types

Thursday, April 3, 2008

Partial Classes

Partial classes allow you to break out a class into multiple pieces, usually this is done in multiple files. Why would you want to do this? Well, have you ever seen a really cluttered class that is about 1,000 lines long? Have you ever opened up a Windows Form that had so many controls and many events on those controls that the code file was incredibly long and you spent a lot of time scrolling?

That is why.

It also allows Microsoft to separate out all their auto-generated code so you never have to see it again.

Before:
// MyClass.cs
public class MyClass 
{
      // Line 1
      public void DoWorkHere() { }

      public void DoWorkThere() { }
      // Line 1,026 
}
After:
// MyClass.DoWorkHere.cs
public partial class MyClass 
{
      // Line 1
      public void DoWorkHere() { }
      // Line 513 
} 

// MyClass.DoWorkThere.cs
public partial class MyClass 
{
      // Line 1
      public void DoWorkThere() { } 
     // Line 513 
}
More Info:MSDN: Partial Classes

Wednesday, April 2, 2008

Nullable Types

Value types (such as int) cannot be made null. This code:
int age = null;
will produce an error that says, Cannot convert source type 'null' to target type 'int'.

Now you can convert value types to a nullable type by using a question mark (?) right after the type declaration:
int? age = null; 
if (age != null) DoSomething(); 
//or 
if (age.HasValue) DoSomething(); 
Nullable types expose a new boolean HasValue property.

Now you don't have to agree upon a "standard" representation of a null condition, invalid value or an uninitialized value for your reference types. Like saying, everytime you see a -1 for this int, it means nothing was returned or that your int has no value.

More Info: MSDN: Nullable Types

Tuesday, April 1, 2008

String.IsNullOrEmpty method

Returns true if the string passed in is null or empty. Otherwise returns false.
Before:
if (name == null || name == string.Empty)) 
{
     return "Please enter a name."; 
}
After:
if (string.IsNullOrEmpty(name)) 
{     
    return "Please enter a name."; 
}
More Info: MSDN: String.IsNullOrEmpty