Skip to main content
main-content
Top

About this book

This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including:

A Refined ADO.NET Entity Framework Programming ModelNumerous IDE and MVVM Enhancements for WPF Desktop DevelopmentNumerous updates to the ASP.NET Web APIs

This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies).

The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms.

What You Will Learn:

Be the first to understand the .NET 4.6 platform and C# 6.

Discover the ins and outs of the leading .NET technology.

Learn from an award-winning author who has been teaching the .NET world since version 1.0.

Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime.

Table of Contents

Introducing C# and the .NET Platform

Frontmatter

Chapter 1. The Philosophy of .NET

Abstract
Microsoft’s .NET platform (and the related C# programming language) were formally introduced circa 2002 and have quickly become a mainstay of modern-day software development. As mentioned in the book’s introductory section, the goal of this text is twofold. The first order of business is to provide you with deep and detailed examination of the syntax and semantics of C#. The second (equally important) order of business is to illustrate the use of numerous .NET APIs, including database access with ADO.NET and the Entity Framework (EF), the LINQ technology set, WPF, WCF and web site development using ASP.NET. As they say, the journey of a thousand miles begins with a single step; and with this I welcome you to Chapter 1.
Andrew Troelsen, Philip Japikse

Chapter 2. Building C# Applications

Abstract
As a C# programmer, you can choose from among numerous tools to build .NET applications. The tool (or tools) you select will be based primarily on three factors: any associated costs, the OS you are using to develop the software, and the computing platforms you are targeting. The point of this chapter is to provide a survey of the most common integrated development environments (IDEs) that support the C# language. Do understand that this chapter will not go over every single detail of each IDE; it will give you enough information to select your programming environment as you work through this text and give you a foundation to build on.
Andrew Troelsen, Philip Japikse

Object-Oriented Programming with C#

Frontmatter

Chapter 5. Understanding Encapsulation

Abstract
In the Chapters 3 and 4, you investigated a number of core syntactical constructs that are commonplace to any .NET application you might be developing. Here, you will begin your examination of the object-oriented capabilities of C#. The first order of business is to examine the process of building well-defined class types that support any number of constructors. After you understand the basics of defining classes and allocating objects, the remainder of this chapter will examine the role of encapsulation. Along the way, you will learn how to define class properties and come to understand the details of the static keyword, object initialization syntax, read-only fields, constant data, and partial classes.
Andrew Troelsen, Philip Japikse

Chapter 6. Understanding Inheritance and Polymorphism

Abstract
Chapter 5 examined the first pillar of OOP: encapsulation. At that time, you learned how to build a single well-defined class type with constructors and various members (fields, properties, methods, constants, and read-only fields). This chapter will focus on the remaining two pillars of OOP: inheritance and polymorphism.
Andrew Troelsen, Philip Japikse

Chapter 7. Understanding Structured Exception Handling

Abstract
In this chapter, you will learn how to handle runtime anomalies in your C# code through the use of structured exception handling. Not only will you examine the C# keywords that allow you to handle such matters (try, catch, throw, finally, when), but you will also come to understand the distinction between application-level and system-level exceptions, as well as the role of the System.Exception base class. This discussion will lead into the topic of building custom exceptions and, finally, to a quick look at some exception-centric debugging tools of Visual Studio.
Andrew Troelsen, Philip Japikse

Chapter 8. Working with Interfaces

Abstract
This chapter builds upon your current understanding of object-oriented development by examining the topic of interface-based programming. Here you’ll learn how to define and implement interfaces and come to understand the benefits of building types that support multiple behaviors. Along the way, you will also examine a number of related topics, such as obtaining interface references, explicit interface implementation, and the construction of interface hierarchies. You’ll also examine a number of standard interfaces defined within the .NET base class libraries. As you will see, your custom classes and structures are free to implement these predefined interfaces to support a number of useful behaviors, such as object cloning, object enumeration, and object sorting.
Andrew Troelsen, Philip Japikse

Advanced C# Programming

Frontmatter

Chapter 9. Collections and Generics

Abstract
Any application you create with the .NET platform will need to contend with the issue of maintaining and manipulating a set of data points in memory. These data points can come from any variety of locations including a relational database, a local text file, an XML document, a web service call, or perhaps via user-provided input.
Andrew Troelsen, Philip Japikse

Chapter 10. Delegates, Events, and Lambda Expressions

Abstract
Up to this point in the text, most of the applications you developed added various bits of code to Main(), which, in some way or another, sent requests to a given object. However, many applications require that an object be able to communicate back to the entity that created it using a callback mechanism. While callback mechanisms can be used in any application, they are especially critical for graphical user interfaces in that controls (such as a button) need to invoke external methods under the correct circumstances (when the button is clicked, when the mouse enters the button surface, and so forth).
Andrew Troelsen, Philip Japikse

Chapter 11. Advanced C# Language Features

Abstract
In this chapter, you’ll deepen your understanding of the C# programming language by examining a number of more advanced topics. To begin, you’ll learn how to implement and use an indexer method. This C# mechanism enables you to build custom types that provide access to internal subitems using an array-like syntax. After you learn how to build an indexer method, you’ll see how to overload various operators (+, -, <, >, and so forth) and how to create custom explicit and implicit conversion routines for your types (and you’ll learn why you might want to do this).
Andrew Troelsen, Philip Japikse

Chapter 12. LINQ to Objects

Abstract
Regardless of the type of application you are creating using the .NET platform, your program will certainly need to access some form of data as it executes. To be sure, data can be found in numerous locations, including XML files, relational databases, in-memory collections, and primitive arrays. Historically speaking, based on the location of said data, programmers needed to make use of different and unrelated APIs. The Language Integrated Query (LINQ) technology set, introduced initially in .NET 3.5, provides a concise, symmetrical, and strongly typed manner to access a wide variety of data stores. In this chapter, you will begin your investigation of LINQ by focusing on LINQ to Objects.
Andrew Troelsen, Philip Japikse

Chapter 13. Understanding Object Lifetime

Abstract
At this point in the book, you have learned a great deal about how to build custom class types using C#. Now you will see how the CLR manages allocated class instances (aka objects) via garbage collection. C# programmers never directly deallocate a managed object from memory (recall there is no delete keyword in the C# language). Rather, .NET objects are allocated to a region of memory termed the managed heap, where they will be automatically destroyed by the garbage collector “sometime in the future.”
Andrew Troelsen, Philip Japikse

Programming with .NET Assemblies

Frontmatter

Chapter 14. Building and Configuring Class Libraries

Abstract
During the first four parts of this book, you have created a number of “stand-alone” executable applications, in which all the programming logic was packaged within a single executable file (*.exe). These executable assemblies were using little more than the primary .NET class library, mscorlib.dll. While some simple .NET programs may be constructed using nothing more than the .NET base class libraries, chances are it will be commonplace for you (or your teammates) to isolate reusable programming logic into custom class libraries (*.dll files) that can be shared among applications.
Andrew Troelsen, Philip Japikse

Chapter 15. Type Reflection, Late Binding, and Attribute-Based Programming

Abstract
As shown in Chapter 14, assemblies are the basic unit of deployment in the .NET universe. Using the integrated object browsers of Visual Studio (and numerous other IDEs), you are able to examine the types within a project’s referenced set of assemblies. Furthermore, external tools such as ildasm.exe allow you to peek into the underlying CIL code, type metadata, and assembly manifest for a given .NET binary. In addition to this design-time investigation of .NET assemblies, you are also able to programmatically obtain this same information using the System.Reflection namespace. To this end, the first task of this chapter is to define the role of reflection and the necessity of .NET metadata.
Andrew Troelsen, Philip Japikse

Chapter 16. Dynamic Types and the Dynamic Language Runtime

Abstract
NET 4.0 introduced a new keyword to the C# language, specifically, dynamic. This keyword allows you to incorporate scripting-like behaviors into the strongly typed world of type safety, semicolons, and curly brackets. Using this loose typing, you can greatly simplify some complex coding tasks and also gain the ability to interoperate with a number of dynamic languages (such as IronRuby or IronPython), which are .NET savvy.
Andrew Troelsen, Philip Japikse

Chapter 17. Processes, AppDomains, and Object Contexts

Abstract
In Chapters 14 and 15, you examined the steps taken by the CLR to resolve the location of a referenced external assembly, as well as the role of .NET metadata. In this chapter, you’ll drill deeper into the details of how an assembly is hosted by the CLR and come to understand the relationship between processes, application domains, and object contexts.
Andrew Troelsen, Philip Japikse

Chapter 18. Understanding CIL and the Role of Dynamic Assemblies

Abstract
When you are building a full-scale .NET application, you will most certainly use C# (or a similar managed language such as Visual Basic), given its inherent productivity and ease of use. However, as you learned in the first chapter, the role of a managed compiler is to translate *.cs code files into terms of CIL code, type metadata, and an assembly manifest. As it turns out, CIL is a full-fledged .NET programming language, with its own syntax, semantics, and compiler (ilasm.exe).
Andrew Troelsen, Philip Japikse

Introducing the .NET Base Class Libraries

Frontmatter

Chapter 19. Multithreaded, Parallel, and Async Programming

Abstract
Nobody enjoys working with an application that is sluggish during its execution. Moreover, nobody enjoys starting a task in an application (perhaps initiated by clicking a toolbar item) that prevents other parts of the program from being as responsive as possible. Before the release of .NET, building applications that had the ability to perform multiple tasks typically required authoring complex C++ code that used the Windows threading APIs. Thankfully, the .NET platform provides a number of ways for you to build software that can perform complex operations on unique paths of execution, with far fewer pain points.
Andrew Troelsen, Philip Japikse

Chapter 20. File I/O and Object Serialization

Abstract
When you create desktop applications, the ability to save information between user sessions is commonplace. This chapter examines a number of I/O-related topics as seen through the eyes of the .NET Framework. The first order of business is to explore the core types defined in the System.IO namespace and learn how to modify a machine’s directory and file structure programmatically. The next task is to explore various ways to read from and write to character-based, binary-based, string-based, and memory-based data stores.
Andrew Troelsen, Philip Japikse

Chapter 21. ADO.NET Part I: The Connected Layer

Abstract
The .NET platform defines a number of namespaces that allow you to interact with relational database systems. Collectively speaking, these namespaces are known as ADO.NET. In this chapter, you’ll learn about the overall role of ADO.NET and the core types and namespaces, and then you’ll move on to the topic of ADO.NET data providers. The .NET platform supports numerous data providers (both provided as part of the .NET Framework and available from third-party sources), each of which is optimized to communicate with a specific database management system (e.g., Microsoft SQL Server, Oracle, and MySQL).
Andrew Troelsen, Philip Japikse

Chapter 22. ADO.NET Part II: The Disconnected Layer

Abstract
The previous chapter gave you a chance to examine the connected layer and the foundational components of ADO.NET, which allow you to submit SQL statements to a database using the connection, command, and data reader objects of your data provider. In this chapter, you will learn about the disconnected layer of ADO.NET. Using this facet of ADO.NET lets you model database data in memory, within the calling tier, by leveraging numerous members of the System.Data namespace (most notably, DataSet, DataTable, DataRow, DataColumn, DataView, and DataRelation). By doing so, you can provide the illusion that the calling tier is continuously connected to an external data source; the reality is that the caller is operating on a local copy of relational data.
Andrew Troelsen, Philip Japikse

Chapter 23. ADO.NET Part III: Entity Framework

Abstract
The previous two chapters examined the fundamental ADO.NET programming models—the connected and disconnected layers, specifically. These approaches have enabled .NET programmers to work with relational data (in a relatively straightforward manner) since the initial release of the platform. However, Microsoft introduced a new component of the ADO.NET API called the Entity Framework (or simply, EF) in .NET 3.5 Service Pack 1.
Andrew Troelsen, Philip Japikse

Chapter 24. Introducing LINQ to XML

Abstract
As a .NET developer, you are bound to encounter XML-based data in numerous places. Application and web-based configuration files store information as XML. Windows Presentation Foundation uses an XML-based grammar (XAML) to represent desktop GUIs. ADO.NET DataSets can easily save out (or load in) data as XML. Even Windows Communication Foundation stores numerous settings as the well-formatted string called XML.
Andrew Troelsen, Philip Japikse

Chapter 25. Introducing Windows Communication Foundation

Abstract
Windows Communication Foundation (WCF) is the name of the API designed specifically for the process of building distributed systems. Unlike other specific distributed APIs you might have used in the past (e.g., DCOM, .NET remoting, XML web services, message queuing), WCF provides a single, unified, and extendable programming object model that you can use to interact with a number of previously diverse distributed technologies.
Andrew Troelsen, Philip Japikse

Windows Presentation Foundation

Frontmatter

Chapter 26. Introducing Windows Presentation Foundation and XAML

Abstract
When version 1.0 of the .NET platform was released, programmers who needed to build graphical desktop applications made use of two APIs named Windows Forms and GDI+, packaged up primarily in the System.Windows.Forms.dll and System.Drawing.dll assemblies. While Windows Forms/GDI+ are excellent APIs for building traditional desktop GUIs, Microsoft shipped an alternative GUI desktop API named Windows Presentation Foundation (WPF) beginning with the release of .NET 3.0.
Andrew Troelsen, Philip Japikse

Chapter 27. Programming with WPF Controls

Abstract
Chapter 26 provided a foundation for the WPF programming model, including an examination of the Window and Application classes, the grammar of XAML, and the use of code files. Chapter 26 also introduced you to the process of building WPF applications using the designers of Visual Studio. In this chapter, you will dig into the construction of more sophisticated graphical user interfaces using several new controls and layout managers, learning about additional features of the WPF designers of Visual Studio along the way.
Andrew Troelsen, Philip Japikse

Chapter 28. WPF Graphics Rendering Services

Abstract
In this chapter, we’ll examine the graphical rendering capabilities of WPF. As you’ll see, WPF provides three separate ways to render graphical data: shapes, drawings, and visuals. After you understand the pros and cons of each approach, you will start learning about the world of interactive 2D graphics using the classes within System.Windows.Shapes. After this, you’ll see how drawings and geometries allow you to render 2D data in a more lightweight manner. And last but not least, you’ll learn how the visual layer gives you the greatest level of power and performance.
Andrew Troelsen, Philip Japikse

Chapter 29. WPF Resources, Animations, Styles, and Templates

Abstract
This chapter introduces you to three important (and interrelated) topics that will deepen your understanding of the Windows Presentation Foundation (WPF) API. The first order of business is to learn the role of logical resources. As you will see, the logical resource (also known as an object resource) system is a way to name and refer to commonly used objects within a WPF application. While logical resources are often authored in XAML, they can also be defined in procedural code.
Andrew Troelsen, Philip Japikse

Chapter 30. Notifications, Commands, Validation, and MVVM

Abstract
This chapter will conclude your investigation of the WPF programming model by introducing you to the Observable pattern and the notification system built into WPF. You’ll also expand your knowledge of commands and validation. These three items are the foundation for implementing the Model-View-ViewModel (MVVM) pattern in WPF.
Andrew Troelsen, Philip Japikse

ASP.NET

Frontmatter

Chapter 31. Introducing ASP.NET Web Forms

Abstract
Until now, all of the examples in this book have focused on console-based and desktop graphical user interfaces created using the WPF platform. The remainder of the text will explore how the .NET platform facilitates the construction of Internet-based applications using a technology named ASP.NET. This and the next two chapters cover ASP.NET Web Forms, and Chapter 34 covers ASP.NET MVC and ASP.NET Web API. To begin, you’ll quickly overview a number of key web development concepts (HTTP, HTML, client-side scripting, postbacks) and examine the role of Microsoft’s commercial web server (IIS) as well as that of the IIS Express.
Andrew Troelsen, Philip Japikse

Chapter 32. ASP.NET Web Controls, Master Pages, and Themes

Abstract
The previous chapter concentrated on the general composition of an Web Forms page and the role of the Page class. This chapter will dive into the details of the web controls that make up a page’s user interface. After examining the overall nature of an Web Forms control, you will learn how to use several UI elements, including validation controls and various data-binding techniques.
Andrew Troelsen, Philip Japikse

Chapter 33. ASP.NET State Management Techniques

Abstract
The previous two chapters concentrated on the composition and behavior of ASP.NET pages and the web controls they contain. This chapter builds on that information by examining the role of the Global.asax file and the underlying HttpApplication type. As you will see, the functionality of HttpApplication allows you to intercept numerous events that enable you to treat your web application as a cohesive unit, rather than a set of stand-alone *.aspx files driven by a master page.
Andrew Troelsen, Philip Japikse

Chapter 34. ASP.NET MVC and Web API

Abstract
The last three chapters covered ASP.NET Web Forms as well as web-related concepts such as HTTP and HTML. This chapter introduces two newcomers to the ASP.NET ecosystem: ASP.NET MVC and Web API. MVC grew out of the user community (specifically the ALT.NET movement) asking for a framework that more closely adhered to the tenants of HTTP, was more testable, and adhered to separation of concerns. While Web Forms still controls the market share of the .NET web development ecosystem, MVC is rapidly growing its adoption rate.
Andrew Troelsen, Philip Japikse
Additional information