Skip to main content
main-content

Über dieses Buch

This new edition of Pro C# 5.0 and the .NET 4.5 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.5 the most comprehensive release yet, including:

.NET APIs for Windows 8 style UI apps New asynchronous task-based model for async operations How HTML5 support is being wrapped into C# web applications New programming interfaces for HTTP applications, including improved IPv6 support Expanded WPF, WCF and WF libraries giving C# more power than ever before

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 (you'll find everything from generics to pLINQ covered here).

The mission of this text is to provide you with a rock-solid foundation in the C# programming language and the core aspects of the .NET platform (assemblies, remoting, Windows Forms, Web Forms, ADO.NET, XML web services, etc.). Once you digest the information presented in these 25 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.

Inhaltsverzeichnis

Frontmatter

Introducing C# and .NET Platform

Frontmatter

Chapter 1. The Philosophy of .NET

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, WF, 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

Chapter 2. Building C# Applications

As a C# programmer, you may choose among numerous tools to build .NET applications. The point of this chapter is to provide a tour of various .NET development options, including, of course, Visual Studio proper. The chapter opens, however, with an examination of working with the C# command-line compiler, csc.exe, and the simplest of all text editors, the Notepad application that ships with the Microsoft Windows OS, as well as the freely downloadable Notepad++.

Andrew Troelsen

Core C# Programming

Frontmatter

Chapter 3. Core C# Programming Constructs, Part I

This chapter begins your formal investigation of the C# programming language by presenting a number of bite-sized, stand-alone topics you must be comfortable with as you explore the .NET Framework. The first order of business is to understand how to build your program’s

application object

and to examine the composition of an executable program’s entry point: the Main() method. Next, you will investigate the fundamental C# data types (and their equivalent types in the System namespace) including an examination of the System.String and System.Text.StringBuilder classes.

Andrew Troelsen

Chapter 4. Core C# Programming Constructs, Part II

This chapter picks up where the Chapter 3 left off, and completes your investigation of the core aspects of the C# programming language. We begin by examining various details regarding the construction of C# methods, exploring the out, ref, and params keywords. Along the way, you will also examine the role of optional and named parameters.

Andrew Troelsen

Object-Oriented Programming with C#

Frontmatter

Chapter 5. Understanding Encapsulation

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 objectoriented 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 role of static members, object initialization syntax, read-only fields, constant data, and partial classes.

Andrew Troelsen

Chapter 6. Understanding Inheritance and Polymorphism

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 (constructors, fields, properties, methods, constants, and read-only fields). This chapter will focus on the remaining two pillars of OOP: inheritance and polymorphism.

Andrew Troelsen

Chapter 7. Understanding Structured Exception Handling

In this chapter, you will learn how to handle runtime anomalies in your C# code through the use of

structured exception handling

(often cryptically abbreviated as SEH). Not only will you examine the C# keywords that allow you to handle such matters (try, catch, throw, finally), 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 the exception-centric debugging tools of Visual Studio.

Andrew Troelsen

Chapter 8. Working with Interfaces

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, I’ll also discuss a number of related topics, such as obtaining interface references, explicit interface implementation, and the construction of interface hierarchies. We’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

Advanced C# Programming

Frontmatter

Chapter 9. Collections and Generics

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

Chapter 10. Delegates, Events, and Lambda Expressions

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

Chapter 11. Advanced C# Language Features

In this chapter, you’ll deepen your understanding of the C# programming language by examining a number of more advanced syntactic constructs. 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

Chapter 12. LINQ to Objects

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 very 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

Chapter 13. Understanding Object Lifetime

At this point in the text, 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 (a.k.a. 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

Programming with .NET Assemblies

Frontmatter

Chapter 14. Building and Configuring Class Libraries

During the first four sections of this text, you have created a number of “stand-alone” executable applications, in which all of the programming logic was packaged within a single executable file (*.exe). These executable assemblies were making use of 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

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

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

Chapter 16. Dynamic Types and the Dynamic Language Runtime

.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

Chapter 17. Processes, AppDomains, and Object Contexts

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

Chapter 18. Understanding CIL and the Role of Dynamic Assemblies

When you are building a full-scale .NET application, you will most certainly make use of C# (or a similar managed language such as Visual Basic), given their inherent productivity and ease of use. However, as you learned in the very 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

Introducing the .NET Base Class Libraries

Frontmatter

Chapter 19. Multithreaded, Parallel, and Async Programming

Nobody enjoys working with an application that is slow and sluggish during its execution. Moreover, nobody enjoys starting a task in an application (perhaps initiated by the clicking of 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 required authoring very complex C++ code that made use of 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

Chapter 20. File I/O and Object Serialization

When you create desktop applications, the ability to save information between user sessions is imperative. 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

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

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, then move on to the topic of ADO.NET data providers. The .NET platform supports numerous data providers, each of which is optimized to communicate with a specific database management system (e.g., Microsoft SQL Server, Oracle, and MySQL).

Andrew Troelsen

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

The previous chapter gave you a chance to examine the

connected layer

of ADO.NET, which allows 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

Chapter 23. ADO.NET Part III: The Entity Framework

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 brand-new component of the ADO.NET API called the

Entity Framework

(or simply,

EF

) in .NET 3.5 Service Pack 1.

Andrew Troelsen

Chapter 24. Introducing LINQ to XML

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, Silverlight, and Windows Workflow Foundation all make use of an XML-based grammar (XAML) to represent desktop UIs, browser-based UIs, and workflows, respectively. 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

Chapter 25. Introducing Windows Communication Foundation

.NET 3.0 introduced an API designed specifically for the process of building distributed systems: Windows Communication Foundation (WCF). Unlike other 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

Chapter 26. Introducing Windows Workflow Foundation

The .NET platform supports a programming model termed

Windows Workflow Foundation

(WF). This API allows you to model, configure, monitor, and execute the

workflows

(which are used to model a business process) used internally by a given .NET program. Workflows are modeled (by default) using a declarative XML-based grammar named XAML where data used by the workflow is treated as a first class citizen.

Andrew Troelsen

Windows Presentation Foundation

Frontmatter

Chapter 27. Introducing Windows Presentation Foundation and XAML

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

Chapter 28. Programming with WPF Controls

Chapter 27 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 27 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

Chapter 29. WPF Graphics Rendering Services

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

Chapter 30. WPF Resources, Animations, and Styles

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

Chapter 31. Dependency Properties, Routed Events, and Templates

This chapter concludes your investigation of the WPF programming model by introducing you to the process of building customized control templates. While it is true that the content model and style mechanism do allow you to add unique bits to a standard WPF control, the process of building custom templates allows you to fully define how a control should render its output, respond to state transitions, and integrate into the WPF APIs.

Andrew Troelsen

ASP.NET Web Forms

Frontmatter

Chapter 32. Introducing ASP.NET Web Forms

Until now, all of the examples in this book have focused on console-based and desktop graphical user interfaces created using the WPF API. The remainder of the text will explore how the .NET platform facilitates the construction of browser-based presentation layers using a technology named ASP.NET. 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 ASP.NET Development Web Server.

Andrew Troelsen

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

The previous chapter concentrated on the general composition of an ASP.NET web 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 ASP.NET web control, you will come to understand how to make use of several UI elements, including the validation controls and various data-binding techniques.

Andrew Troelsen

Chapter 34. ASP.NET State Management Techniques

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

Backmatter

Weitere Informationen

Premium Partner

    Bildnachweise