There are a couple of very good reasons to use partial classes. First, using partial classes enables the programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: a huge class file may signal a design fault, and refactoring may be required.
Second, and most compelling, you can use partial classes to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2008 for a Windows Form is kept separate from your business logic. This prevents developers from messing with the code that is used for the user interface. At the same time, it prevents you from losing your changes to the designer-generated code when you change the user interface.
Creating an Instance of a Class (Object Instantiation)
A class works like a template. To do anything useful, you need to use the template to create an actual object so that you can work with it. The process of creating an object from a class is known as
To instantiate the Contact
class defined earlier, you first create a variable of type Contact
:
Contact contact1;
At this stage, contact1
is of type Contact
, but it does not actually contain the object data yet. For it to contain the object data, you need to use the new keyword to create a new instance of the Contact
class, a process is known as
contact1 = new Contact();
Alternatively, you can combine those two steps into one, like this:
Contact contact1 = new Contact();
Once an object is instantiated, you can set the various members of the object. Here's an example:
contact1.ID = 12;
contact1.FirstName = 'Wei-Meng';
contact1.LastName = 'Lee';
contact1.Email = '[email protected]';
You can also assign an object to an object, like the following:
Contact contact1 = new Contact();
Contact contact2 = contact1;
In these statements, contact2
and contact1
are now both pointing to the same object. Any changes made to one object will be reflected in the other object, as the following example shows:
Contact contact1 = new Contact();
Contact contact2 = contact1;
contact1.FirstName = 'Wei-Meng';
contact2.FirstName = 'Jackson';
//---prints out 'Jackson'---
Console.WriteLine(contact1.FirstName);
It prints out 'Jackson' because both contact1
and contact2
are pointing to the same object, and when you assign 'Jackson' to the FirstName
property of contact2
, contact1
's FirstName
property also sees 'Jackson'.
Anonymous Types (C# 3.0)
C# 3.0 introduces a new feature known as
var book1 = new {
ISBN = '978-0-470-17661-0',
Title='Professional Windows Vista Gadgets Programming',
Author = 'Wei-Meng Lee',
Publisher='Wrox'
};
Chapter 3 discusses the new C# 3.0 keyword var
.
Here, book1 is an object with 4 properties: ISBN
, Title
, Author
, and Publisher
(see Figure 4-1).

Figure 4-1
In this example, there's no need for you to define a class containing the four properties. Instead, the object is created and its properties initialized with their respective values.
C# anonymous types are immutable, which means all the properties are read-only — their values cannot be changed once they are initialized.
You can use variable names when assigning values to properties in an anonymous type; for example:
var Title = 'Professional Windows Vista Gadgets Programming';
var Author = 'Wei-Meng Lee';
var Publisher = 'Wrox';
var book1 = new {
ISBN = '978-0-470-17661-0',
};
In this case, the names of the properties will assume the names of the variables, as shown in Figure 4- 2.

Figure 4-2
However, you cannot create anonymous types with literals, as the following example demonstrates:
//---error---
var book1 = new {
'978-0-470-17661-0',
'Professional Windows Vista Gadgets Programming',
'Wei-Meng Lee',
'Wrox'
};
When assigning a literal value to a property in an anonymous type, you must use an identifier, like this: