Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)
Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)
Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)
Ebook513 pages3 hours

Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book starts by introducing the concepts of .NET framework. It then discusses OOP and explores how one can work with OOP in C#. There are two chapters on OOP: the first one covers the basics of object-oriented programming (OOP); and the second one delineates advanced concepts related to OOP and how they can be implemented in C#.
Next, the book discusses Language Integrated Query (LINQ) and how to work with it in C#, followed by multithreading, asynchronous and parallel programming concepts with relevant code examples to illustrate the concepts covered. Generics, collections, generic collections, delegates, lambda expressions are also covered in this section.
In the last section of the book, serialization, file I/O and how to work with them in C# are discussed concisely. A separate chapter on C# 8.0 is added to highlight its new features. .. There is an appendix chapter as well that discusses how one can get started working with Visual Studio 2019.
LanguageEnglish
Release dateOct 14, 2019
ISBN9789388176576
Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)

Read more from Joydip Kanjilal

Related to Mastering C# 8.0

Related ebooks

Programming For You

View More

Related articles

Reviews for Mastering C# 8.0

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Mastering C# 8.0 - Joydip Kanjilal

    CHAPTER 1

    The Microsoft .NET Ecosystem

    Microsoft .NET Framework is a developer platform that can be used for building robust, scalable and portable applications. The first version of the Microsoft .NET Framework was released in February 2002 and since then it has matured by leaps and bounds. This book starts with a discussion on Microsoft .NET Framework.

    Since C# programs would leverage the .NET Framework, a good knowledge of what the underlying platform is all about and how its components work is imperative for building efficient applications. This chapter discusses the concepts related to Microsoft .NET Framework, its components, memory management, etc. In order to be a proficient programmer in C#, one must understand the internals of .NET as well. Let’s take a round of briefing on the Microsoft .NET Framework before we start learning C# in the next chapter.

    Structure

    In this chapter, we will discuss the following points:

    Introduction to Microsoft .NET

    Common Language Runtime

    Framework Class Library

    Common Language Specification

    Common Type System

    Common Intermediate Language

    Components of Microsoft .NET Framework

    Garbage collection

    The Dynamic Language Runtime

    Objective

    After reading this chapter, the reader will be able to:

    Understand the components of .NET Framework

    Learn how the garbage collector works in .NET.

    Understand why the Common Intermediate Language is needed

    Understand the Dynamic Language Runtime

    Introduction to Microsoft .NET

    .NET Framework is a popular software development platform created by Microsoft. It has been around for a long time now and can be used to build windows applications, web services, and web-based applications. The .NET Framework primarily runs on Windows. However, you can use .NET Core, a light-weight framework, if you have to build and run your applications on Linux or Mac as well. .NET Core is a free, modular, open-source framework for Windows, Linux, and macOS operating systems. It is a platform-independent successor to .NET Framework that has been released under the MIT license.

    The .NET Framework provides the following benefits:

    Portability

    Cross-Language Integration

    Interoperability

    Base Class Library

    Security

    Evolution of .NET framework

    Figure 1.1 illustrates the evolution of .NET Framework from version 2.0:

    Figure 1.1: The evolution of .NET over the years

    Note that Figure 1.1 shows the evolution of .NET till .NET Framework 4.5 only. The major releases of .NET Framework after .NET Framework 4.5, with their features, are as follows:

    .NET Framework 4.6: This version introduces new features and enhancements in cryptography, ADO.NET, WPF, profiling, NGen, and Windows Workflow Foundation (WWF).

    .NET Framework 4.7: This version introduces new features and enhancements in base classes, networking, ASP.NET, Windows Communication Foundation (WCF), Windows Forms, and Windows Presentation Foundation (WPF).

    .NET Framework 4.8: This version introduces new features and enhancements in the areas ofbase classes, WCF, WPF, and Common Language Runtime (CLR).

    Common Language Runtime

    The Common Language Runtime (CLR) is a runtime engine that is responsible for executing your programs, code verification, type safety, memory allocation, garbage collection, security, and many more. It provides several services, which includes:

    Just-in-time compilation

    Garbage collection

    Code access security

    Memory management

    Code verification

    Figure 1.2 depicts the components of .NET Framework.

    Figure 1.2: Components of .NET Framework

    Common Intermediate Language

    The Common Intermediate Language (CIL), formerly known as Microsoft Intermediate Language (MSIL), is a platform-independent language into which source code that is compiled in the .NET environment is written. It is a set of instructions that are platform independent and is generated by the compiler from the source code. At runtime, the CIL is converted into a machine code by the Just-in-Time (JIT) compiler. In other words, the JIT compiler converts the CIL into a machine code specific to the target environment, that is, the environment in which the application needs to execute. Figure 1.3 shows how this entire process works:

    Figure 1.3

    Dynamic Language Runtime

    While the CLR provides services such as, a common type system, JIT, garbage collection, and many more, the Dynamic Language Runtime (DLR) runs on top of the CLR and offers services for dynamic languages. These include: dynamic types, dynamic method dispatch, code generation, and more.

    Figure 1.4 shows how CLR and DLR are related:

    Figure 1.4: The Dynamic Language Runtime (DLR)

    The DLR was first introduced in .NET 4.0, and since then it has been a part of .NET Framework. The addition of a powerful feature, dynamic keyword, in .NET 4.0 enhanced the capabilities of C# programming language. Essentially, the dynamic keyword is used to skip the compile-time type checking. When you have a method with a dynamic parameter, the method can accept any type at runtime. We’ll learn more about the dynamic keyword later in this book.

    Framework Class Library

    The .NET Framework Class Library (FCL) is a comprehensive collection of reusable classes, interfaces, and data types that provide several services. Note that the FCL provides the base on top of which applications and components written in .NET are built.

    The services provided by the FCL include the following:

    File handling

    Exceptions

    Remoting

    Sockets

    Database access

    XML

    Web services

    Common Type System

    The Common Type System (CTS) is a standard that is used to define how the .NET types are represented in the memory. The CTS enables multiple languages targeted at the .NET Framework to co-exist. It provides support for two types, which are explained inthe next section.

    Value types and reference types

    The CTS provides support for two types. These include: value types and reference types. While value types are stored in the stack, reference types are stored in the managed heap. The primitive data types, such as char, int, and etc, are examples of value types. Other examples of value types include: struct and enum. Refer to Figure 1.5 for better understanding:

    Figure 1.5: Common Type System

    Here’s the list of value types in .NET:

    bool

    byte

    char

    decimal

    double

    enum

    float

    int

    long

    sbyte

    short

    struct

    uint

    ulong

    ushort

    Instances of classes, arrays, and etc are examples of reference types. Here’s the list of the reference types in .NET:

    class

    interface

    delegate

    object

    strings

    arrays

    Managed and unmanaged code

    Managed code represents the source code that runs within the context of the CLR and managed by it. Type safety is a key concept in .NET. On the contrary, unmanaged code is code that runs outside the CLR’s context. In other words, the unmanaged code is one that is simply not managed by the CLR. Therefore, the safety of unmanaged or unsafe code is not verified by the CLR.

    Just-in-Time compiler

    Programs written in C# or VB.NET when compiled generate intermediate language code. This is also known as the intermediate language (IL) code. The JIT compiler is responsible for compiling the IL code to a native code at runtime. Figure 1.6 illustrates the CLR execution mode.l

    Figure 1.6: The CLR Execution Model

    The native code is then executed by the host operating system with the help of the CPU and other resources.

    Garbage collection

    Garbage collection (GC) is a feature provided by .NET CLR to manage memory automatically. It cleans up the unused objects, that is, objects that are no longer in use. The term garbage here implies an object that is no longer needed, that is, it’s now a waste.

    The heap memory is divided into two portions: The small object heap (SOH) and the large object heap (LOH). Let’s first discuss how the GC works in the SOH. For reference, see Figure 1.7:

    Figure 1.7: The Managed Heap in .NET

    Microsoft .NET adopts a generational model for storing objects in the memory. In this model, the garbage collected heap (part of the small object heap) is partitioned into three distinct regions: generation 0, generation 1, and generation 2. When objects are first created, they are placed in generation 0. If the objects in generation 0 survive a generation 0 collection, they are promoted to generation 1. Similarly, when objects in generation 1 survive a generation 1 collection, they are promoted to generation 2.

    Figure 1.8 illustrates these three generations of the GC heap:

    Figure 1.8: The three generations of the GC

    It should be noted that the GC works much more frequently in the lower generations than in the higher ones. The objects that survive GC cleanup in generation 1 are promoted to generation 2. While the objects residing in generation 0 are called short-lived objects, the objects in generation 2 are considered old objects.

    Objects that reside in the LOH don’t pass through the generations, that is, generation 0, 1, or 2. The LOH is a special area in the managed memory that is reserved for storing objects larger than the size of 85KB.

    The System.GC class represents the GC in .NET. This class contains several methods and properties. Some of the important members of this class include:

    GC.Collect: This method is used to force a garbage collection of one or more generations. If you pass the generation number to be collected, it would collect that generation only. If no generation number is passed to this method as an argument, all generations are collected.

    GC.GetTotalMemory: This method returns the total number of bytes that have been allocated to the managed memory.

    GC.KeepAlive: This method is used to extend the lifetime of an object.

    GC.SupressFinalize: This method is used to suppress the finalization of an object that is passed to it as a parameter.

    GC.GetGeneration: This method accepts a reference of an object as a parameter and returns the generation that the object is in.

    GC.MaxGeneration: This method is used to return the maximum number of generations that are supported in the version of CLR installed.

    Security

    Security is an important aspect in any application and it is all about protecting the assets in an application from unauthorized users. Basically, Microsoft .NET supports two types of security. These include:

    Role-based security

    Code access security

    Figure 1.9 illustrates the types of security:

    Figure 1.9: Security in .NET

    Role-based security

    Role-based security is a security mechanism that provides access to resources in an application to an authenticated user based on his or her access permissions. To enforce security in a .NET application, you need to authenticate the user and authorize access to the resources that the user has access to. In other words, authentication and authorization are the key concepts as far as implementing security in .NET applications is concerned.

    Enforcing role-based security in .NET applications is quite easy. Support for role-based security in .NET is flexible and extensible as well. To ensure consistency with code access security, role-based security in .NET includes the System.Security.Permissions.PrincipalPermission objects that can be taken advantage of to perform authorization much the same way you do with code access security.

    Code access security

    Code access security (CAS) is a sandbox for .NET. It is a security mechanism that is used by the CLR to restrict untrusted code from performing privileged actions. Note that CAS can only be applied to managed applications. Unmanaged applications as the name suggests run out of the CLR’s context and hence there are no CAS restrictions. The only restriction that is applicable to unmanaged applications is the operating system’s role-based security. The components of CAS include: code group, evidence, and permissions.

    Conclusion

    This chapter presented a discussion on Microsoft .NET internals. Our journey towards exploring C# will begin in the next chapter. In the next chapter, we’ll examine how we can get started working with C#.

    Questions

    What is the difference between .NET Framework and .NET Core?

    What are the components of .NET Framework?

    What is garbage collection? How does it work in the .NET framework?

    What is the difference between managed and unmanaged code?

    What is the difference between a value type and a reference type?

    What is CIL? How does it work?

    What is the purpose of JIT compiler?

    What is DLR? Why is it needed?

    What is code access security?

    What is role-based security?

    CHAPTER 2

    Getting started with C#

    As of this writing, the latest version of C# programming language is C# 8.0. C# 8 has many new exciting features that would change the way you write code. In this chapter, we'll learn how to get started working with C# 8 in Visual Studio.

    Structure

    In this chapter, we will discuss the following points:

    Building a Hello World application

    Building a class library

    Keywords

    Constructs

    Loops

    Operators

    Value and reference types

    Arrays

    Objective

    After reading this chapter, the reader will be able to:

    Build Console applications in Visual Studio

    Build Class Library in Visual Studio

    Understand the basic programming concepts

    Learn how to work with operators

    Learn how to work with arrays

    Building a Hello World application

    In this section, we’ll examine how to build a Hello World console application using C# and Visual Studio 2019 IDE:

    Launch the Visual Studio 2019 IDE.

    Click on File | New | Project… to create a new project as shown in Figure 2.1:

    Figure 2.1

    In the Create a new project dialog, select Console App (.NET Framework) as the project template and click on Next. Refer to Figure 2.2:

    Figure 2.2

    In the Configure your new project that is shown in the next screen, specify the name and the location of the project.

    Select the .NET Framework version you would like to use from the dropdown menu at the bottom of the

    Enjoying the preview?
    Page 1 of 1