Книга: C# 2008 Programmer

Using Partial Classes

Using Partial Classes

Instead of defining an entire class by using the class keyword, you can split the definition into multiple classes by using the partial keyword. For example, the Contact class defined in the previous section can be split into two partial classes like this:

public partial class Contact {
 public int ID;
 public string Email;
}
public partial class Contact {
 public string FirstName;
 public string LastName;
}

When the application is compiled, the C# compiler will group all the partial classes together and treat them as a single class.

Uses for Partial Classes

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.

Оглавление книги


Генерация: 1.356. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз