Deep dive into C# and .NET architecture to build efficient, powerful applications About This Book Uniquely structured content to help you understand what goes on under the hood of .NET's managed code platform to master .NET programming Deep dive into C# programming and how the code executes via the CLR Packed with hands-on practical examples, you'll understand how to write applications to make full use of the new features of .NET 4.6, .NET Core and C# 6/7 Who This Book Is For 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. What You Will Learn Understand C# core concepts in depth, from sorting algorithms to the Big O notation Get up to speed with the latest changes in C# 6/7 Interface SQL Server and NoSQL databases with .NET Apprehend SOLID principles and the most relevant GoF Patterns with practical examples in C# 6.0 Defend C# applications against attacks Use Roslyn, a self-hosted framework to compile and advanced edition in both C# and Visual basic .NET languages Discern LINQ and associated Lambda expressions, generics, and delegates Design a .NET application from the ground up Understand the internals of a .NET assembly Grasp some useful advanced features in optimization and parallelism In Detail Mastering C# and .NET Framework will take you in to the depths of C# 6.0/7.0 and .NET 4.6, so you can understand how the platform works when it runs your code, and how you can use this knowledge to write efficient applications. Take full advantage of the new revolution in .NET development, including open source status and cross-platform capability, and get to grips with the architectural changes of CoreCLR. Start with how the CLR executes code, and discover the niche and advanced aspects of C# programming from delegates and generics, through to asynchronous programming. Run through new forms of type declarations and assignments, source code callers, static using syntax, auto-property initializers, dictionary initializers, null conditional operators, and many others. Then unlock the true potential of the .NET platform. Learn how to write OWASP-compliant applications, how to properly implement design patterns in C#, and how to follow the general SOLID principles and its implementations in C# code. We finish by focusing on tips and tricks that you'll need to get the most from C# and .NET. This book also covers .NET Core 1.1 concepts as per the latest RTM release in the last chapter. Style and approach This book uses hands-on practical code examples that will take you into the depths of C# and .NET. Packed with hands-on practical examples, it is great as a tutorial, or as a reference guide."
Author(s): Marino Posadas
Publisher: Packt Publishing
Year: 2016
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