In this section, we will go over a basic programming syntax of a C# application, namely: classes, namespaces, and assemblies.
As C# is an object-oriented language, and at the basic level it contains building blocks known as classes. The classes interact with one another, and as a result, provide functionality at runtime. A class consists of two components:
- Data attributes: Data attributes refer to the different properties defined in the class object.
- Methods: Methods indicate the different operations that are to be executed in the class object.
As an example, we will look at the representation of a car as an object in C#. At a very basic level, a car will have attributes such as the following:
- Make: For example Toyota, Ford, or Honda.
- Model: For example Mustang, Focus, or Beetle.
- Color: Color of the car, such as Red or Black.
- Mileage: Distance covered per liter of fuel consumed.
Please note that a car can have more attributes, but as this example is just being used for the sake of explanation, we have included these basic attributes. While writing a C# application, all of these will be captured as attributes for the Car class.
Similarly, to make sure the Car class achieves all of the desired features, it will need to implement the following operations:
- StartEngine: This function represents how the car starts moving.
- GainSpeed: This function represents how the car accelerates.
- ApplyBrake: This function represents how the car applies brakes to slow down.
- StopEngine: This function represents how the car stops.
While writing any application in C#, the starting point is always to capture all the actors/objects that are interacting with each other. Once we identify the actors, we can then identify the data attributes and methods that each of them must have so that they can exchange the required information with each other.
For the Car example being discussed, the following would be the definition of the Car class. For the sake of explanation, we have just assumed that the attributes will be of type String; however, when we go through Chapter 2, Understanding Classes, Structures, and Interfaces, we will go over some more data types that can be declared in a class. For the car example, the following syntax would be a representative program in a C# application:
class Car
{
string Make;
string Model;
string Color;
float Mileage;
void StartEngine()
{
// Implement Start Engine.
}
void GainSpeed()
{
// Implement Gain Speed.
}
void ApplyBrake()
{
// Implement Gain Speed.
}
void StopEngine()
{
// Implement Gain Speed.
}
}
In any application, there can be some classes that are related to one another. They can be based in terms of similar functionality, or they could be dependent on each other. In C#, we handle such a segregation of functionality via namespaces. For example, we can have a namespace for handling all operations related to reading/writing logs in the file directory. Similarly, we can have namespaces for handling all operations related to capturing user-specified information from inputs.
When our applications continue to evolve and we have several namespaces, we may have a need to group related namespaces under one umbrella. This ensures that if any class changes under any particular namespaces, it will not affect all the classes defined in the application. This structuring of namespace is done via assemblies in C#. Assemblies are also known as DLLs, or dynamically linked libraries. Depending upon how we structure our code, when an application is compiled, it results in multiple DLLs.