Book Image

Programming in C#: Exam 70-483 (MCSD) Guide

By : Simaranjit Singh Bhalla, SrinivasMadhav Gorthi
Book Image

Programming in C#: Exam 70-483 (MCSD) Guide

By: Simaranjit Singh Bhalla, SrinivasMadhav Gorthi

Overview of this book

Programming in C# is a certification from Microsoft that measures the ability of developers to use the power of C# in decision making and creating business logic. This book is a certification guide that equips you with the skills that you need to crack this exam and promote your problem-solving acumen with C#. The book has been designed as preparation material for the Microsoft specialization exam in C#. It contains examples spanning the main focus areas of the certification exam, such as debugging and securing applications, and managing an application's code base, among others. This book will be full of scenarios that demand decision-making skills and require a thorough knowledge of C# concepts. You will learn how to develop business logic for your application types in C#. This book is exam-oriented, considering all the patterns for Microsoft certifications and practical solutions to challenges from Microsoft-certified authors. By the time you've finished this book, you will have had sufficient practice solving real-world application development problems with C# and will be able to carry your newly-learned skills to crack the Microsoft certification exam to level up your career.
Table of Contents (22 chapters)
17
Mock Test 1
18
Mock Test 2
19
Mock Test 3

Basic structure of C#

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.