68Microsoft Visual Studio 2010: A Beginner’s Guide

Key Skills & Concepts

Create Classes

Write Methods

Code Fields and Properties

Atype is a general term for classes, modules, enums, and more. This chapter will specifically discuss the class type, which allows you to create your own custom types.

You’ll also see the value of a class when you learn about class members. You’ll see how the field, method, and property class members can be used. We’ll start with learning how to create and use classes.

Creating Classes

Previously, you learned about the primitive types, which are built into languages and alias the underlying .NET types. You can also create your own types, via classes, which you can instantiate and create objects with. The following section explains how to create

a class and then instantiate an object from it.

Class Syntax

To create a new custom class definition, right-click the project, select Add Class, name the class Employee for this example, and type the file extension .cs for C# or .vb for VB; then click Add (VS will add this file extension for you if you don’t). You’ll see a file with the same code as Listing 3-1.

Listing 3-1 A new Employee class

C#:

using System;

using System.Collections.Generic; using System.Linq;

using System.Text; namespace FirstProgram

Page 91
Image 91
Microsoft 9GD00001 manual Creating Classes, Class Syntax, Listing 3-1 a new Employee class