Tuesday, June 24, 2008

Introduction to classes in C# and the .NET framework

Programming Language: C#
Type: Tutorial
Level: Beginners
Topic Category: General Programming
Main Series: Introduction to programming in C# and the .NET framework
Topic Title: 1. Classes - Introduction



What are classes and types?

Classes are code units that describe the attributes of interest of some real life object/thing to a software under development. It describes the data and data structures needed to define an instance (some arbitrary single object of the kind described by the class) of the class/type/kind and the operations that kind of objects can perform.

In other words, the class is a digital representation of a "kind of objects/things" or a "category of things". It’s not a digital representation of the "things" themselves. A digital representation of one of the things is an object/instance of that class!

For example, let’s consider a software that needs to represent a car electronically. Of course a car represented digitally will not be an exact copy of the real life car neither will it include all its features. Instead we see what actually is of concern to our software and represent it. That is, we usually represent real-life things in a certain "context" rather than representing them generally and thoroughly. The cars we want to represent are needed for the context of a software that car resellers can use, so the information of use will not include for example, the exact materials of which the car parts were manufactured, the list of brands of each component of the car neither the thorough dimensions or technical drawing of the car’s body.

Instead, let’s suppose that for the client of the software you are designing, what matters is the color, make, model, tire model, number of passengers, manufacturer and price of the car.
As we said earlier, classes represent kinds/categories not single instance things. So, we need to represent the type/kind/category of things called cars. A class will describe what kind of values are needed to represent the attributes of a car, but it will not give specific values for these attributes. Actually, the process of designing classes is simply the process of identifying the
attributes of the kind/type/category of things the class will represent!

So if we want to represent a car in the "context" we extracted from the customer, we will need to represent its color, its make, its model ... etc. The colored words are the names of the attributes/fields needed to define a single car. By "the attributes needed to define a car" we mean that to define a specific single car, we must give a value for each of these attributes. We will also need to define the type/kind of each attribute so as to be able to give it a value. For instance, a color can be represented by a number, a list of available color names and by three numbers each representing the amount of red, green and blue required to be mixed to get the color. We choose the representation that best fits our need.

You can think of classes as the means by which we describe types of things to the computer. For example, if we want to describe to the computer what points (as a kind of geometrical shapes) are, we tell it that for a point to be defined, it needs an x coordinate and a y coordinate both being numbers (integers if we work on an integral environment)
Now when we say that we want to define/create a specific point, we have to give the computer its specific x and y coordinates values.

The primitive/built-in/fundamental classes in C# and .NET

The C# programming language provides a set of what we call primitive/essential/fundamental/basic/simple types so as to use them in describing other types. Each of these types is a class like any other class you develop but it is built-in to the .NET framework and some of their names are reserved words in the C# language.

For example, there is a set of types that represents the different kinds of numerals in the real world (the short, int, long, float, double, decimal, ushort , unsigned short, uint, ulong types/classes)

The integer class itself (actually named Int32 in the .NET framework) describes integer numbers and an instance integer represents a single integer value. We declare a reference to an integer by writing:
int someInteger;
Then we must assign a value to the referenced object (actually for integers, it is given the value of 0 initially)
someInteger = 10;
Some of the classes that are considered simple/basic/primitive are:
short, int, long, ushort, uint, ulong, float, double, decimal, string, object, byte, void
Some of the more complex but still considered basic by many:
DateTime, TimeSpan, Version, Guid, DayOfWeek

No comments: