C Sharp

Defining Classes

The syntax used for defining classes in C# is simple, especially if you usually program in C++ or Java. You place the class keyword in front of the name of your class and then insert the class's members between the "curlies," like so: -

class Employee
    private long employeeId;

As you can see, this class is as basic as it gets. We have a class named Employee that contains a single member named employeeId. Notice the keyword private that precedes our member. This is called an access modifier. Four valid access modifiers are defined by C#, and I'll cover them in a moment.

Class Members

In Chapter 4, "The Type System," I discussed the different types defined by the Common Type System (CTS). These types are supported as members of C# classes and include the following: -

  • fields A field is a member variable used to hold a value. In OOP parlance, fields are sometimes referred to as the object's data. You can apply several modifiers to a field, depending on how you want the field used. These modifiers include static, readonly, and const. We'll get to what these modifiers signify and how to use them shortly.
  • methods A method is the actual code that acts on the object's data (or fields). In this chapter, we'll focus on defining class data-Chapter 6, "Methods," will cover methods in a lot more detail.
  • properties Properties are sometimes called smart fields because they are actually methods that look like fields to the class's clients. This allows a greater degree of abstraction for the client in that the client doesn't have to know whether it's accessing the field directly or an accessor method is being called. Chapter 7, "Properties, Arrays, and Indexers," covers properties in detail.
  • constants As the name suggests, a constant is a field with a value that cannot be changed. Later in this chapter, I'll discuss constants and compare them to something called read-only fields.
  • indexers As a property is a smart field, an indexer is a smart array-that is, a member that lets an object be indexed through get and set accessor methods. An indexer enables you to easily index into an object for purposes of setting or retrieving values. Indexers and properties are both covered in Chapter 7.
  • events An event is something that causes some piece of code to run. Events are an integral part of Microsoft Windows programming. An event is fired, for example, when the mouse is moved or a window is clicked or resized. C# events use the standard publish/subscribe design pattern seen in Microsoft Message Queuing (MSMQ) and the COM+ asynchronous event model-which gives an application asynchronous event-handling capabilities-but in C# this design pattern is a "first-class" concept built into the language. Chapter 14, "Delegates and Event Handlers," describes how to use events.
  • operators C# gives you the ability, via operator overloading, to add the standard mathematical operators to a class so that you can write more intuitive code by using those operators. Operator overloading is covered in detail in Chapter 13, "Operator Overloading and User-Defined Conversions."

by BrainBellupdated