The post C# Introduction lists all of the C# series of posts we have at this site. We also have another series of posts on C# classes that is designed to be studied after this series. It is called C# Classes Intermediate.
What is the Objective?
When writing code, think about what problem you are solving. What’s the objective? This gets you to think in terms of objects and prepare you for object oriented programming (OOP). Your objective will help you identify the objects of your application. The objects will translate into classes. What are the requirements? Suppose you have a retail store that needs a list of their movies and their customers. They also need to allow customers to rent movies. We’ve got three objects: movies, customers and transactions.
Classes
Classes are the templates for objects. With .NET, everything is an object. OOP sets out to solve some of the problems with procedural programming. OOP is more modular giving you more versatility and provides more opportunity for code reuse. With OOP you spend more time in the design of the solution.
An object is the building block of an application. An object can be similar to a C# struct type. Objects in C# are created from types, just like the variables such as integer and double and others. The type of an object is known as a class. You use class definitions to instantiate objects, which creates real, named instances of a class.
Class Members
There are nine types of class members. Within the 9 types there are 2 types: data members and function members. Data members store data. Function members execute code. Data members include: fields and constants. Function members include the following: methods, properties, constructors, destructors, operators, indexers and events.
Properties and Fields
Unified Modelling Language (UML) is used to define your classes. Classes contain properties, fields and methods. Properties and fields contain data in the form of strings and numbers. Properties do not provide direct access to data, whereas fields can. Properties allow you to provide reasonable ranges of values. For example, if your class was “CupOfCoffee” and you had a property called Sugar of the type int (integer), you could prevent negative numbers and very large numbers for being stored in the object. Therefore, in general it is better to use properties rather than fields.
Read/write access to properties can be clearly defined by an object. You can also specify different sorts of access permissions for both fields and properties, known as accessibility. Accessibility determines which code can access these members. If it is “public” then all code can access it. If it is “private” then only code within the class itself may access it. There are more complex schemes available. Private fields and properties can be thought of as local to the object that possesses them.
One common theme is to make fields private and provide access to them through public properties. The private field is often called a backing field.
Method
A method is a function exposed by objects. These methods can be called in the same way as any other function can and can use return values in the same way. Methods can be public or private just like fields and properties. Our “CupOfCoffee” class may have a method called AddSugar().
Every object has a life cycle. There are three states: Construction, In Use and Destruction. When an object is first instantiated it needs to be initialized. Initialization is known as construction and is carried out by the constructor. When an object is destroyed there are often some clean-up tasks to perform such as freeing up memory. This is the job of the destructor function known as the destructor.
In C# the constructors are called with the new keyword. There are default and non-default constructors. The default constructor is a parameter-less method with the same name as the class itself. Objects can be instantiated with nondefault constructors, that have parameters. Constructors, like fields and properties can be public or private. Code external to the class cannot instantiate an object using a private constructor; it must use a public constructor. Therefore you can force users of your class to use a non-default constructor by making the default constructor private.
Some classes have no public constructors, making it impossible for external code to instantiate them and they are said to be non-createable (but not useless).
As well as having members such as properties, methods and fields that are specific to object instances, it is also possible to have static members which can be methods, properties or fields. Static members are shared between instances of a class so they can be thought of as global for objects of a given class. Static properties and fields enable you to access data that is independent of any object instances. Static methods enable you to execute commands related to the class type but not specific to object instances. When using static members you don’t even need to instantiate an object. For example, the Console.WriteLine() and Convert.ToString() methods are static. Constructors of these classes are not publicly accessible. You might use a static property to keep track of how many instances of a class have been created.
That covers the very basics of classes. Topics yet to be discussed are: interfaces, disposable objects, inheritance, polymorphism, containment, collections, operator overloading, events, reference types and value types.