Using Classes and Structures

Classes and structures represent the two principal user-defined types.

Classes are templates for objects. They describe the kind and amount of data that an object will contain, but they do not represent any particular instance of an object. A real-world example of a class might be "Car"—the abstract idea of what a car is.

You know that a car has an engine and four wheels, a color, a fuel efficiency, and a dozen other properties. Although the Car class would describe all of these properties as well as have descriptions of actions the car might perform (go forward, turn on windshield wipers, etc.), the class would not represent any particular car.

Your car, on the other hand, is an object. It has a specific color, a specific fuel efficiency, a specific kind of engine, and four specific wheels. A different car might have different values for each of these properties, but both would be recognizable as being an instance of the Car class.

Members:

Classes describe the properties and behaviors of the objects they represent through members. Members are methods, fields, properties, and events that belong to a particular class. Fields and properties represent the data about an object—the color of the car, its fuel efficiency, and whether it has an automatic or manual transmission, for example.

A method represents something the object can do, such as go forward or turn on headlights. An event represents something interesting that happens to the object, such as overheating or crash.

Creating Classes:

You create a new class by using the Class (Visual Basic .NET) or class (C#) keyword. For example:

Visual Basic .NET

Public Class Widget
' Class member implementation goes here
End Class

Visual C#

public class Widget
{
// Class member implementation goes here
}

In this example, you use the Class (class) keyword to create a user-defined class. Widget is the name of the class, and the Public keyword specifies the access level.

Creating Structures:

Creating structures is very similar to creating classes. You use the Structure (Visual Basic .NET) or struct (C#) keyword. For example:

Visual Basic .NET

Public Structure Vector
' Structure implementation goes here
End Structure

Visual C#

public struct Vector
{
// Structure implementation goes here
}

Adding Members:

In Visual Basic .NET, a class comprises everything between the Class keyword and the End Class keyword. In C#, a class comprises everything within the curly braces ({}). Structures are similar. Within the bounds of a class or structure, you add the members. The following example demonstrates adding a member field to your Widget class.

Visual Basic .NET

Public Class Widget
Public Integer Spin
End Class

Visual C#

public class Widget
{
public int Spin;
}

Your Widget class now contains a member variable called Spin. This variable has a Public access level and can contain an Integer (int) value.

Nested Types:

Types can also contain other types. These are called nested types. Using classes as an example, a nested class usually represents an object that the parent class might need to create and manipulate, but that an external object would never need to create independently. An abstract example might be a Wheel class.

A Wheel class might need to create and maintain a collection of Spoke objects internally, but outside users would probably never need to create a Spoke object independent of a wheel. A more concrete example might be an AccountManager class that controls all the interaction with Account objects. You might not want to allow Account objects to be created independently of the AccountManager class, thus making Account a nested class inside AccountManager.

This does not mean that outside objects can never instantiate objects based on nested classes—this depends on the access level of both the parent class and the nested class.

Visual Basic .NET

Public Class Widget
' Widget Class code goes here
Private Class Widgurt
' Widgurt class code goes here
End Class
End Class

Visual C#

public class Widget
{
// Widget class code goes here
private class Widgurt
{
// Widgurt class code goes here.
}
}

Instantiating User-Defined Types:

You declare and instantiate a user-defined type the same way that you declare and instantiate a .NET Frameworks type. For both value types (structures) and reference types (classes), you need to declare the variable as a variable of that type, and then create an instance of it with the New (new) keyword.

Classes vs. Structures:

On the surface, classes and structures (structs) appear to be very similar. Both can contain members such as fields and methods, both require a constructor to create a new instance of themselves, and like all types in the .NET Framework, both inherit from Object.

The key difference between classes and structures is that classes are reference types and structures are value types. On a low level, this means that the instance data for classes is allocated in a memory location called the heap, whereas the instance data for structures is allocated in a memory location called the stack. Access to the stack is designed to be light and fast, but storage of large amounts of data on the stack can impede overall application performance.

In practical terms, what this means is that structures are best used for smaller, lightweight objects that contain relatively little instance data, or for objects that do not persist for long. Classes are best used for larger objects that contain more instance data and are expected to exist in memory for extended periods.

RELATED POSTS

DAY 1 MICROSOFT DOT NET FRAME WORK

DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY

DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES

DAY 4 METHODS IN FRAME WORK

DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE

DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO

DAY 7 DATA TYPES IN DOT NET

DAY 8 DATA TYPES IN DOT NET PART TWO

DAY 9 IMPLEMENTING PROPERTIES IN DOT NET

DAY 10 DELEGATES AND EVENTS

No comments:

Post a Comment