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

Comparing C# with C and C++

In this section, we will explore how C# compares against other programming languages, such as C and C++. We will look at aspects that make C# similar, and also areas in which it differs from these languages.

C# versus C

If you have done some previous development on C# and C , you will realize that they follow similar code syntax, such as the use of semi-colons, and similar declarations of methods; the two languages are very different from one another. Just like in C, we can declare data variables with the same type, such as Char, and Integer. The following features make C# different from C:

Feature

C#

C

Object-oriented programming

Object-oriented programming is the main essence of any high-level programming language, and C# allows us to utilize the capabilities of OOP using the four main pillars of encapsulation, polymorphism, inheritance, and abstraction. In Chapter 3, Understanding Object-Oriented Programming, we will look at this in detail.

C as a programming language

does not support polymorphism, encapsulation, and inheritance.

It does not provide features such as function overloading, virtual functions, and inheritance.

Exception handling

Exception handling is the process of handling runtime errors that occur during the execution of the application. C# provides us with exception handling features that help us handle these scenarios in a better way. In Chapter 7, Implementing Exception Handling, we will look at this in detail.

C also does not provide any exception handling features.

Type safety

Every variable declared in a program has a type. In a typical type-safe language during the program compilation stage itself, the compiler will validate the values being assigned to variables and raise a compile time error if an incorrect type is assigned to it. C# is a type-safe language. However, in Chapter 8, Creating and Using of Types in C#, we will learn that it also allows you to use pointers using a keyword, UnSafe.

C language implements type safety, albeit with some exceptions. There are certain in-built functions such as printf that do not enforce that only character strings are passed to them.

Let's now look at how C# compares against another language, C++. After exploring the comparison between C# and C++, we will also explore how the .NET Framework makes C# a platform-independent language compared to C and C++.

C# versus C++

In most programming scenarios, C++ can be classified as an extension of C and can execute all the code that was written in C. It provides all the features of object-oriented programming while retaining the functionalities provided by C. There are several features that are common between C# and C++. Just as in C#, we can implement object-oriented programming, exception handling, and type safety in C++. However, there are also certain things that make C# different to C++ and more similar to Java.

Before we look at the differences and similarities between C# and C++, we must understand some key concepts pertaining to object-oriented programming.

The languages that implement object-oriented programming are classified in two categories:

  • Fully object-oriented languages
  • Pure object-oriented languages

A language is classified as a fully object-oriented programming language if it implements at least the four core pillars of Abstraction, Encapsulation, Polymorphism, and Inheritance.

On the other hand, a language can be defined as a pure object-oriented programming language when, apart from being fully object-oriented programming, it only contains classes and objects. This means that all methods, properties, and attributes declared must be inside a class and also should not have any predefined data types, such as char and int.

In the case of C#, we can have predefined data types. In Chapter 2, Understanding Classes, Structures, and Interfaces, we will look into those predefined data types in detail. This makes C# a fully object-oriented language and not a pure object-oriented language.

On the other hand, in the case of C++, we can define methods that are not part of any class. This, too, makes it a fully object-oriented language.

Now, let's look at some of the similarities and differences between C# and C++:

Feature

C#

C++

Object-oriented programming

As described previously, C# is a fully object-oriented language.

Similar to C#, C++ is also a fully object-oriented language.

Memory management

C# has got an inbuilt garbage collector that manages the allocation and deallocation of memory. In Chapter 9, Managing the Object Life Cycle, we will understand memory management in C# in detail.

C++ does not have a built-in garbage collector. Due to this, developers are responsible for handling the allocation and deallocation of memory.

Inheritance

C# does not support multiple inheritance. In Chapter 2, Understanding Classes, Structures, and Interfaces, we will learn what it means; however in simple terms, it means that a class can only inherit from one class at a time.

Compared to C# , C++ allows us to implement multi-level inheritance.

Use of pointers

Although C# allows us to use pointers in our code, we need to declare the code with a snippet of UnSafe. We will look into this in detail in Chapter 8, Creating and Using of Types in C#.

C++ allows us to use pointers anywhere without any implicit declaration in the code.

In the previous two sections, we saw how C# compares to both C and C++. However, there is one important difference that we haven't yet explored. That feature is platform independence and was one of the main reasons C# was introduced by Microsoft. When working with C and C++, we need to compile the code in accordance with the underlying platform features, such as the operating system.

Suppose we write an application in C or C++ and compile it. During the compilation stage, the compiler translates the code into a native language code that is only compatible with the underlying platform. This basically implies that an application in C++, developed and compiled on a Windows machine, will just be compatible with a Windows machine. If the compiled bits are used on a different system, such as Linux, it will not work there.

This difference is caused due to the varying nature of compilers and their compatibility with underlying operating systems, such as Linux and Windows. These are some of the common compilers in Linux and Windows that are available for C and C++:

  • Linux: GCC, Failsafe C, and SubC
  • Windows: Microsoft Windows SDK, Turbo C++, and SubC

Before C# was developed, this platform dependence issue was a major disadvantage compared to some of the other programming languages, such as Java. In Java, when an application is compiled, it's not directly converted into machine code. Instead, it's converted into an intermediate language known as ByteCode. The ByteCode is platform-independent and can be deployed on different platforms.

When Microsoft introduced C#, they inculcated the same principle in the language. When an application written in C# is compiled, instead of being converted to the native code compatible with the machine, the application is first translated to an intermediate language commonly known as IL code.

After the IL code is generated, the Common Language Runtime (CLR) comes into effect. CLR is a runtime environment that sits in the memory of the underlying machine and converts the IL code to the native code, which is specific to the machine. This process is Just-In-Time (JIT) compilation. In the next section, we will look at the underlying platform of the .NET Framework, which handles all this for a C# application.