Fundamentals of Computer Programming with C#
Chapter 14. Defining Classes
In This Chapter
In this chapter we will understand how to define custom classes and their
elements. We will learn to declare fields, constructors and properties for
the classes. We will revise what a method is and we will broaden our
knowledge about access modifiers and methods. We will observe the
characteristics of the constructors and we will set out how the program
objects coexist in the dynamic memory and how their fields are initialized.
Finally, we will explain what the static elements of a class are – fields
(including constants), properties and methods and how to use them
properly. In this chapter we will also introduce generic types (generics),
enumerated types (enumerations) and nested classes.
The aim of every program written by the programmer is to solve a given
problem based on the implementation of a certain idea. In order to create a
solution, first, we sketch a simplified actual model, which does not represent
everything, but focuses on these facts, which are significant for the end
result. Afterwards, based on the sketched model, we are looking for an
answer (i.e. to create an algorithm) for our problem and the solution we
describe via given programming language.
Nowadays, the most used programming languages are the object-oriented.
And because the object-oriented programming (OOP) is close to the way
humans think, using one easily allows us to describe models of the
surrounding life. Certain reason for this behavior is, because OOP offers tools
to draw the set of concepts, which outline classes of objects in every model.
The term – class and the definition of custom classes, different from the .NET
system framework’s, is built-in feature of the C# programming language. The
purpose of this chapter is to get us know with it.
Let’s Recall: What Does It Mean Class and Object?
Class in the OOP is called a definition (specification) of a given type of
objects from the real-world. The class represents a pattern, which describes
the different states and behavior of the certain objects (the copies), which are
created from this class (pattern).
Object is a copy created from the definition (specification) of a given class,
also called an instance. When one object is created by the description of one
class we say the object is from type "name of the class".