Mastering C# 8.0: Master C# Skills with Hands-on Code Examples (English Edition)
()
About this ebook
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.
Read more from Joydip Kanjilal
Entity Framework Tutorial Rating: 0 out of 5 stars0 ratingsASP.NET Data Presentation Controls Essentials Rating: 0 out of 5 stars0 ratingsEntity Framework Tutorial - Second Edition Rating: 0 out of 5 stars0 ratings
Related to Mastering C# 8.0
Related ebooks
C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsASP.NET Core for Jobseekers: Build Career in Designing Cross-Platform Web Applications Using Razor and Entity Framework Core Rating: 0 out of 5 stars0 ratingsC# 7 and .NET Core Cookbook Rating: 0 out of 5 stars0 ratingsEnterprise Applications with C# and .NET: Develop robust, secure, and scalable applications using .NET and C# (English Edition) Rating: 0 out of 5 stars0 ratingsBuilding a Web App with Blazor and ASP .Net Core: Create a Single Page App with Blazor Server and Entity Framework Core Rating: 0 out of 5 stars0 ratingsParallel Programming with C# and .NET Core: Developing Multithreaded Applications Using C# and .NET Core 3.1 from Scratch Rating: 0 out of 5 stars0 ratingsC# Programming & Software Development: 6 In 1 Coding Syntax, Expressions, Interfaces, Generics And App Debugging Rating: 0 out of 5 stars0 ratingsMastering Windows Presentation Foundation Rating: 4 out of 5 stars4/5Art of Clean Code: How to Write Codes for Human Rating: 3 out of 5 stars3/5100+ Solutions in Java: A Hands-On Introduction to Programming in Java (English Edition) Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsThe basic concepts of OOP in C#: Learn conceptually in simple language Rating: 0 out of 5 stars0 ratingsMastering C# and .NET Framework Rating: 5 out of 5 stars5/5C# Programming from Zero to Proficiency (Introduction): C# from Zero to Proficiency, #0 Rating: 0 out of 5 stars0 ratingsFunctional C# Rating: 5 out of 5 stars5/5Thinking In C# Programming. Rating: 0 out of 5 stars0 ratingsC# Programming Fundamentals Rating: 0 out of 5 stars0 ratingsHands-On Network Programming with C# and .NET Core: Build robust network applications with C# and .NET Core Rating: 0 out of 5 stars0 ratingsCode like a Pro in C# Rating: 0 out of 5 stars0 ratingsA Concise Guide to Object Orientated Programming Rating: 0 out of 5 stars0 ratingsProfessional C++ Rating: 2 out of 5 stars2/5
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsHTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5
Reviews for Mastering C# 8.0
0 ratings0 reviews
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