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

Only $11.99/month after trial. Cancel anytime.

Mastering C# and .NET Framework
Mastering C# and .NET Framework
Mastering C# and .NET Framework
Ebook1,076 pages10 hours

Mastering C# and .NET Framework

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

This book was written exclusively for .NET developers. If you’ve been creating C# applications for your clients, at work or at home, this book will help you develop the skills you need to create modern, powerful, and efficient applications in C#.
No knowledge of C# 6/7 or .NET 4.6 is needed to follow along—all the latest features are included to help you start writing cross-platform applications immediately. You will need to be familiar with Visual Studio, though all the new features in Visual Studio 2015 will also be covered.
LanguageEnglish
Release dateDec 15, 2016
ISBN9781785885402
Mastering C# and .NET Framework

Related to Mastering C# and .NET Framework

Related ebooks

Programming For You

View More

Related articles

Reviews for Mastering C# and .NET Framework

Rating: 5 out of 5 stars
5/5

6 ratings1 review

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    Good book but wish that there is more c# in depth

Book preview

Mastering C# and .NET Framework - Marino Posadas

Table of Contents

Mastering C# and .NET Framework

Credits

About the Author

Acknowledgements

About the Reviewer

www.PacktPub.com

eBooks, discount offers, and more

Why subscribe?

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Customer support

Downloading the example code

Errata

Piracy

Questions

1. Inside the CLR

An annotated reminder of some important computing terms

Context

The OS multitask execution model

Context types

Thread safety

State

Program state

Serialization

Process

Thread

SysInternals

Static versus dynamic memory

Garbage collector

Concurrent computing

Parallel computing

Imperative programming

Declarative programming

The evolution of .NET

.NET as a reaction to the Java World

The open source movement and .NET Core

Common Language Runtime

Common Intermediate Language

Managed execution

Components and languages

Structure of an assembly file

Program execution

Metadata

Introducing metadata with a basic Hello World

PreJIT, JIT, EconoJIT, and RyuJIT

Common Type System

Naming conventions, rules, and type access modes

Members of a type

A quick tip on the execution and memory analysis of an assembly in Visual Studio 2015

The stack and the heap

Garbage collection

Implementing algorithms with the CLR

Data structures, algorithms, and complexity

Big O Notation

An approach to performance in the most common sorting algorithms

Relevant features appearing in versions 4.5x, 4.6, and .NET Core 1.0 and 1.1

.NET 4.5.x

.NET 4.6 (aligned with Visual Studio 2015)

.NET Core 1.0

.NET Core 1.1

Summary

2. Core Concepts of C# and .NET

C# – what's different in the language?

Languages: strongly typed, weakly typed, dynamic, and static

The main differences

The true reason for delegates

The evolution in versions 2.0 and 3.0

Generics

Creating custom generic types and methods

Lambda expressions and anonymous types

Lambda expressions

It's all about signatures

The LINQ syntax

LINQ syntax is based on the SQL language

Deferred execution

Joining and grouping collections

Type projections

Extension methods

Summary

3. Advanced Concepts of C# and .NET

C# 4 and .NET framework 4.0

Covariance and contravariance

Covariance in interfaces

Covariance in generic types

Covariance in LINQ

Contravariance

Tuples: a remembrance

Tuples: implementation in C#

Tuples: support for structural equality

Tuples versus anonymous types

Lazy initialization and instantiation

Dynamic programming

Dynamic typing

The ExpandoObject object

Optional and named parameters

The Task object and asynchronous calls

C# 5.0: async/await declarations

What's new in C# 6.0

String interpolation

Exception filters

The nameof operator

The null-conditional operator

Auto-property initializers

Static using declarations

Expression bodied methods

Index initializers

What's new in C# 7.0

Binary literals and digit separators

Pattern matching and switch statements

Tuples

Decomposition

Local functions

Ref return values

Summary

4. Comparing Approaches for Programming

Functional languages

F# 4 and .NET Framework

The inevitable Hello World demo

Identifiers and scope

Lists

Function declarations

The pipeline operator

Pattern matching

Classes and types

Casting

The TypeScript language

The new JavaScript

TypeScript: a superset of JavaScript

So, what exactly is TypeScript?

Main features and coalitions

Installing the tools

Transpiling to different versions

Advantages in the IDE

A note on TypeScript's object-oriented syntax

More details and functionality

Summary

5. Reflection and Dynamic Programming

Reflection in the .NET Framework

Calling external assemblies

Generic Reflection

Emitting code at runtime

The System.CodeDOM namespace

The Reflection.Emit namespace

Interoperability

Primary Interop Assemblies

Formatting cells

Inserting multimedia in a sheet

Interop with Microsoft Word

Office apps

The Office app default project

Architectural differences

Summary

6. SQL Database Programming

The relational model

Properties of relational tables

The tools – SQL Server 2014

The SQL language

SQL Server from Visual Studio

Data access in Visual Studio

.NET data access

Using ADO.NET basic objects

Configuring the user interface

The Entity Framework data model

Summary

7. NoSQL Database Programming

A brief historical context

The NoSQL world

Architectural changes with respect to RDBMS

Querying multiple queries

The problem of nonnormalized data

Data nesting

About CRUD operations

MongoDB on Windows

File structure and default configuration

Some useful commands

Operators

Altering data – the rest of CRUD operations

Text indexes

MongoDB from Visual Studio

First demo: a simple query from Visual Studio

CRUD operations

Deletion

Insertion

Modifications and replacements

Summary

8. Open Source Programming

Historical open source movements

Other projects and initiatives

Open source code for the programmer

Other languages

The Roslyn project

Differences from traditional compilers

Getting started with Roslyn

A first look at Microsoft Code Analysis Services

Code Analyzers

An entire open source sample for you to check: ScriptCS

A basic project using Microsoft.CodeAnalysis

The first approach to code refactoring

Debugging and testing the demo

TypeScript

Debugging TypeScript

Debugging TypeScript with Chrome

Interfaces and strong typing

Implementing namespaces

Declarations, scope, and Intellisense

Scope and encapsulation

Classes and class inheritance

Functions

Arrays and interfaces

More TypeScript in action

The DOM connection

Summary

9. Architecture

The election of an architecture

The Microsoft platform

A universal platform

The MSF application model

The Team Model

The Governance Model

The Risk Model

Risk evaluation

Risk assessment

Risk action plans

CASE tools

The role of Visio

A first example

The database design

Creating the demo application in Visual Studio

Website design

Reports

Many other options

BPMN 2.0 (Business Process Model and Notation)

UML standard support

Visual Studio architecture, testing, and analysis tools

Application's architecture using Visual Studio

Class diagrams

Testing

Testing our application in Visual Studio

The Analyze menu

The end of the life cycle – publishing the solution

Summary

10. Design Patterns

The origins

The SOLID principles

Single Responsibility principle

An example

Open/Closed principle

Back to our sample

Liskov Substitution principle

Back to the code again

Other implementations of LSP in .NET (Generics)

Interface Segregation principle

Dependency Inversion principle

A final version of the sample

Design patterns

Singleton

The Factory pattern

The Adapter pattern

The Façade pattern

The Decorator pattern

The Command pattern

An example already implemented in .NET

The Observer pattern

The Strategy pattern

Other software patterns

Other patterns

Summary

11. Security

The OWASP initiative

The OWASP Top 10

A1 – injection

SQL injection

Prevention

The case for NoSQL databases

A2 – Broken Authentication and Session Management

The causes

Prevention

.NET coding for A2

Desktop applications

Web applications

A3 – Cross-Site Scripting (XSS)

Prevention

A4 – Insecure Direct Object References

Prevention

A5 – Security Misconfiguration

Possible examples of attacks

Prevention – aspects to consider

Prevention – measures

A6 – Sensitive Data Exposure

A7 – Missing Function-level Access Control

Prevention

A8 – Cross-Site Request Forgery

Prevention

A9 – Using components with known vulnerabilities

A10 – Invalidated redirects and forwards

Summary

12. Performance

Application Performance Engineering

The tools

Advanced options in Visual Studio 2015

Advanced options in the Diagnostic Tools menu

Other tools

The process of performance tuning

Performance Counters

Bottleneck detection

Bottleneck detection in practice

Using code to evaluate performance

Optimizing web applications

IIS optimization

ASP.NET optimization

General and configuration

Caching

Data access

Load balancing

Client side

Summary

13. Advanced Topics

The Windows messaging subsystem

The MSG structure

Sub-classing techniques

Some useful tools

Platform/Invoke: calling the OS from .NET

The process of platform invocation

Windows Management Instrumentation

CIM searchable tables

Parallel programming

Difference between multithreading and parallel programming

Parallel LINQ

Dealing with other issues

Canceling execution

The Parallel class

The Parallel.ForEach version

Task Parallel

Communication between threads

.NET Core 1.0

The list of supported environments

Core FX

Core CLR

Core RT

Core CLI

Installation of .NET Core

The CLI interface

ASP.NET Core 1.0

What's new

A first approach

Configuration and Startup settings

Self-hosted applications

ASP.NET Core 1.0 MVC

Managing scripts

NET Core 1.1

Summary

Index

Mastering C# and .NET Framework


Mastering C# and .NET Framework

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2016

Production reference: 1091216

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-437-5

www.packtpub.com

Credits

Author

Marino Posadas

Reviewers

Fabio Claudio Ferracchiati

Commissioning Editor

Edward Gordon

Acquisition Editor

Denim Pinto

Content Development Editor

Priyanka Mehta

Technical Editor

Dhiraj Chandanshive

Copy Editor

Stuti Srivastava

Project Coordinator

Izzat Contractor

Proofreader

Safis Editing

Indexer

Rekha Nair

Graphics

Disha Haria

Production Coordinator

Aparna Bhagat

Cover Work

Aparna Bhagat

About the Author

Marino Posadas is an independent senior trainer, writer, and consultant in Microsoft Technologies and Web Standards. He is a Microsoft MVP in C#, Visual Studio, and Development Technologies; an MCT, MAP (2013), MCPD, MCTS, MCAD, MCSD, and MCP. Additionally, he was the former director for development in Spain and Portugal for Solid Quality Mentors.

He's published 14 books and more than 500 articles on development technologies in several magazines. Topics covered in his books range from Clipper and Visual Basic 5.0/6.0 to C #, .NET-safe programming, Silverlight 2.0 and 4.0, and Web Standards. The Guide to Programming in HTML5, CSS3 and JavaScript with Visual Studio is his latest book.

He is also a speaker at Microsoft events, having lectured in Spain, Portugal, England, the USA, Costa Rica, and Mexico.

You can find him on LinkedIn at https://es.linkedin.com/in/mposadas.

His website, http//elavefenix.net, also contains developer's resources and videos, in English and Spanish, interviewing representatives of the Microsoft and Web Standards development world.

Acknowledgements

I'd like to thank Denim Pinto, Priyanka Mehta, and Fabio Claudio Ferracchiati from Packt Publishing for their continuous support and confidence while writing this book.

Special thanks to some professionals and technology evangelists whose work inspired different parts of this book, in particular, Mark Russinowich, Scott Hanselman, Scott Hunter (the lesser Scotts), Daniel Roth, Lluis Franco, Luis Ruiz Pavón, Dino Esposito, Miguel Katrib, James Gray, Paul Cotton, Stephen Toub, and Troy Hunt.

Also, I would like to remember my MVP lead, Cristina González Herrero, for her continuous encouragement and help, and other people at Microsoft who have always supported my activities. My memories go here to Alfonso Rodríguez, David Carmona, David Salgado, José Bonnín, César de la Torre, Andy Gonzalez, and Leon Welicki.

My appreciation also goes to my mates at Netmind, Alex and Bernat Palau, Miquel Rodriguez, Israel Zorrilla, and Angel Rayo, for their support in this initiative from the beginning.

About the Reviewer

Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/developer using Microsoft technologies. He works for React Consulting (www.reactconsulting.it) as Microsoft Dynamics 365 Solution Architect. He is a Microsoft Certified Solution Developer for .NET, a Microsoft Certified Application Developer for .NET, a Microsoft Certified Professional, and a prolific author and technical reviewer. Over the past 10 years, he's written articles for Italian and international magazines and coauthored more than 10 books on a variety of computer topics.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

I dedicate this book to my wife, Milagros, and my family, with a special mention to my nephews and nieces: Fernando, Sarah, Ana, Paula, Pablo, Javier, Adrian, Irene, Luis, and Juan.

Preface

.NET and the C# language have continuously grown in adoption since its release in early 2001. C#'s main author, Anders Hejlsberg, has lead several groups of developers in constant growing and improvement, until reaching the current version, .NET 4.6, and the very important .NET Core 1.0/1.1, and keeps on going with this work, also linked to the new TypeScript language, which we will also cover in this book.

This book is a journey through the different options and possibilities .NET Framework in general, and C# in particular, provide to developers to build applications that run on Windows and, as seen in the last chapter, on other platforms and devices.

I believe that it can be a reference for programmers wanting to update their knowledge to the latest versions of this set of technologies, but also for those who, coming from other environments, would like to approach .NET and the C# language to extend their skills and programming toolset.

All the main points discussed in here are illustrated with examples, and the important parts of these demos are explained in detail, so you can easily follow this route.

What this book covers

Chapter 1, Inside the CLR, goes through the internal structure of .NET, the way assemblies are built, the tools and resources we have to work with them, and the way .NET integrates with the operating system.

Chapter 2, Core Concepts of C# and .NET, reviews the foundations of the language, its main characteristics, and some of the true reasons for the appearance of certain features, such as delegates.

Chapter 3, Advanced Concepts of C# and .NET, starts with version 4.0, viewing some common practices new to the language and Framework libraries, especially those related to synchronicity, execution threads, and dynamic programming. Finally, we can find many new aspects that appeared in versions 6.0 and 7.0, intended to simplify the way we write code.

Chapter 4, Comparing Approaches for Programming, deals with two members of the .NET language ecosystem: F# and TypeScript (also called functional languages), which are gaining momentum among the programmer's community.

Chapter 5, Reflection and Dynamic Programming, covers the ability of a .NET program to examine, introspect, and modify its own structure and behavior, and also how to interoperate with other programs, such as the Office Suite.

Chapter 6, SQL Database Programming, deals with access to databases built according to the principles of the Relational Model, and in particular to SQL databases. It covers Entity Framework 6.0 and gives a brief reminder of ADO.NET.

Chapter 7, NoSQL Database Programming, reviews the emerging database paradigm called NoSQL databases. We will use MongoDB, the most popular of its kind, and see how to manage it from C# code.

Chapter 8, Open Source Programming, goes through the current state of open source programming with Microsoft technologies, the open source ecosystem. We will review Node.js, Roselyn, and also TypeScript, although with a slightly different point of view.

Chapter 9, Architecture, goes through the structure of an application and the tools available in its construction, such as MSF, good practices, and so on.

Chapter 10, Design Patterns, focuses on the quality of the code and its structures in terms of efficacy, precision, and maintainability. It deals with SOLID principles, the Gang of Four patterns, and other proposals.

Chapter 11, Security, analyzes the OWASP Top 10 security recommendations from the point of view of the .NET developer.

Chapter 12, Performance, deals with common issues that a developer encounters in relation to an application's performance, and which techniques and tips are commonly suggested in order to obtain flexible, responsive, and well-behaved software, with a special emphasis on web performance.

Chapter 13, Advanced Topics, covers interaction with the OS via subclassing and platform/invoke, system data retrieval through WMI, parallel programming, and an introduction to the new .NET Core and ASP.NET Core multiplatform technologies.

What you need for this book

As this book is dedicated to C# and .NET, the main tool to use is Visual Studio. However, you can use several versions to follow the majority of the sections in this book.

I've used Visual Studio 2015 Ultimate Update 3, but you can also use the free Community Edition for more than 90% of its contents. Other available options are Visual Studio 2015 Express Edition, which is also free, and Visual Studio Code, which is also free and multiplatform.

Additionally, a basic installation of SQL Server Express 2014, which is free, is required together with the SQL Server Management Studio (version 2016 works equally well with the topics covered here).

For the NoSQL part, MongoDB is also required in its basic installation.

To debug websites, it's good to have Chrome Canary or Firefox Developer Edition, for they have extended capabilities for developers.

Other tools and utilities can be installed from the Extensions and Updates menu option, linked to the Tools menu in the different versions of Visual Studio.

Finally, in some cases, there are tools that you can download from the sites indicated in this book; although, they're not an absolute requirement for the full comprehension of this book's contents.

Who this book is for

This book was written exclusively for .NET developers. If you're creating C# applications for your clients, at work or at home, this book will help you develop the skills you need to create modern, powerful, and efficient applications in C#.

No knowledge of C# 6/7 or .NET 4.6 is needed to follow along—all the latest features are included to help you start writing cross-platform applications immediately. You will need to be familiar with Visual Studio, although all the new features in Visual Studio 2015 will also be covered.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: We use the ForEach method, which receives an Action delegate argument.

A block of code is set as follows:

static void GenerateStrings()

{

  string initialString = Initial Data-;

  for (int i = 0; i < 5000; i++)

  {

    initialString += -More data-;

  }

  Console.WriteLine(Strings generated);

}

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: In the Memory Usage tab, we can take a snapshot of what's going on.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.

Hover the mouse pointer on the SUPPORT tab at the top.

Click on Code Downloads & Errata.

Enter the name of the book in the Search box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-C-Sharp-and-.NET-Framework. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <copyright@packtpub.com> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>, and we will do our best to address the problem.

Chapter 1. Inside the CLR

Since CLR is just a generic name for different tools and software based on well-known and accepted principles in computing, we'll begin with a review of some of the most important concepts of software programming that we often take for granted. So, to put things in context, this chapter reviews the most important concepts around the motivations for the creation of .NET, how this framework integrates with the Windows operating system, and what makes the so called CLR the excellent runtime it is.

In short, this chapter covers the following topics:

A brief, but carefully selected, dictionary of the common terms and concepts utilized in general and .NET programming

A rapid review of goals after the creation of .NET and the main architects behind its construction

Explanations of each of the main parts that compose the CLR, its tools, and how the tools work

A basic approach to the complexity of algorithms and how to measure it

A select list of the most outstanding characteristics related to the CLR that appeared in recent versions

An annotated reminder of some important computing terms

Let's check out some important concepts widely used in software construction that show up frequently in .NET programming.

Context

As Wikipedia states:

In computer science, a task context is the minimal set of data used by a task (which may be a process or thread) that must be saved to allow a task interruption at a given date, and a continuation of this task at the point it has been interrupted and at an arbitrary future date.

In other words, context is a term related to the data handled by a thread. Such data is conveniently stored and recovered by the system as required.

Practical approaches to this concept include HTTP request/response and database scenarios in which the context plays a very important role.

The OS multitask execution model

A CPU is able to manage multiple processes in a period of time. As we mentioned, this is achieved by saving and restoring (in an extremely fast manner) the context of execution with a technique called context switch.

When a thread ceases to execute, it is said to be in the Idle state. This categorization might be useful at the time of analyzing processes execution with the tools that are able to isolate threads in the Idle state:

Context types

In some languages, such as C#, we also find the concept of safe or secure context. In a way, this relates to the so-called thread safety.

Thread safety

A piece of code is said to be thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. There are various strategies used in order to create thread-safe data structures, and the .NET framework is very careful about this concept and its implementations.

Actually, most of the MSDN (the official documentation) includes the indication this type is thread-safe at the bottom for those to whom it is applicable (a vast majority).

State

The state of a computer program is a technical term for all the stored information, at a given instant in time, to which the program has access. The output of a computer program at any time is completely determined by its current inputs and its state. A very important variant of this concept is the program's state.

Program state

This concept is especially important, and it has several meanings. We know that a computer program stores data in variables, which are just labeled storage locations in the computer's memory. The contents of these memory locations, at any given point in the program's execution, are called the program's state.

In object-oriented languages, it is said that a class defines its state through fields, and the values that these fields have at a given moment of execution determine the state of that object. Although it's not mandatory, it's considered a good practice in OOP programming when the methods of a class have the sole purpose of preserving the coherence and logic of its state and nothing else.

In addition, a common taxonomy of programming languages establishes two categories: imperative and declarative programming. C# or Java are examples of the former, and HTML is a typical declarative syntax (since it's not a language itself). Well, in declarative programming, sentences tend to change the state of the program while using the declarative paradigm, languages indicate only the desired result, with no specifications about how the engine will manage to obtain the results.

Serialization

Serialization is the process of translating data structures or the object state into a format that can be stored (for example, in a file or a memory buffer) or transmitted across a network connection and reconstructed later in the same or another computer environment.

So, we used to say that serializing an object means to convert its state into a byte stream in such a way that the byte stream can be converted back into a copy of the object. Popular text formats emerged years ago and are now well known and accepted, such as XML and JSON, independently of other previous formats (binary included):

Process

The OS fragments operations among several functional units. This is done by allocating different memory areas for each unit in execution. It's important to distinguish between processes and threads.

Each process is given a set of resources by the OS, which—in Windows—means that a process will have its own virtual address space allocated and managed accordingly. When Windows initializes a process, it is actually establishing a context of execution, which implies a process environment block, also known as PEB and a data structure. However, let's make this clear: the OS doesn't execute processes; it only establishes the execution context.

Thread

A thread is the functional (or working) unit of a process. And that is what the OS executes. Thus, a single process might have several threads of execution, which is something that happens very often. Each thread has its own address space within the resources previously allocated by the creation of the process. These resources are shared by all threads linked to the process:

It's important to recall that a thread only belongs to a single process, thus having access to only the resources defined by that process. When using the tools that will be suggested now, we can look at multiple threads executing concurrently (which means that they start working in an independent manner) and share resources, such as memory and data.

Different processes do not share these resources. In particular, the threads of a process share its instructions (the executable code) and its context (the values of its variables at any given moment).

Programming languages such as .NET languages, Java, or Python expose threading to the developer while abstracting the platform-specific differences in threading implementations at runtime.

Tip

Note that communication between threads is possible through the common set of resources initialized by the process creation.

Of course, there is much more written about these two concepts, which go far beyond the scope of this book (refer to Wikipedia, https://en.wikipedia.org/wiki/Thread_(computing), for more details), but the system provides us with mechanisms to check the execution of any process and also check what the threads in execution are.

If you are curious about it or just need to check whether something is going wrong, there are two main tools that I recommend: the Task Manager (included in the operating system, which you'll probably know), and—even better—one of the tools designed by the distinguished engineer and technical fellow Mark Russinowitch, available for free and composed of a set of more than 50 utilities.

Some have a Windows interface and others are console utilities, but all of them are highly optimized and configurable to monitoring and controlling the inner aspects of our operating system at any moment. They are available for free at https://technet.microsoft.com/en-us/sysinternals/bb545021.aspx.

If you don't want to install anything else, open Task Manager (just right-click on the task bar to access it) and select the Details tab. You will see a more detailed description of every process, the amount of CPU used by each process, the memory allocated for each process, and so on. You can even right-click on one of the processes and see how there is a context menu that offers a few possibilities, including launching a new dialog window that shows some properties related to it:

SysInternals

If you really want to know how a process behaves in its entirety, the tools to use are SysInternals. If you go to the link indicated earlier, you'll see an item menu especially dedicated to process utilities. There, you have several choices to work with, but the most comprehensive are Process Explorer and Process Monitor.

Process Explorer and Process Monitor don't require installation (they're written in C++), so you can execute them directly from any device for a Windows platform.

For example, if you run Process Explorer, you'll see a fully detailed window showing every single detail of all the processes currently active in the system.

With Process Explorer, you can find out what files, registry keys, and other objects processes have opened, together with the DLLs they have loaded, who owns each process, and so on. Every thread is visible and the tool provides you with detailed information, available through a very intuitive user interface:

It's also very useful to check the system's general behavior at real time, since it creates graphics of activities of CPU usage, I/O, Memory, among others, as shown in the following screenshot:

In a similar way, Process Monitor, focuses on monitoring the filesystem, the Registry, and all processes and threads with their activities in real time, since it actually is a mixture of two previous utilities merged together: FileMon (File Monitor) and RegMon (Registry Monitor), which are not available anymore.

If you try out PM, you'll see some of the information included in PE, plus the specific information provided by PM—just conveyed in a different manner.

Static versus dynamic memory

When a program starts execution, the OS assigns a process to it by means of scheduling: the method by which work specified by some means is assigned to resources that complete the work. This means that the resources for the process are assigned, and that implies memory allocation.

As we'll see, there are mainly two types of memory allocation:

Fixed memory (linked to the stack), determined at compile time. Local variables are declared and used in the stack. Note that it is a contiguous block of memory allocated when the process resources are initially assigned. The allocation mechanism is very fast (although the access not so much).

The other is dynamic memory (the heap), which can grow as the program required it, and it's assigned at runtime. This is the place where instance variables are allocated (those that point to an instance of a class or object).

Usually, the first type is calculated at compile time since the compiler knows how much memory will be needed to allocate the variables declared depending on its type (int, double, and so on). They are declared inside functions with a syntax such as int x = 1;

The second type requires the new operator to be invoked. Let's say there is a class named Book in our code, we create an instance of such Book with an expression of this type:

Book myBook = new Book();

This instructs the runtime to allocate enough space in the heap to hold an instance of that type along with its fields; the state of the class is allocated in the heap. This means that the whole state of a program will store its state in a different memory (and, optionally, disk) locations.

Of course, there are more aspects to account for, which we'll cover in the The Stack and the Heap section in this chapter. Luckily, the IDE lets us watch and analyze all these aspects (and many more) at debug time, offering an extraordinary debugging experience.

Garbage collector

Garbage collection (GC) is a form of automatic memory management. The GC in .NET, attempts to reclaim garbage or the memory occupied by objects that are no longer in use by the program. Going back to the previous code declaration of Book, when there are no references to the Book object in the stack, the GC will reclaim that space to the system, liberating memory (it's a bit more complex, in fact, and I'll get into further detail later in this chapter—when we talk about memory management—but let's put it that way for the moment).

It's important to note that garbage collectors are not something exclusive to the .NET platform. Actually, you can find it in all platforms and programs even if you're dealing with browsers. Current JavaScript engines, for instance, such as Chrome's V8, Microsoft's Chakra—and others—use a garbage collection mechanism as well.

Concurrent computing

Concurrency or concurrent computing is a very common concept nowadays, and we'll discover it at several instances along this book. The official definition in Wikipedia (https://en.wikipedia.org/wiki/Concurrent_computing) says:

Concurrent computing is a form of computing in which several computations are executed during overlapping time periods—concurrently—instead of sequentially (one completing before the next starts). This is a property of a system—this may be an individual program, a computer, or a network—and there is a separate execution point or thread of control for each computation (process). A concurrent system is one where a computation can advance without waiting for all other computations to complete; where more than one computation can advance at the same time.

Parallel computing

Parallel computing is a type of computation in which many calculations are carried out simultaneously, operating on the principle that large problems can often be divided into smaller ones, which are then solved at the same time. .NET offers several variants of this type of computing, which we'll cover over the next few chapters:

Imperative programming

Imperative programming is a programming paradigm that describes computation in terms of the program's state. C#, JavaScript, Java, or C++ are typical examples of imperative languages.

Declarative programming

In contrast to imperative programming, languages considered declarative describe only the desired results without explicitly listing commands or steps that must be performed. Many markup languages, such as HTML, XAML, or XSLT, fall into this category.

The evolution of .NET

Until the arrival of .NET, the Microsoft programming ecosystem had been ruled by a few classic languages, Visual Basic and C++ (with the Microsoft Foundation classes) being typical examples of this.

Note

Also known as MFC, Microsoft Foundation Classes is a library that wraps portions of the Windows API in C++ classes, including functionalities that enable them to use a default application framework. Classes are defined for many of the handle-managed Windows objects and also for predefined windows and common controls. It was introduced in 1992 with Microsoft's C/C++ 7.0 compiler for use with 16-bit versions of Windows as an extremely thin object-oriented C++ wrapper for the Windows API.

However, the big changes proposed by .NET were started using a totally different component model approach. Up until 2002, when .NET officially appeared, such a component model was COM (Component Object Model), introduced by the company in 1993. COM is the basis for several other Microsoft technologies and frameworks, including OLE, OLE automation, ActiveX, COM+, DCOM, the Windows shell, DirectX, UMDF (User-Mode Driver Framework), and Windows runtime.

Note

A device-driver development platform (Windows Driver Development Kit) first introduced with Microsoft's Windows Vista operating system is also available for Windows XP. It facilitates the creation of drivers for certain classes of devices.

At the time of writing this, COM is a competitor with another specification named CORBA (Common Object Request Broker Architecture), a standard defined by the Object Management Group (OMG), designed to facilitate the communication of systems that are deployed on diverse platforms. CORBA enables collaboration between systems on different operating systems, programming languages, and computing hardware. In its life cycle, it has received a lot of criticism, mainly because of poor implementations of the standard.

.NET as a reaction to the Java World

In 1995, a new model was conceived to supersede COM and the unwanted effects related to it, especially versions and the use of the Windows Registry on which COM depends to define accessible interfaces or contracts; a corruption or modified fragment of the registry could indicate that a component was not accessible at runtime. Also, in order to install applications, elevated permissions were required, since the Windows Registry is a sensible part of the system.

A year later, various quarters of Microsoft started making contacts with some of the most distinguished software engineers, and these contacts remained active over the years. These included architects such as Anders Hejlsberg (who became the main author of C# and the principal architect of .NET framework), Jean Paoli (one of the signatures in the XML Standard and the former ideologist of AJAX technologies), Don Box (who participated in the creation of SOAP and XML Schemas), Stan Lippman (one of the fathers of C++, who was working at the time at Disney), Don Syme (the architect for generics and the principal author of the F# language), and so on.

The purpose of this project was to create a new execution platform, free from the caveats of COM and one that was able to hold a set of languages to execute in a secure and extensible manner. The

Enjoying the preview?
Page 1 of 1