Summary

In this chapter, you explored the basic syntax of the C# language and saw how to use Visual Studio 2008 to compile and run a working C# application. You examined the different data types available in the .NET Framework and how you can perform type conversion from one type to another. You have also seen the various ways to perform looping, and the various processor directives with which you can change the way your program is compiled.

Chapter 4

Classes and Objects

One of the most important topics in C# programming — in fact, the cornerstone of .NET development — is classes and objects.

Classes are essentially templates from which you create objects. In C# .NET programming, everything you deal with involves classes and objects. This chapter assumes that you already have a basic grasp of object-oriented programming. It tackles:

□ How to define a class

□ How to create an object from a class

□ The different types of members in a class

□ The root of all objects — System.Object

Classes

Everything you encounter in .NET in based on classes. For example, you have a Windows Forms application containing a default form called Form1. Form1 itself is a class that inherits from the base class System.Windows.Forms.Form, which defines the basic behaviors that a Windows Form should exhibit:

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace Project1 {

 public partial class Form1 : Form {

  public Form1() {

   InitializeComponent();

  }

 }

}

Within the Form1 class, you code in your methods. For example, to display a 'Hello World' message when the form is loaded, add the following statement in the Form1_Load() method:

public partial class Form1 : Form {

 public Form1() {

  InitializeComponent();

 }

 protected override void OnLoad(EventArgs e) {

  MessageBox.Show('Hello World!');

 }

}

The following sections walk you through the basics of defining your own class and the various members you can have in the class.

Defining a Class

You use the class keyword to define a class. The following example is the definition of a class called Contact:

public class Contact {

 public int ID;

 public string FirstName;

 public string LastName;

 public string Email;

}

This Contact class has four public members — ID, FirstName, LastName, and Email. The syntax of a class definition is:

<access_modifiers> class Class_Name{

 //---Fields, properties, methods, and events---

}

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.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату