Program Structure

 
Chapter 2 - C# Basics
bySimon Robinsonet al.
Wrox Press 2002
  

So far, we've been introduced to some of the main 'building blocks' that make up the C# language including declaring variables , data types, and program flow statements, and we have seen a very short complete program containing only the Main() method. What we haven't really seen is how we can put all these together to form a longer complete program. The key to this lies in working with classes.

Classes

As we've seen, classes play a huge role in C# programs, so much that we're going to dedicate Chapter 3 entirely to object-oriented programming in C#. However, since it really is pretty well impossible to write a C# program without using classes, we will need to say a little bit about them here. We will cover basic syntax for writing and calling into classes, but we will save inheritance and other OOP features for future chapters.

Classes are essentially templates from which we can create objects. Each object contains data and has methods to manipulate and access that data. The class defines what data and functionality each particular object (called an instance ) of that class can contain, but doesn't usually contain any data itself. For example, if we have a class that represents a customer, it might define fields such as CustomerID , FirstName , LastName , and Address , which we will use to hold information about a particular customer. It might also define functionality that acts upon the data stored in these fields. We can then instantiate an object of this class to represent one specific customer, set the field values for that instance, and use its functionality.

Class Members

The data and functions within a class are known as the class's members . Microsoft's official terminology distinguishes between data members and function members. As well as these members, classes can also contain nested types (such as other classes). All members of a class can be declared as public (in which case they are directly accessible from outside the class) or as private (in which case they are only visible to other code within the class), just as in VB, C#, and Java. C# also has variants on this theme, such as protected , which we will explore in future chapters.

Microsoft has published detailed guidelines for the naming of variables, data types, and so on, which are available in the MSDN documentation and discussed in Chapter 6. We'll note for now that in this book, we are tending to name public fields and methods, as well as classes like this: PhoneNumber , CustomerAddress; while private fields and variables local to methods are named like this: phoneNumber , customerAddress . Note the capitalization in both cases. This broadly conforms to the guidelines and to standard practice among the C# community. These types of names are respectively referred to as Pascal-cased and camel-cased respectively.

Data Members

Data members are those members that contain the data for the class fields, constants, and events.

Fields are any variables associated with the class. In fact, if we define any variable at the class level, that is in fact a field of the class. If fields are declared as public , they will be accessible from outside the class. For example, we can define a PhoneCustomer class with CustomerID , FirstName and LastName fields like this:

   class PhoneCustomer     {     public int CustomerID;     public string FirstName;     public string LastName;     }   

Once we have instantiated a PhoneCustomer object, we can then access these fields using the Object.FieldName syntax, for example:

   PhoneCustomer Customer1 = new PhoneCustomer();     Customer1.FirstName = "Burton";   

Constants can be associated with classes in the same way as variables. We declare a constant using the const keyword. Once again, if it is declared as public , it will be accessible from outside the class.

 class PhoneCustomer {   public const int DayOfSendingBill = 1;   public int CustomerID;    public string FirstName;    public string LastName; } 

Chapter 4.

Function Members

Function members are those members that provide some functionality for manipulating the data in the class. They include methods, properties, constructors and destructors, operators, and indexers.

Methods are functions that are associated with a particular class. They can be either instance methods , which work on a particular instance of a class, or static methods , which provide more generic functionality that doesn't require us to instantiate a class (like the Console.WriteLine() method which we have already met). We will look at methods in the next section.

Properties are sets of functions that can be accessed from the client in a similar way to the public fields of the class. C# provides a specific syntax for implementing read and write properties on our classes, so we don't have to jury-rig methods whose names have the words Get or Set embedded in them. Since there's a dedicated syntax for properties that is distinct from that for normal functions, the illusion of objects as actual things is strengthened for client code. Even better, properties centralize the read/write aspects of a property, making property code easier to maintain for the developer of the class.

Constructors are functions called when an object is instantiated. They must have the same name as the class to which they belong, and cannot have a return type. Constructors are useful for setting the values of fields when an object is instantiated.

Destructors are similar to constructors, but are called when the object is destroyed . They have the name of the class, preceded by a tilde ( ~ ). Since the CLR handles garbage collection, it is impossible to predict when a destructor will be called, and destructors are used much less frequently in C# than in C++.

Classes may also contain definitions for Chapter 3.

Chapter 3.

For reference, we will also list the modifiers that can be applied to a method, though we won't be covering most of these until the next two chapters:

Modifier

Description

new

The method hides an inherited method with the same signature.

public

The method can be accessed from anywhere , including outside the class.

protected

The method can be accessed from within the class to which it belongs, or a type derived from that class.

internal

The method can be accessed only from within the same assembly.

private

The method can only be accessed from inside the class to which it belongs.

static

The method does not operate on a specific instance of the class.

virtual

The method can be overridden by a derived class.

abstract

A virtual method that defines the signature of the method, but doesn't provide an implementation.

override

The method overrides an inherited virtual or abstract method.

sealed

The method overrides an inherited virtual method, but cannot be overridden by any classes that inherit from this class. Must be used in conjunction with override .

extern

The method is implemented externally, in a different language.

Structs

We will briefly mention that, besides classes, it is also possible to declare structs. The syntax is basically identical, except that we use the keyword struct instead of class . For example, if we wished to declare PhoneCustomer as a struct , we would write

   struct PhoneCustomer   {    public const int DayOfSendingBill = 1;    public int CustomerID;    public string FirstName;    public string LastName; } 

We won't look at structs in detail until Chapter 3. They differ from classes in the way that they are stored in memory and accessed (classes are reference types stored in the heap, structs are value types stored on the stack), and in some of the features (structs don't support inheritance). You will tend to use structs for smaller data types for performance reasons.

C++ developers beware; structs in C# are very different from classes in their implementation. This is very different to the situation in C++, for which classes and structs are virtually the same thing.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net