C# Advanced Programming - (3rd Edition)

Author: Robinson
Publisher:
Publish Date: 2005-06-01
Features: C# and .NET provide a brand-new development environment for programming on the Windows platform, where almost all Windows and Web-based applications can be developed. This book comprehensively introduces the programming knowledge of C#, while providing the necessary background knowledge to understand the working principles of the .NET architecture. Compared to the previous two versions, it has added content on application deployment and localization. This book is a C# reference for developers and a valuable guide for users of C# and .NET. Through learning this book, you will gain an in-depth understanding of various aspects of the C# language and be able to apply C# in the .NET environment, build Windows forms, access databases with ADO.NET, write components with ASP.NET, and leverage .NET to provide support for COM and COM+.
Main Content:
· Object-oriented programming with C#
· Handling XML with C#
· Integrating COM, COM+, and Active Directory
· Writing Windows applications and Windows services
· Developing distributed applications with .NET Remoting
· Understanding .NET assemblies
· Creating graphics with C#
· Controlling .NET security
Target Audience: This book is suitable for developers who have experience in C++, Visual Basic, or J++, as well as beginner programmers who wish to delve deeper into C#. For developers, it is not an exaggeration to describe C# and its related environment, the .NET Framework, as one of the most important new technologies in years. The .NET Framework provides a new environment where almost all applications that run on Windows can be developed, and C# is a new programming language specifically designed for .NET. For example, with C#, you can write dynamic web pages, XML Web services, components for distributed applications, database access components, or traditional Windows desktop applications. This book introduces the .NET Framework 1.1, the second version of the .NET Framework, but most of the content is also applicable to the .NET Framework 1.0. If you are coding with version 1.0, some modifications will be required, and this book will point out the necessary changes. Do not be misled by the name .NET; it only emphasizes Microsoft's belief that distributed applications are the future trend, where processes are distributed between clients and servers. However, C# is not just a language for writing Internet or network-related applications; it also provides a way to write almost any type of software or component for the Windows platform. Additionally, both C# and .NET have revolutionized the way programs are written, making it easier to program on Windows. This is a rather important statement. After all, as we all know, the development of computer technology is very fast, and Microsoft releases new software, programming tools, or new versions of Windows every year, claiming they are very useful for developers, and .NET and C# are no exception.
The Importance of .NET and C#:
To understand the importance of .NET, it may help to consider the nature of many Windows technologies that have emerged in the past decade. Although all Windows operating systems appear completely different on the surface, from Windows 3.1 (1992) to Windows Server 2003, they all share the same Windows API at their core. When we switch to a new version of Windows, the API is enriched with many new features, but this is a process of evolution and expansion, not replacement. Many of the technologies and architectures used to develop Windows software are similar. For example, COM (Component Object Model) was developed as part of OLE (Object Linking and Embedding), and at that time, it was largely used to link different types of office documents together, so it could place a small Excel spreadsheet in a Word document. Over time, it evolved into COM, DCOM (Distributed Component Object Model), and finally COM+. COM+ is a complex technology that is the foundation for almost all component communication methods, implementing transaction processing, message queuing services, and object pooling. Microsoft's choice of this path is very clear: it prioritizes backward compatibility. Over the years, third-party vendors have written a significant amount of Windows software. If Microsoft introduced a new technology that did not follow the existing codebase every time, Windows would not have achieved the success it has today. Backward compatibility is an extremely important feature of Windows technology and a long-standing strength of the Windows platform, but it has a major drawback. Every time a technology evolves and adds new features, it becomes more complex. Clearly, improvements are necessary. Microsoft cannot keep extending these development tools and languages to make them increasingly complex while keeping up with the latest hardware developments and maintaining backward compatibility with Windows products that became popular in the early 1990s. To get a simpler and more specialized language, environment, and development tools that allow developers to write excellent software more easily, a new starting point is needed. This is where C# and .NET come in. Roughly speaking, .NET is a new programming architecture for Windows—a new API. C# is a new language that can leverage all the new features of the .NET Framework and its development environment, as well as the object-oriented programming methods that have emerged in the past 20 years. Before proceeding, it must be clarified that backward compatibility has not been lost in this evolutionary process. Existing programs are still usable, and .NET is also compatible with existing software. Communication between software components on Windows is now almost always done using COM. Therefore, .NET can provide wrappers for existing COM components to allow .NET components to communicate with them. Microsoft has extended C++ to provide a new language, J#, and has made many improvements to VB, transforming it into a more powerful VB.NET, and allowing code written in these languages to be used in the .NET environment. However, these languages cannot fully leverage the latest technology due to their evolutionary history. This book will introduce C# programming techniques while providing the necessary background knowledge on how the .NET architecture works. We will not only cover the basics of the C# language but also provide application examples using various related technologies, including database access, dynamic web pages, advanced graphics techniques, and directory access. The only requirement is that the user is familiar with at least one high-level language for Windows, such as C++, VB, or J++.
Advantages of .NET:
While the advantages of .NET have been discussed, it has not been mentioned how it makes the work of developers easier. In this section, we will briefly discuss the improved features of .NET.
● Object-oriented programming: The .NET Framework and C# are completely object-oriented from the beginning.
● Excellent design: A base class library designed in a very intuitive way.
● Language independence: In .NET, languages such as VB.NET, C#, J#, and Managed C++ can all be compiled into a common Intermediate Language (IL). This means that languages can interact in ways that were not previously possible.
● Support for dynamic web pages: ASP is highly flexible but not very efficient, as it uses an interpreted scripting language and lacks object-oriented design, resulting in relatively messy ASP code. .NET uses a new technology, ASP.NET, which provides integrated support for web pages. With ASP.NET, the code in pages can be compiled, and this code can be written in .NET high-level languages such as C#, J#, or VB.NET.
● Efficient data access: A set of .NET components collectively known as ADO.NET provides efficient access to relational databases and various data sources. These components can also access file systems and directories. .NET has built-in XML support and can handle data imported or exported from non-Windows platforms.
● Code sharing: .NET introduces the concept of assemblies to replace traditional DLLs, providing a flawless way to share code between applications. Assemblies have a formal system to resolve version conflicts, and different versions of assemblies can coexist.
● Enhanced security: Each assembly can also contain built-in security information, which can accurately specify who or what type of user or process can call which methods of which classes. This allows for very precise control over how assemblies are used.
● No impact on installation: There are two types of assemblies: shared assemblies and private assemblies. Shared assemblies are public libraries that can be used by all software, while private assemblies are used only by a specific application. Private assemblies are fully functional, so the installation process is very simple, with no registry entries required—just place the corresponding files in the appropriate folders of the file system.
● Web service support: .NET provides complete support for developing web services, allowing users to develop any type of application.
● Visual Studio .NET 2003: .NET comes with a development environment, Visual Studio .NET, which can be used to write code in C++, C#, J#, VB.NET, and ASP.NET. Visual Studio .NET integrates all the best features of the language-specific environments in Visual Studio 6.
● C#: A new object-oriented language for using .NET. Chapter 1 will discuss the advantages of the .NET architecture in detail.
New Features in .NET Framework 1.1:
The first version of the .NET Framework (1.0) was released in 2002 and was widely praised. The new version of the .NET Framework, 1.1, was released in 2003 and is considered a minor improvement to the architecture. Even with smaller improvements, the new version still has some significant changes and new features worth exploring. In all the improvements made to the .NET Framework 1.1 version, Microsoft has tried to ensure that the changes required for code written in version 1.0 are as minimal as possible. Despite this effort, there are still some notable changes in the new version. Many code improvements were made to enhance security. Readers can find the complete list of improvements on Microsoft's GotDotNet web site (http://www.gotdotnet.com). Below, we will discuss some of the improvements in the 1.1 version of the .NET Framework and the new features in Visual Studio .NET 2003 (.NET Framework 1.1 development environment).
Mobility:
When using .NET Framework 1.0 and Visual Studio .NET 2002, to create mobile applications, you had to download the Microsoft Mobile Internet Toolkit (MMIT). However, with .NET Framework 1.1 and Visual Studio .NET 2003, you can directly create mobile applications without downloading any additional toolkits. This is evident when creating new projects in Visual Studio .NET 2003. For example, when viewing the list of C# project types that can be created, you will see ASP.NET Mobile Web Application and Smart Device Application. The ASP.NET Mobile Web Application project type can be used to create web-based mobile applications. The Smart Device Application project type can create applications for Pocket PC or other Windows CE devices. Third-party client applications for Windows CE devices use the Compact Framework, which is a trimmed-down version of the .NET Framework. When you open any type of mobile project, a set of mobile server controls will be listed in the Visual Studio .NET toolbox, and you can use these controls to create your application.
New Data Providers:
Another major change in the new architecture is ADO.NET. ADO.NET is the .NET way of accessing and processing data, and now it has two new data providers: one for ODBC and another for Oracle. When using .NET Framework 1.0, you could use the ODBC data provider, but it had to be downloaded separately. Additionally, once downloaded, the namespace for this data provider was Microsoft.Data.Odbc. In .NET Framework 1.1, the ODBC data provider is built-in and does not require separate download. Moreover, you can use ODBC data sources, access ODBC data connections, data adapters, and data reader objects through the System.Data.Odbc namespace. The other new data provider is for handling Oracle databases, which are widely used in enterprise environments, and the lack of an Oracle data provider is often a barrier for .NET to enter the enterprise. To use this new data provider, you need to reference the System.Data.OracleClient namespace in your project.
New Language: Visual J#
When installing Visual Studio .NET 2003, note that this version provides a new language, Visual J#, for building .NET applications. Before this version, Visual Studio .NET 2002 required the separate installation of this language. Visual J# isJ# (read as "J-Sharp"), the new version of Visual J++ and is very similar to the Java language, making it easy for Java developers to migrate to .NET. J# developers will use the .NET class library instead of the Java runtime library. On the .NET platform, J# developers will have the same capabilities as C# developers. With J#, you can also create .NET classes, Windows Forms applications, ASP.NET web applications, and XML web services. Additionally, you can use J# in a cross-language manner, just like other .NET-compatible languages. For example, you can create a J# class and use it in a C# application, or you can create a C# class and use it in a J# application. Like other languages, there is a built-in compiler for J# in the .NET Framework. All compilers are located in the directory C:\Windows\Microsoft.NET\Framework\v1.1.xxxx. The C# compiler is csc.exe, the VB.NET compiler is vbc.exe, and the J# compiler is vjc.exe.
Side-by-Side Execution
Side-by-side execution refers to running multiple versions of applications on the same server, where different application versions use different runtime library versions. Microsoft has always promised this feature to developers, but it has always been difficult to visualize because you can only use one version of the Framework. After the release of the second version of the Framework, .NET Framework 1.1, you can now see this feature provided by Microsoft. Now, you can create .NET applications for the new version of .NET Framework 1.1 while still allowing old applications targeting .NET Framework 1.0 to continue running as before.
Support for Internet Protocol 6 (IPv6)
Recently, much of the Internet runs on IPv4, also known as IPv4. It provides IP addresses, such as 255.255.255.255. The .NET Framework 1.1 now supports IPv6, which was created in 1995 and addresses many of the problems faced by IPv4. If people had continued to use IPv4, the available IP addresses would have been exhausted very quickly. The .NET Framework 1.1 supports IPv6 through the System.Net namespace, and ASP.NET and XML web services also support IPv6.
Improvements in Visual Studio .NET 2003
When upgrading the .NET Framework, Visual Studio .NET itself was also upgraded. Note that there are new graphics on the start page, and the organization of objects on the page has also changed. Additionally, a major change in the new IDE is that once installed, it is not just an upgrade from Visual Studio .NET 2002 to Visual Studio .NET 2003, but rather a completely new version of the IDE. If Visual Studio .NET 2002 is already installed on your machine, you will get two completely independent VS.NET IDEs. So, if you want to create and use applications targeting .NET Framework 1.0, use VS.NET 2002; if you want to create and use applications targeting .NET Framework 1.1, use VS.NET 2003. Also, note that when you open a project created with VS.NET 2002, the system will ask if you want to upgrade the project to a VS.NET 2003 project. If you answer "yes," the project will be upgraded to an application targeting .NET Framework 1.1. Note that this is an irreversible process. In addition to these major changes, in the IDE of VS.NET 2003, Intellisense is more intelligent, and the code auto-completion feature is stronger. This book will use this version of the IDE.
Advantages of C#
C# can be considered a .NET-oriented programming language for the Windows environment. Over the past decade, Microsoft has added many features to Windows and the Windows API, and both VB and C++ have undergone many changes. Although VB and C++ have ultimately become very powerful languages, they also have problems because they retain some of their original elements. For Visual Basic, its main advantage is that it is very easy to understand, and many programming tasks are very easy to accomplish, basically hiding the of the Windows API and COM component structure. Its disadvantage is that Visual Basic never truly implemented object-oriented programming, so it is difficult to decompose and maintain large applications. Additionally, because the syntax of VB inherits from early versions of BASIC (BASIC was mainly designed for beginners rather than for writing large commercial applications), it cannot truly be a structured or object-oriented programming language. On the other hand, C++ has its own roots in the ANSI C++ language definition. It is not fully compatible with ANSI because Microsoft wrote the C++ compiler before the ANSI definition was standardized, but it is very close. Unfortunately, this has led to two problems. First, ANSI C++ was developed under the technological conditions of the past decade, so it does not support modern concepts (such as Unicode strings and generating XML documents), and some outdated syntax structures were designed for older compilers (such as separating the declaration and definition of member functions). Second, Microsoft also tried to evolve C++ into a language for performing high-performance tasks on Windows, avoiding the addition of a large number of Microsoft-specific keywords and various libraries in the language. The result is that in Windows, the language has become very cluttered. Let a C++ developer say how many ways there are to define a string, and this can illustrate the point: char\, LPTSTR, string, CString (MFC version), CString (WTL version), wchar\_t\, and OLECHAR\ are some of the ways to define a string. Now, entering the .NET era—a completely new environment that has extended both of these languages. Microsoft has added many Microsoft-specific keywords to C++, and has evolved VB into VB.NET, retaining some basic VB syntax but with a completely different design, making VB.NET a new language in practical terms. Here, Microsoft has decided to give developers another choice—a new language specifically for .NET with a new starting point, namely Visual C#.NET. Microsoft describes C# as a simple, modern, object-oriented, type-safe programming language derived from C and C++. Most independent commentators describe it as "derived from C, C++, and Java." This description is technically accurate but does not involve the real advantages of the language. From a syntax perspective, C# is very similar to C++ and Java, with many keywords being the same, and C# also uses a block structure similar to C++ and Java, marking code blocks with parentheses ({}) and separating statements with semicolons. The impression one gets of C# code is that it is very similar to C++ or Java code. But behind these superficial similarities, C# is much easier to learn than C++ but slightly more difficult than Java. Its design is more adaptable to modern development tools than other languages, and it combines the ease of use of Visual Basic, high performance, and the low-level memory access of C++. C# includes the following features:
● Fully supports classes and object-oriented programming, including interfaces and inheritance, virtual functions, and operator overloading.
● Defines a complete and consistent set of basic types.
● Built-in support for automatically generating XML documentation.
● Automatically cleans up dynamically allocated memory.
● Can mark classes or methods with user-defined attributes. This can be used for documentation purposes and has an impact on compilation (for example, marking a method as compiled only in debug mode).
● Full access to the .NET base class library and easy access to the Windows API.
● Can use pointers and direct memory access, but C# can access memory without them.
● Supports properties and events in the style of VB.
● Can change compiler options to compile the program into an executable file or a .NET component library, which can be called by other code in the same way as ActiveX controls (COM components).
● C# can be used to write ASP.NET dynamic web pages and XML web services.
It should be noted that for most of the above features, VB.NET and Managed C++ also have them. But C# has been using .NET from the beginning, with complete support for .NET features and providing a more suitable syntax than other languages. C# itself is very similar to Java, but there are some improvements because Java was not designed for use in the .NET environment.
Before concluding this topic, it is also worth noting two limitations of C#. One is that the language is not suitable for writing time-critical or very high-performance code, such as a loop that runs 1000 or 1050 times and immediately cleans up the resources it occupies when it is not needed. In this regard, C++ may still be the best among all low-level languages. The other is that C# lacks the key features needed for very high-performance applications, including inline functions and destructors that guarantee execution at specific points in the code. But such applications are very rare.
The Environment Needed to Write and Run C# Code
.NET runs on Windows 98, 2000, XP, and 2003. To write code with .NET, you need to install the .NET SDK unless you are using Windows Server 2003, which has the .NET Framework 1.0 and 1.1 built-in. Unless you are writing C# code with a text editor or other third-party development environment, you generally use Visual Studio .NET 2003. Running managed code does not require installing the full SDK, but you do need the .NET runtime. You need to distribute the .NET runtime to client machines that do not have it installed.
Content of This Book
In this book, we first introduce the overall structure of .NET in Chapter 1, providing the background knowledge needed to write managed code. After that, the book is divided into several parts to introduce the C# language and its applications in various fields.
Part (Chapters 1~11)—C# Language
This part provides background knowledge of the C# language. This part does not specify any language but assumes the reader is an experienced programmer. First, it introduces the basic syntax and data types of C#, then the object-oriented features of C#, and after that, some advanced topics in C#.
Part (Chapters 12~18)—.NET Environment
This part introduces the rules for programming in the .NET environment. Specifically, it covers Visual Studio .NET, security, thread deployment for .NET applications, and how to create libraries as assemblies.
Part (Chapters 19~20)—Windows Forms
This part discusses the creation of traditional Windows applications, which are called Windows Forms in .NET. Windows Forms are the client versions of applications, and using .NET to create these types of applications is a quick and simple way to achieve this task. In addition to introducing Windows Forms, we will also discuss GDI+, a technology that can be used to create applications containing advanced graphics.
Part (Chapters 21~24)—Data
This part introduces how to use ADO.NET to access databases and interact with directories and Active Directory. We also provide a detailed explanation of .NET's support for XML and its support for the Windows operating system.
Part (Chapters 25~27)—Web Programming
This part introduces how to write components that run on websites and how to write web pages. This includes the use of ASP.NET and the writing of web service programs.
Part (Chapters 28~29)—Interoperability
COM's backward compatibility is an important part of .NET, and COM+ is responsible for transaction processing, object pooling, and message queuing. This part introduces .NET's support for handling COM and COM+ and discusses how to write C# code that interacts with these technologies.
Part (Chapters 30~32)—Windows Basic Services
This part is a summary of the main content of the book, introducing how to access files and the registry, how to access the Internet through applications, and how to use Windows services.
Part—Appendices (This book only provides content download addresses)
This part contains several appendices that provide detailed information on object-oriented programming rules and information specific to the C# programming language. These appendices are not provided in the book but are available as PDF versions on the web site mentioned in the book: http://www.wrox.com.
How to Download the Example Code in This Book
When learning the examples in this book, you can choose to manually enter all the code or use the source code files related to the book. All the source code in this book can be downloaded from http://www.wrox.com/. When you log in to this site, you can use the Search tool or the book title list to find this book. Next, click the Download Code link on the book information page to get all the source code.
Tip: Many book titles are very similar, so using the ISBN to find this book is the simplest, and the ISBN for this book is 0-7645-5759-9. After downloading the code, you can use your favorite compression tool to decompress it. Additionally, you can go to the main download page for Wrox code at http://www.wrox.com/dynamic/books/download.aspx to see the code used in this book and other Wrox books.
Errata
Although we have made every effort to ensure the accuracy of this book, errors are inevitable. If you find any errors in this book, such as spelling errors or code errors, please let us know, and we will be very grateful. The errata list can help other readers avoid frustration, and of course, it also helps provide higher-quality information. To find the errata list for this book on the web site, you can log in to http://www.wrox.com, use the Search tool or the book title list to find this book, and then click the Book Errata link on the book information page. On this page, you can view all the errata that have been submitted and reviewed by the editor. The complete list of book errata links is available through http://www.wrox.com/misc-pages/booklist.shtml. If you do not find the error you discovered on the Book Errata page, you can go to http://www.wrox.com/contact/techsupport.shtml and fill out the form to send us the error you found. We will review your information, and if it is correct, we will upload it to the errata page for this book or adopt it in a later version of the book.
p2p.wrox.com
P2P mailing list was established for discussions between the author and readers. Readers can join the P2P forum on p2p.wrox.com. The forum is a web-based system for transmitting information related to Wrox books and related technology, and for exchanging with other readers and technical users. The forum provides subscription functionality, where you will be sent new posts on the topics you choose when new posts are made on the forum. Wrox authors, editors, and other industry experts and readers will discuss on this forum. There are many different forums on http://p2p.wrox.com that can help readers read this book and benefit from them when developing their own applications. To join the forum, follow the steps below:
(1) Go to p2p.wrox.com and click the Register link.
(2) Read the content, click the Agree button.
(3) Provide the information needed to join the forum and optional information you are willing to provide, and click the Submit button. Then you will receive an email with information on how to verify your account and complete the joining process.
Tip: You can read the information on the forum without joining P2P, but only by joining the forum can you send your own information. After joining the forum, you can send new information and respond to other users' posts. You can read information on the web at any time. If you want the forum to send you new information, you can click the SubscribetothisForum icon for the forum in the forum list. For more information on using Wrox P2P,you can read the P2P FAQ to understand how the forum software works and many common questions and answers related to P2P and Wrox books. To read the FAQ,click the FAQ link on any P2P page.

📌 Related Posts