O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering C# and .NET Framework

Book Description

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
  • Learn 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.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. Mastering C# and .NET Framework
    1. Table of Contents
    2. Mastering C# and .NET Framework
    3. Credits
    4. About the Author
    5. Acknowledgements
    6. About the Reviewer
    7. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Inside the CLR
      1. An annotated reminder of some important computing terms
        1. Context
        2. The OS multitask execution model
        3. Context types
        4. Thread safety
        5. State
        6. Program state
        7. Serialization
        8. Process
        9. Thread
        10. SysInternals
        11. Static versus dynamic memory
        12. Garbage collector
        13. Concurrent computing
        14. Parallel computing
        15. Imperative programming
        16. Declarative programming
      2. The evolution of .NET
        1. .NET as a reaction to the Java World
          1. The open source movement and .NET Core
        2. Common Language Runtime
          1. Common Intermediate Language
          2. Managed execution
          3. Components and languages
          4. Structure of an assembly file
            1. Program execution
        3. Metadata
          1. Introducing metadata with a basic Hello World
          2. PreJIT, JIT, EconoJIT, and RyuJIT
          3. Common Type System
            1. Naming conventions, rules, and type access modes
            2. Members of a type
          4. A quick tip on the execution and memory analysis of an assembly in Visual Studio 2015
          5. The stack and the heap
          6. Garbage collection
        4. Implementing algorithms with the CLR
          1. Data structures, algorithms, and complexity
          2. Big O Notation
            1. An approach to performance in the most common sorting algorithms
        5. Relevant features appearing in versions 4.5x, 4.6, and .NET Core 1.0 and 1.1
          1. .NET 4.5.x
          2. .NET 4.6 (aligned with Visual Studio 2015)
          3. .NET Core 1.0
          4. .NET Core 1.1
      3. Summary
    10. 2. Core Concepts of C# and .NET
      1. C# – what's different in the language?
      2. Languages: strongly typed, weakly typed, dynamic, and static
        1. The main differences
      3. The true reason for delegates
      4. The evolution in versions 2.0 and 3.0
        1. Generics
          1. Creating custom generic types and methods
        2. Lambda expressions and anonymous types
          1. Lambda expressions
            1. It's all about signatures
        3. The LINQ syntax
          1. LINQ syntax is based on the SQL language
          2. Deferred execution
          3. Joining and grouping collections
          4. Type projections
        4. Extension methods
      5. Summary
    11. 3. Advanced Concepts of C# and .NET
      1. C# 4 and .NET framework 4.0
        1. Covariance and contravariance
          1. Covariance in interfaces
          2. Covariance in generic types
          3. Covariance in LINQ
          4. Contravariance
        2. Tuples: a remembrance
        3. Tuples: implementation in C#
        4. Tuples: support for structural equality
        5. Tuples versus anonymous types
        6. Lazy initialization and instantiation
        7. Dynamic programming
        8. Dynamic typing
        9. The ExpandoObject object
        10. Optional and named parameters
        11. The Task object and asynchronous calls
      2. C# 5.0: async/await declarations
      3. What's new in C# 6.0
        1. String interpolation
        2. Exception filters
        3. The nameof operator
        4. The null-conditional operator
        5. Auto-property initializers
        6. Static using declarations
        7. Expression bodied methods
        8. Index initializers
      4. What's new in C# 7.0
        1. Binary literals and digit separators
        2. Pattern matching and switch statements
          1. Tuples
          2. Decomposition
          3. Local functions
          4. Ref return values
      5. Summary
    12. 4. Comparing Approaches for Programming
      1. Functional languages
        1. F# 4 and .NET Framework
          1. The inevitable Hello World demo
          2. Identifiers and scope
          3. Lists
            1. Function declarations
            2. The pipeline operator
            3. Pattern matching
            4. Classes and types
            5. Casting
      2. The TypeScript language
        1. The new JavaScript
        2. TypeScript: a superset of JavaScript
        3. So, what exactly is TypeScript?
          1. Main features and coalitions
          2. Installing the tools
          3. Transpiling to different versions
          4. Advantages in the IDE
        4. A note on TypeScript's object-oriented syntax
        5. More details and functionality
      3. Summary
    13. 5. Reflection and Dynamic Programming
      1. Reflection in the .NET Framework
        1. Calling external assemblies
        2. Generic Reflection
        3. Emitting code at runtime
          1. The System.CodeDOM namespace
          2. The Reflection.Emit namespace
      2. Interoperability
        1. Primary Interop Assemblies
          1. Formatting cells
          2. Inserting multimedia in a sheet
        2. Interop with Microsoft Word
        3. Office apps
          1. The Office app default project
          2. Architectural differences
      3. Summary
    14. 6. SQL Database Programming
      1. The relational model
        1. Properties of relational tables
      2. The tools – SQL Server 2014
        1. The SQL language
        2. SQL Server from Visual Studio
      3. Data access in Visual Studio
        1. .NET data access
        2. Using ADO.NET basic objects
          1. Configuring the user interface
      4. The Entity Framework data model
      5. Summary
    15. 7. NoSQL Database Programming
      1. A brief historical context
      2. The NoSQL world
        1. Architectural changes with respect to RDBMS
          1. Querying multiple queries
          2. The problem of nonnormalized data
          3. Data nesting
        2. About CRUD operations
      3. MongoDB on Windows
        1. File structure and default configuration
          1. Some useful commands
            1. Operators
        2. Altering data – the rest of CRUD operations
          1. Text indexes
      4. MongoDB from Visual Studio
        1. First demo: a simple query from Visual Studio
        2. CRUD operations
          1. Deletion
          2. Insertion
          3. Modifications and replacements
      5. Summary
    16. 8. Open Source Programming
      1. Historical open source movements
        1. Other projects and initiatives
        2. Open source code for the programmer
          1. Other languages
      2. The Roslyn project
        1. Differences from traditional compilers
        2. Getting started with Roslyn
        3. A first look at Microsoft Code Analysis Services
          1. Code Analyzers
          2. An entire open source sample for you to check: ScriptCS
          3. A basic project using Microsoft.CodeAnalysis
        4. The first approach to code refactoring
          1. Debugging and testing the demo
      3. TypeScript
        1. Debugging TypeScript
        2. Debugging TypeScript with Chrome
        3. Interfaces and strong typing
        4. Implementing namespaces
        5. Declarations, scope, and Intellisense
          1. Scope and encapsulation
        6. Classes and class inheritance
          1. Functions
          2. Arrays and interfaces
        7. More TypeScript in action
        8. The DOM connection
      4. Summary
    17. 9. Architecture
      1. The election of an architecture
        1. The Microsoft platform
        2. A universal platform
        3. The MSF application model
          1. The Team Model
        4. The Governance Model
        5. The Risk Model
          1. Risk evaluation
          2. Risk assessment
          3. Risk action plans
      2. CASE tools
      3. The role of Visio
        1. A first example
      4. The database design
        1. Creating the demo application in Visual Studio
        2. Website design
        3. Reports
        4. Many other options
          1. BPMN 2.0 (Business Process Model and Notation)
        5. UML standard support
      5. Visual Studio architecture, testing, and analysis tools
        1. Application's architecture using Visual Studio
          1. Class diagrams
        2. Testing
          1. Testing our application in Visual Studio
          2. The Analyze menu
      6. The end of the life cycle – publishing the solution
      7. Summary
    18. 10. Design Patterns
      1. The origins
      2. The SOLID principles
        1. Single Responsibility principle
          1. An example
      3. Open/Closed principle
        1. Back to our sample
      4. Liskov Substitution principle
        1. Back to the code again
        2. Other implementations of LSP in .NET (Generics)
      5. Interface Segregation principle
      6. Dependency Inversion principle
        1. A final version of the sample
      7. Design patterns
        1. Singleton
        2. The Factory pattern
        3. The Adapter pattern
        4. The Façade pattern
        5. The Decorator pattern
        6. The Command pattern
          1. An example already implemented in .NET
        7. The Observer pattern
        8. The Strategy pattern
      8. Other software patterns
      9. Other patterns
      10. Summary
    19. 11. Security
      1. The OWASP initiative
      2. The OWASP Top 10
      3. A1 – injection
        1. SQL injection
        2. Prevention
        3. The case for NoSQL databases
      4. A2 – Broken Authentication and Session Management
        1. The causes
          1. Prevention
        2. .NET coding for A2
          1. Desktop applications
        3. Web applications
      5. A3 – Cross-Site Scripting (XSS)
        1. Prevention
      6. A4 – Insecure Direct Object References
        1. Prevention
      7. A5 – Security Misconfiguration
        1. Possible examples of attacks
          1. Prevention – aspects to consider
          2. Prevention – measures
      8. A6 – Sensitive Data Exposure
      9. A7 – Missing Function-level Access Control
        1. Prevention
      10. A8 – Cross-Site Request Forgery
        1. Prevention
      11. A9 – Using components with known vulnerabilities
      12. A10 – Invalidated redirects and forwards
      13. Summary
    20. 12. Performance
      1. Application Performance Engineering
        1. The tools
          1. Advanced options in Visual Studio 2015
            1. Advanced options in the Diagnostic Tools menu
        2. Other tools
        3. The process of performance tuning
          1. Performance Counters
          2. Bottleneck detection
            1. Bottleneck detection in practice
        4. Using code to evaluate performance
        5. Optimizing web applications
          1. IIS optimization
          2. ASP.NET optimization
            1. General and configuration
            2. Caching
            3. Data access
            4. Load balancing
            5. Client side
      2. Summary
    21. 13. Advanced Topics
      1. The Windows messaging subsystem
        1. The MSG structure
      2. Sub-classing techniques
      3. Some useful tools
      4. Platform/Invoke: calling the OS from .NET
        1. The process of platform invocation
          1. Windows Management Instrumentation
            1. CIM searchable tables
      5. Parallel programming
        1. Difference between multithreading and parallel programming
      6. Parallel LINQ
        1. Dealing with other issues
        2. Canceling execution
      7. The Parallel class
        1. The Parallel.ForEach version
      8. Task Parallel
        1. Communication between threads
      9. .NET Core 1.0
        1. The list of supported environments
          1. Core FX
          2. Core CLR
          3. Core RT
          4. Core CLI
        2. Installation of .NET Core
        3. The CLI interface
      10. ASP.NET Core 1.0
        1. What's new
        2. A first approach
        3. Configuration and Startup settings
        4. Self-hosted applications
        5. ASP.NET Core 1.0 MVC
        6. Managing scripts
      11. NET Core 1.1
      12. Summary
    22. Index