Back to description
Throughout this book, we emphasize that the C# language cannot be viewed in isolation, but must be considered in parallel... more
Throughout this book, we emphasize that the C# language cannot be viewed in isolation, but must be considered in parallel with the .NET Framework. The C# compiler specifically targets .NET, which means that all code written in C# will always run within the .NET Framework. This has two important consequences for the C# language:
The architecture and methodologies of C# reflect the underlying methodologies of .NET.
In many cases, specific language features of C# actually depend upon features of .NET, or of the .NET base classes.
Because of this dependence, it is important to gain some understanding of the architecture and methodology of .NET before you begin C# programming. That is the purpose of this chapter.
This chapter begins by going over what happens when all code (including C#) that targets .NET is compiled and run. Once you have this broad overview, you take a more detailed look at the Microsoft Intermediate Language (MSIL or simply IL), the assembly language that all compiled code ends up in on .NET. In particular, you see how IL, in partnership with the Common Type System (CTS) and Common Language Specification (CLS), works to give you interoperability between languages that target .NET. This chapter also discusses where common languages (including Visual Basic and C++) fit into .NET.
Next, you move on to examine some of the other features of .NET, including assemblies, namespaces, and the .NET base classes. The chapter finishes with a brief look at the kinds of applications you can create as a C# developer.
... less
Now that you understand a little more about what C# can do, you will want to learn how to use it.... more
Now that you understand a little more about what C# can do, you will want to learn how to use it. This chapter on the basics of C# gives you a good start in that direction by providing you with a basic knowledge of the fundamentals of C# programming, which we build on in subsequent chapters. The main topics we cover are:
Declaring variables
Initialization and scope of variables
Predefined C# data types
Dictating the flow of execution within a C# program using loops and conditional statements
Enumerations
Namespaces
The Main() method
Main()
Basic command-line C# compiler options
Using System.Console to perform console I/O
System.Console
Using documentation features in C# and Visual Studio .NET
C# identifiers and keywords
Recommended guidelines and conventions for good programming in C#
By the end of this chapter, you will know enough C# to write simple programs, though without using inheritance or other object-oriented features, which are covered in the following chapters.
So far, you’ve been introduced to some of the main building blocks that make up the C# language,... more
So far, you’ve been introduced to some of the main building blocks that make up the C# language, including declaring variables, data types, and program flow statements, and you have seen a couple of very short complete programs containing little more than the Main() method. What you haven’t really seen is how you can put all these together to form a longer, complete program. The key to this lies in working with classes the subject of this chapter. In particular, this chapter covers:
The differences between classes and structs
Fields, properties, and methods
Passing values by value and reference
Method overloading
Constructors and static constructors
Read-only fields
The Object class, from which all other types are derived
Object
Inheritance and features related to inheritance are discussed in Chapter 4, “Inheritance.”
Tip
This chapter introduces the basic syntax associated with classes. However, we assume that you are already familiar with the underlying principles of using classes for example, that you know what a constructor or a property is, and this chapter is largely confined to applying those principles in C# code.
This chapter introduces and explains those concepts that are not necessarily supported by most object-oriented languages. For example, although object constructors are a widely used concept that you should be familiar with, static constructors are something new to C#, so this chapter explains how static constructors work.
Chapter 3, “Objects and Types,” examined how to use individual classes in C#. The focus in that chapter was... more
Chapter 3, “Objects and Types,” examined how to use individual classes in C#. The focus in that chapter was on how to define methods, constructors, properties, and other members of a single class (or a single struct). Although you did learn that all classes are ultimately derived from the class System.Object, you did not see how to create a hierarchy of inherited classes. Inheritance is the subject of this chapter. It briefly discusses the scope of C#’s support for inheritance before examining in detail how to code first implementation inheritance and then interface inheritance in C#. Note that this chapter presumes familiarity with the basic concepts of inheritance, including virtual functions and overriding. This chapter concentrates on the syntax used to provide inheritance and inheritance-related topics, such as virtual functions, and on those aspects of the C# inheritance model that are particular to C# and not necessarily shared by other object-oriented languages.
System.Object
If you need to work with multiple objects of the same type, you can use collections and arrays. C# has a special notation... more
If you need to work with multiple objects of the same type, you can use collections and arrays. C# has a special notation to declare and use arrays. Behind the scenes, the Array class comes into play that offers several methods to sort and filter the elements inside the array.
Array
Using an enumerator, you can iterate through all the elements of an array.
This chapter discusses the following:
Simple arrays
Multidimensional arrays
Jagged arrays
Array class
Interfaces for arrays
The preceding chapters have covered most of what you need to start writing useful programs using C#.... more
The preceding chapters have covered most of what you need to start writing useful programs using C#. This chapter completes the discussion of the essential language elements and goes on to discuss powerful aspects of C# that allow you to extend the capabilities of the C# language. Specifically, this chapter discusses the following:
The operators available in C#
The idea of equality when dealing with reference and value types
Data conversion between the primitive data types
Converting value types to reference types using boxing
Converting between reference types by casting
Overloading the standard operators to support operations on the custom types you define
Adding cast operators to the custom types you define to support seamless data type conversions
Callback functions are an important part of programming in Windows. If you have a background in C or C++ programming,... more
Callback functions are an important part of programming in Windows. If you have a background in C or C++ programming, you have seen callbacks used in many of the Windows APIs. With the addition of the AddressOf keyword, Visual Basic developers are now able to take advantage of the API that once was off limits. Callback functions are really pointers to a method call. Also known as function pointers, they are a very powerful programming feature. .NET has implemented the concept of a function pointer in the form of delegates. What makes them special is that unlike the C function pointer, the .NET delegate is type-safe. What this means is that a function pointer in C is nothing but a pointer to a memory location. You have no idea what that pointer is really pointing to. Things like parameters and return types are not known. As you see in this chapter, .NET has made delegates a type-safe operation. Later in the chapter, you see how .NET uses delegates as the means of implementing events.
AddressOf
In the beginning of this book, you have been almost constantly using strings and have taken for granted the stated mapping... more
In the beginning of this book, you have been almost constantly using strings and have taken for granted the stated mapping that the string keyword in C# actually refers to the .NET base class. System.String. System.String is a very powerful and versatile class, but it is by no means the only string-related class in the .NET armory. This chapter starts off by reviewing the features of System.String and then looks at some quite nifty things you can do with strings using some of the other .NET classes in particular those in the System.Text and System.Text.RegularExpressions namespaces. This chapter covers the following areas:
string
System.String
System.Text
System.Text.RegularExpressions
Building strings If you’re performing repeated modifications on a string, for example in order to build up a lengthy string prior to displaying it or passing it to some other method or application, the String class can be very inefficient. For this kind of situation, another class, System.Text.StringBuilder is more suitable, because it has been designed exactly for this situation.
String
System.Text.StringBuilder
Formatting expressions You also take a closer look at those formatting expressions that have been used in the Console.WriteLine() method throughout these last few chapters. These formatting expressions are processed using a couple of useful interfaces, IFormatProvider and IFormattable, and by implementing these interfaces on your own classes, you can actually define your own formatting sequences so that Console.WriteLine() and similar classes will display the values of your classes in whatever way you specify.
Console.WriteLine()
IFormatProvider
IFormattable
Regular expressions .NET also offers some very sophisticated classes that deal with the situation in which you need to identify or extract substrings that satisfy certain fairly sophisticated criteria; for example, finding all occurrences within a string where a character or set of characters is repeated, finding all words that begin with s and contain at least one n, or strings that adhere to employee ID or Social Security number constructions. Although you can write methods to perform this kind of processing using the String class, such methods are cumbersome to write. Instead, you can use some classes from System.Text.RegularExpressions, which are designed specifically to perform this kind of processing.
One of the biggest changes of the C# language and the CLR is the introduction of generics. With .NET 1.0,... more
One of the biggest changes of the C# language and the CLR is the introduction of generics. With .NET 1.0, creating a flexible class or method that should use classes that are not known at compile time must be based on the Object class. With the Object class, there’s no type safety during compile time. Casting is necessary. Also, using the Object class for value types has a performance impact.
.NET 2.0 supports generics. With generics, the Object class is no longer necessary in such scenarios. Generic classes make use of generic types that are replaced with specific types as needed. This allows for type safety: the compiler complains if a specific type is not supported with the generic class.
Generics are a great feature, especially with collection classes. Most of the .NET 1.0 collection classes are based on the Object type. .NET 2.0 offers new collection classes that are implemented as generics.
Generics are not limited to classes; in this chapter, you also see generics with delegates, interfaces, and methods.
Generics overview
Creating generic classes
Features of generic classes
Generic interfaces
Generic methods
Generic delegates
Other generic framework types
In Chapter 5, “Arrays,” you read information about arrays and the interfaces implemented by the Array... more
In Chapter 5, “Arrays,” you read information about arrays and the interfaces implemented by the Array class. The size of arrays is fixed. If the number of elements is dynamic, you should use a collection class.
List<T> and ArrayList are collection classes that can be compared to arrays. But there are also other kinds of collections: queues, stacks, linked lists, and dictionaries.
List<T>
ArrayList
This chapter shows you how to work with groups of objects. It takes a close look at these topics:
Collection interfaces and types
Lists
Queues
Stack
Linked lists
Sorted lists
Dictionaries
Dictionary with multiple keys
Bit arrays
Performance
This chapter looks at various aspects of memory management and memory access. Although the runtime takes much of the responsibility... more
This chapter looks at various aspects of memory management and memory access. Although the runtime takes much of the responsibility for memory management away from the programmer, it is useful to understand how memory management works and important to know how to work with unmanaged resources efficiently.
If you have a good understanding of memory management and knowledge of the pointer capabilities provided by C#, you are also better positioned to integrate C# code with legacy code and perform efficient memory manipulation in performance-critical systems.
Specifically, this chapter discusses:
How the runtime allocates space on the stack and the heap
How garbage collection works
How to use destructors and the System.IDisposable interface to ensure unmanaged resources are released correctly
System.IDisposable
The syntax for using pointers in C#
How to use pointers to implement high-performance stack-based arrays
Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime.... more
Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime. For example, reflection allows you to:
Enumerate the members of a type
Instantiate a new object
Execute the members of an object
Find out information about a type
Find out information about an assembly
Inspect the custom attributes applied to a type
Create and compile a new assembly
This list represents a great deal of functionality and encompasses some of the most powerful and complex capabilities provided by the .NET Framework class library. Unfortunately, this chapter does not have the space to cover all the capabilities of reflection, so it focuses on those elements you are likely to use frequently.
The discussion begins with custom attributes, a mechanism that allows you to associate custom metadata with program elements. This metadata is created at compile time and embedded in an assembly. You can then inspect the metadata at runtime using some of the capabilities of reflection.
After looking at custom attributes, the chapter also looks at some of the fundamental classes that enable reflection, including the System.Type and System.Reflection.Assembly classes, which provide the access points for much of what you can do with reflection.
System.Type
System.Reflection.Assembly
To demonstrate custom attributes and reflection, you will develop an example based on a company that regularly ships upgrades to its software, and wants to have details of these upgrades documented automatically. In the example, you define custom attributes that indicate the date when program elements were last modified, and what changes were made. You then use reflection to develop an application that looks for these attributes in an assembly, and can automatically display all the details about what upgrades have been made to the software since a given date.
Another example in this chapter considers an application that reads from or writes to a database and uses custom attributes as a way of marking which classes and properties correspond to which database tables and columns. By reading these attributes from the assembly at runtime, the program is able to automatically retrieve or write data to the appropriate location in the database, without requiring specific logic for each table or column.
Errors happen, and it isn’t always because of the person who coded the application. Sometimes your application will... more
Errors happen, and it isn’t always because of the person who coded the application. Sometimes your application will generate an error because of an action that was initiated by the end user of your application. In any case, you should anticipate errors occurring in your applications and code accordingly.
The .NET Framework has enhanced the ways in which you deal with errors. C#’s mechanism for handling error conditions allows you to provide custom handling for each type of error condition as well as to separate code that identifies errors from the code that handles them.
The main topics covered in this chapter include:
Looking at the exception classes
Using try – catch – finally to capture exceptions
try
catch
finally
Creating user-defined exceptions
By the end of the chapter, you will have a good handle on advanced exception handling in your C# applications.
At this point you should be familiar with the C# language and almost ready to move on to the applied sections of the book,... more
At this point you should be familiar with the C# language and almost ready to move on to the applied sections of the book, which look at how to use C# to program a variety of applications. Before doing that, however, you need to examine how you can use Visual Studio and some of the features provided by the .NET environment to get the best from your programs.
This chapter looks at what programming in the .NET environment means in practice. It covers Visual Studio, the main development environment in which you will write, compile, debug, and optimize your C# programs, and provides guidelines for writing good applications. Visual Studio is the main IDE used for everything from writing Web Forms and Windows Forms to XML Web services, and more. For more details on Windows Forms and how to write user interface code, see Chapter 28, “Windows Forms.”
This chapter also looks at what it takes to build applications that are targeted at the .NET Framework 3.0. The types of applications provided through the .NET Framework 3.0 class library include the Windows Presentation Foundation (WPF), the Windows Communication Foundation (WCF), and the Windows Workflow Foundation (WF).
The development process does not end when the source code is compiled and testing is complete. At that stage,... more
The development process does not end when the source code is compiled and testing is complete. At that stage, the job of getting the application into the user’s hands begins. Whether it’s an ASP.NET application, a smart client application, or an application built using the Compact Framework, the software must be deployed to a target environment. The .NET Framework has made deployment much easier than it was in the past. The pains of registering COM components and writing new hives to the registry are all gone.
This chapter looks at the options that are available for application deployment, both from an ASP.NET perspective and from the smart client perspective. The following topics are discussed:
Deployment requirements
Simple deployment scenarios
Windows Installer–based projects
ClickOnce
An assembly is the .NET term for a deployment and configuration unit. This chapter discusses exactly what assemblies... more
An assembly is the .NET term for a deployment and configuration unit. This chapter discusses exactly what assemblies are, how they can be applied, and why they are such a useful feature. In particular, this chapter covers the following topics:
The innovations offered by assemblies over previous technologies
How to create and view assemblies
What the Common Language Specification means, and how cross-language support is made possible
How to share assemblies
The chapter begins with an overview of assemblies.
Chapter 13 of this book covered errors and exception handling. However, besides handling exceptional code it might be... more
Chapter 13 of this book covered errors and exception handling. However, besides handling exceptional code it might be really interesting to get some live information of your running application. This is where the namespace System.Diagnostics comes into play.
System.Diagnostics
The application doesn’t throw exceptions, but sometimes it doesn’t behave as expected. The application might be running well on most systems, but there’s a problem on a few. On the live system, you change the log behavior by changing a configuration value and get detailed live information about what’s going on in the application. This can be done with tracing.
If there are problems with applications the system administrator needs to be informed. With the Event Viewer the system administrator both interactively monitors problems with applications and gets informed about specific events that happen by adding subscriptions. The event-logging mechanism allows you to write information about the application.
To analyze resources needed from applications, monitor applications with specified time intervals, plan for a different application distribution or extending of system resources, the system administrator is using the performance monitor. You can write live data of your application using performance counts.
This chapter explains these three facilities and demonstrates how you can use them from your applications:
Tracing
Event logging
Performance monitoring
You are making a network call from an application that might take some time. You don’t want to stall the user interface... more
You are making a network call from an application that might take some time. You don’t want to stall the user interface and just let the user wait until the response is returned from the server. The user could do some other actions in the meantime or even cancel the request that was sent to the server. Using threads helps.
There are several reasons for using threading. Not making the user wait is one of the reasons. For all activities that require a wait, for example because of a file, database, or network access, a new thread can be started to fulfill other tasks at the same time. Even if you’ve just processing-intensive tasks to do, threading can help. Multiple threads of a single process can run on different CPUs, or nowadays on different cores of a multiple-core CPU, at the same time.
You must also be aware of some issues running multiple threads. As they can run during the same time, you can easily get into problems if the threads access the same data. You must implement synchronization mechanisms.
This chapter gives you the information you need to know when programming applications with multiple threads, including:
An overview of threading
Lightweight threading using delegates
Thread class
Thread pools
Threading issues
Synchronization techniques
COM apartments
BackgroundWorker
You’re sitting at your machine, and you click a button on an application you’re using.... more
You’re sitting at your machine, and you click a button on an application you’re using. Behind the scenes, your application responds to the fact that you are attempting to use a feature for which it does not have the relevant module. It connects to the Internet, downloads the module into the Download Assembly Cache, and begins executing all without prompting you.
This kind of behind-the-scenes upgrade functionality is already used with many .NET applications, but there is a concern here over the security implications relating to what is called mobile code. In clear terms, what evidence do you actually have that you can trust the code your computer is downloading? How do you know that the module you requested is, in fact, the one that you are receiving? What does the CLR do behind the scenes to ensure, for example, that a control on a Web site is not reading your private e-mails?
.NET enforces a security policy around assemblies. It uses the evidence it has about them where they are from or by whom they are published to group the assemblies with similar characteristics. For example, the runtime places all code from the local intranet into a specific group. It then uses the security policy (normally defined by a system administrator using the Code Access Security Policy tool (caspol.exe) command-line utility, or the Microsoft Management Console) to decide what permissions the code should be granted at a very granular level. What do you have to do to enable security on a machine or for a specific application? Nothing all code automatically runs within the security context of the CLR, although you can turn off security if necessary.
caspol.exe
In addition to high levels of confidence that the code you are executing can be trusted, it is also important to allow the application users access to the features they need, but nothing more. By virtue of its role-based security, .NET facilitates effective management of users and roles.
This chapter looks through the features available in .NET to help you manage security, including how .NET protects you from malicious code, how to administer security policies, and how to access the security subsystem programmatically. It also looks at deploying .NET applications securely and provides a number of short example applications to solidify the concepts in this chapter for you.
NASA’s Mars Climate Orbiter was lost on September 23, 1999, at a cost of $125 million because one engineering team... more
NASA’s Mars Climate Orbiter was lost on September 23, 1999, at a cost of $125 million because one engineering team used metric units, while another one used inches for a key spacecraft operation. When writing applications for international distribution, different cultures and regions must be kept in mind.
Different cultures have diverging calendars and use different number and date formats. Also, sorting strings may lead to various results because the order of A–Z is defined differently based on the culture. To make applications fit for global markets, you have to globalize and localize them.
Globalization is about internationalizing applications: preparing applications for international markets. With globalization, the application supports number and date formats that vary depending on the culture, different calendars, and so on. Localization is about translating applications for specific cultures. For translations of strings, you can use resources.
.NET supports globalization and localization of Windows and Web applications. To globalize an application, you can use classes from the namespace System.Globalization; to localize an application, you can use resources that are supported by the namespace System.Resources.
System.Globalization
System.Resources
This chapter covers the globalization and localization of .NET applications; more specifically, it discusses the following:
Using classes that represent cultures and regions
Internationalization of applications
Localization of applications
All or nothing this is the main feature of a transaction. When writing a few records ... more
All or nothing this is the main feature of a transaction. When writing a few records either all are written, or everything will be undone. If there is just one failure when writing one records, all the other things that are done within the transaction are rolled back.
Transactions are commonly used with databases, but with classes from the namespace System.Transactions you can also perform transactions on volatile or in-memory based objects such as a list of objects. With a list that supports transactions, if an object is added or removed and the transaction fails, the list action is automatically undone. Writing to a memory-based list can be done in the same transaction as writing to a database.
System.Transactions
In Windows Vista, the file system and registry also get transactional support. Writing a file and making changes within the registry supports transactions.
In this chapter, the following topics on transactions are covered:
Overview
Traditional transactions
Committable transactions
Transaction promotions
Dependent transactions
Ambient transactions
Transaction isolation level
Custom resource managers
Transactions with Windows Vista
Windows Services are programs that can be started automatically at boot time without the need for anyone to log on to... more
Windows Services are programs that can be started automatically at boot time without the need for anyone to log on to the machine.
In the following pages, you learn:
The architecture of Windows Services, including the functionality of a service program, a service control program, and a service configuration program
How to implement a Windows Service with the classes found in the System.ServiceProcess namespace
System.ServiceProcess
Installation programs to configure the Windows Service in the registry
How to write a program to control the Windows Service using the ServiceController class
ServiceController
The first section explains the architecture of Windows Services.
You can download the code for this chapter from the Wrox Web site at www.wrox.com.
www.wrox.com
If you have Windows programs written prior to .NET, you probably don’t have the time and resources to rewrite everything... more
If you have Windows programs written prior to .NET, you probably don’t have the time and resources to rewrite everything for .NET. Sometimes rewriting code is useful to do some refactoring, rethinking the application architecture. A rewrite can also help with productivity in the long-term when adding new features is easier to do with the new technology. However, there should not be a reason to rewrite old code just because a new technology is available. You might have thousands of lines of existing, running code, which would require too much effort to rewrite just to move it into the managed environment.
The same applies to Microsoft. With the namespace System.DirectoryServices, Microsoft hasn’t rewritten the COM objects accessing the hierarchical data store; the classes inside this namespace are wrappers accessing the ADSI COM objects instead. The same thing happens with System.Data.OleDb, where the OLE DB providers that are used by classes from this namespace do have quite complex COM interfaces.
System.DirectoryServices
System.Data.OleDb
The same issue may apply for your own solutions. If you have existing COM objects that should be used from .NET applications, or the other way around if you want to write .NET components that should be used in old COM clients, this chapter will be a starter for using COM interoperability.
If you don’t have existing COM components you want to integrate with your application, or old COM clients that should use some .NET components, you can skip this chapter.
COM and .NET technologies
Using COM objects from within .NET applications
Using .NET components from within COM clients
Platform invoke for invoking native methods
Like all other chapters, you can download the sample code for this chapter from the Wrox Web site at www.wrox.com.
This chapter examines how to perform tasks involving reading from and writing to files and the system registry in C#.... more
This chapter examines how to perform tasks involving reading from and writing to files and the system registry in C#. In particular, it covers the following:
Exploring the directory structure, finding out what files and folders are present, and checking their properties
Moving, copying, and deleting files and folders
Reading and writing text in files
Reading and writing keys in the registry
Reading and writing to isolated storage
Microsoft has provided very intuitive object models covering these areas, and in this chapter you learn how to use .NET base classes to perform the tasks mentioned in the preceding list. In the case of file system operations, the relevant classes are almost all found in the System.IO namespace, whereas registry operations are dealt with by classes in the Microsoft.Win32 namespace.
System.IO
Microsoft.Win32
Note
The .NET base classes also include a number of classes and interfaces in the System.Runtime.Serialization namespace concerned with serialization that is, the process of converting data (for example, the contents of a document) into a stream of bytes for storage. This chapter doesn’t focus on these classes; it focuses on the classes that give you direct access to files.
System.Runtime.Serialization
Note that security is particularly important when modifying files or registry entries. The whole area of security is covered separately in Chapter 19, “.NET Security.” In this chapter, however, we assume that you have sufficient access rights to run all the examples that modify files or registry entries, which should be the case if you are running from an account with administrator privileges.
This chapter discusses how to get at data from your C# programs using ADO.NET and covers the following details:... more
This chapter discusses how to get at data from your C# programs using ADO.NET and covers the following details:
Connecting to the database You learn how to use the SqlConnection and OleDbConnection classes to connect to and disconnect from the database.
SqlConnection
OleDbConnection
Executing commands ADO.NET has command objects, which can execute SQL commands or issue a stored procedure with return values. You learn the various command object options and see how commands can be used for each of the options presented by the Sql and OleDB classes.
Sql
OleDB
Stored procedures You learn how to call stored procedures with command objects, and how the results of those stored procedures can be integrated into the data cached on the client.
The ADO.NET object model This is significantly different from the objects available with ADO, and the DataSet, DataTable, DataRow, and DataColumn classes are discussed as well as the relationships between tables and constraints that are part of DataSet. The class hierarchy has changed significantly with version 2 of the .NET Framework, and some of these changes are also described.
DataSet
DataTable
DataRow
DataColumn
Using XML and XML schemas You examine the XML framework on which ADO.NET is built.
As is the case with the other chapters, you can download the code for the examples used in this chapter from the Wrox Web site at www.wrox.com. The chapter begins with a brief tour of ADO.NET.
XML plays a significant role in the .NET Framework. Not only does the .NET Framework allow you to use XML in your application,... more
XML plays a significant role in the .NET Framework. Not only does the .NET Framework allow you to use XML in your application, but the .NET Framework itself uses XML for configuration files and source code documentation, as do SOAP, Web services, and ADO.NET, to name just a few.
To accommodate this extensive use of XML, the .NET Framework includes the System.Xml namespace. This namespace is loaded with classes that can be used for the processing of XML, and many of these classes are discussed in this chapter.
System.Xml
This chapter discusses how to use the XmlDocument class, which is the implementation of the Document Object Model (DOM), as well as what .NET offers as a replacement for SAX (the XmlReader and XmlWriter classes). It also discusses the class implementations of XPath and XSLT and demonstrates how XML and ADO.NET work together, as well as how easy it is to transform one to the other. You also learn how you can serialize your objects to XML and create an object from (or deserialize) an XML document using classes in the System.Xml.Serialization namespace. More to the point, you learn how you can incorporate XML into your C# applications.
XmlDocument
XmlReader
XmlWriter
System.Xml.Serialization
You should note that the XML namespace allows you to get similar results in a number of different ways. It is impossible to include all these variations in one chapter, so while exploring one possible way of doing things we’ll try our best to mention alternative routes that will yield the same or similar results.
Because there’s not space here to teach you XML from scratch, we are assuming that you are already somewhat familiar with XML technology. For example, you should be familiar with elements, attributes, and nodes, and you should also know what is meant by a well-formed document. You should also be familiar with SAX and DOM. If you want to find out more about XML, Wrox’s Beginning XML (Wiley Publishing, Inc., ISBN 0-7645-7077-3) is a great place to start.
This chapter covers the following:
XML Standards
XmlReader and XmlWriter
XPathDocument
XmlNavigator
The discussion begins with a brief overview of the current status of XML standards.
SQL Server 2005 is the first version of this database product that hosts the .NET runtime. In fact,... more
SQL Server 2005 is the first version of this database product that hosts the .NET runtime. In fact, it is the first new version of Microsoft’s SQL Server product in nearly six years. It allows running .NET assemblies in the SQL Server process. Furthermore, it enables you to create stored procedures, functions, and data types with .NET programming languages such as C# and Visual Basic.
In this chapter, you will find the following:
Hosting the .NET runtime with SQL Server
Classes from the namespace System.Data.SqlServer
System.Data.SqlServer
Creating user-defined types
Creating user-defined aggregates
Stored procedures
User-defined functions
Triggers
XML data types
SQL Server 2005 also has many new features that are not directly associated with the CLR, such as many T-SQL improvements, but they are not covered in this book. To get more information about these features you can read Wrox’s SQL Server 2005 Express Edition Starter Kit (Wiley Publishing, Inc., ISBN 0-7645-8923-7).
The samples in this chapter make use of a ProCSharp database that you can download with the code samples, and the AdventureWorks database. The AdventureWorks database is a sample database from Microsoft that you can install as optional component with SQL Server 2005.
Web-based applications have become very popular over the past several years. The ability to have all of your application... more
Web-based applications have become very popular over the past several years. The ability to have all of your application logic reside on a centralized server is very appealing from an administrator’s viewpoint. Deploying client-based software can be very difficult, especially COM-based client software. The downside of Web-based applications is that they cannot provide that rich user experience. The .NET Framework has given developers the ability to create rich, smart client applications and eliminate the deployment problems and “DLL Hell” that existed before. Whether Windows Forms or Windows Presentation Foundation (see Chapter 24) is chosen, client applications are no longer difficult to develop or deploy.
Windows Forms has already made an impact on Windows development. Now when an application is in the initial design phase, the decision between building a Web-based application or a client application has become a little more difficult. Windows client applications can be developed quickly and efficiently, and they can provide users with the rich experience that they expect.
Windows Forms will seem somewhat familiar if you are a Visual Basic developer. You create new forms (also known as windows or dialogs) in the same fashion of dragging and dropping controls from a toolbox onto the form Designer. However, if your background is in the classic C style of windows programming where you create the message pump and monitor messages, or if you’re an MFC programmer, you will find that you’re able to get to the lower-level internals if you need to. You can override the wndproc and catch those messages, but you might be surprised that you really won’t need to very often.
This chapter looks at the following aspects of Windows Forms:
The Form class
Form
The class hierarchy of Windows Forms
The controls and components that are part of the System.Windows.Forms namespace
System.Windows.Forms
Menus and toolbars
Creating controls
Creating user controls
This chapter builds on the content of Chapter 25, “Data Access with .NET,” which covers various ways of selecting... more
This chapter builds on the content of Chapter 25, “Data Access with .NET,” which covers various ways of selecting and changing data, showing you how to present data to the user by binding to various Windows controls. More specifically, this chapter discusses:
Displaying data using the DataGridView control new with Visual Studio 2005
DataGridView
The .NET data-binding capabilities and how they work
How to use the Server Explorer to create a connection and generate a DataSet class (all without writing a line of code)
How to use hit testing and reflection on rows in the DataGrid
DataGrid
You can download the source code for the examples in this chapter from the Wrox Web site at www.wrox.com.
.
This is the third of the eight chapters that deal with user interaction and the .NET Framework. Chapter 28,... more
This is the third of the eight chapters that deal with user interaction and the .NET Framework. Chapter 28, “Windows Forms,” focused on Windows Forms and discussed how to display a dialog box or SDI or MDI window, and how to place various controls such as buttons, text boxes, and list boxes. Chapter 29, “Viewing .NET Data,” looked at working with data in Windows Forms using a number of the Windows Forms controls that work with the disparate data sources that you might encounter.
Although these standard controls are powerful and, by themselves, quite adequate for the complete user interface for many applications, there are some situations in which you need more flexibility. For example, you might want to draw text in a given font in a precise position in a window, display images without using a picture box control, or draw simple shapes or other graphics. None of this can be done with the controls discussed in Chapter 28. To display that kind of output, the application must instruct the operating system what to display and where in its window to display it.
Therefore, this chapter shows you how to draw a variety of items including:
Lines and simple shapes
BMP images and other image files
Text
In the process, you’ll need to use a variety of helper objects, including pens (to define the characteristics of lines), brushes (to define how areas are filled in), and fonts (to define the shape of the characters of text). The chapter also goes into some detail on how devices interpret and display different colors.
The chapter starts, however, by discussing a technology called GDI+. GDI+ consists of the set of .NET base classes that are available to control custom drawing on the screen. These classes arrange for the appropriate instructions to be sent to graphics device drivers to ensure the correct output is placed on the screen (or printed to a hard copy).
Windows Presentation Foundation (WPF) is one of the three major extensions of .NET Framework 3.0.... more
Windows Presentation Foundation (WPF) is one of the three major extensions of .NET Framework 3.0. WPF is a new library to create the UI for smart client applications. While the Windows Forms controls are based on native Windows controls that make use of Window handles that are based on screen pixels, WPF is based on DirectX. The application is no longer using Window handles, it is easy to resize the UI, and support for sound and video is included.
The main topics of this chapter are:
An overview of WPF
Shapes as the base drawing elements
WPF controls and their features
How to define a layout with WPF panels
The WPF event-handling mechanism
Styles, templates, and resources
How to create animations
WPF data-binding features
Windows Forms Integration
This chapter requires that you have .NET Framework 3.0 and the .NET Framework 3.0 Extensions for Visual Studio 2005 installed.
If you are new to the world of C# and .NET, you might wonder why a chapter on ASP.NET has been included in this book.... more
If you are new to the world of C# and .NET, you might wonder why a chapter on ASP.NET has been included in this book. It’s a whole new language, right? Well, not really. In fact, as you will see, you can use C# to create ASP.NET pages.
ASP.NET is part of the .NET Framework and is a technology that allows for the dynamic creation of documents on a Web server when they are requested via HTTP. This mostly means HTML documents, although it is equally possible to create WML documents for consumption on WAP browsers, or anything else that supports MIME types.
In some ways ASP.NET is similar to many other technologies such as PHP, ASP, or ColdFusion. There is, however, one key difference: ASP.NET, as its name suggests, has been designed to be fully integrated with the .NET Framework, part of which includes support for C#.
Perhaps you are familiar with Active Server Pages (ASP) technology, which enables you to create dynamic content. If this is the case, you will probably know that programming in this technology used scripting languages such as VBScript or JScript. The result was not always perfect, at least not for those of us used to “proper,” compiled programming languages, and it certainly resulted in a loss of performance.
One major difference related to the use of more advanced programming languages is the provision of a complete server-side object model for use at runtime. ASP.NET provides access to all of the controls on a page as objects, in a rich environment. On the server side you also have access to other .NET classes, allowing for the integration of many useful services. Controls used on a page expose a lot of functionality; in fact, you can do almost as much as with Windows Forms classes, which provide plenty of flexibility. For this reason, ASP.NET pages that generate HTML content are often called Web Forms.
This chapter takes a more detailed look at ASP.NET, including how it works, what you can do with it, and how C# fits in.
It has often been the case with Web development that the tools available, however powerful, don’t quite match up... more
It has often been the case with Web development that the tools available, however powerful, don’t quite match up with your requirements for a specific project. Perhaps a given control doesn’t quite work as you’d like it to, or perhaps one section of code, intended for reuse on several pages, is too complex in the hands of multiple developers. In cases such as these, there is a strong argument for building your own controls. Such controls can, at their simplest, wrap multiple existing controls together, perhaps with additional properties specifying layout. They can also be completely unlike any existing control. Using a control you have built yourself can be as simple as using any other control in ASP.NET (if you have written them well), which can certainly ease Web site coding.
In the first part of this chapter, you examine the options available to control developers, and assemble some simple user controls of your own. You also look at the basics of more advanced control construction, although you won’t see these in any great depth; whole books are devoted to the subject.
Next, you look at master pages, a technique new to ASP.NET 2.0 that enables you to provide templates for your Web sites. Using master pages you can implement complex layouts on Web pages throughout a Web site with a great deal of code reuse. You also see how you can use the navigation Web server controls in combination with a master page to provide consistent navigation across a Web site.
Site navigation can be made user-specific, such that only certain users (those that are registered with the site, or site administrators, say) can access certain sections. You also look at site security and how to log in to Web sites in this chapter something that is made extremely easy via the login Web server controls.
After that, you look at some more advanced styling techniques, namely providing and choosing themes for Web sites, which separate the presentation of your Web pages from their functionality. You can supply alternative CSS style sheets for your sites as well as different skins for Web server controls.
Finally, you’ll see how to use Web Parts to enable your users to dynamically personalize Web pages by positioning and customizing controls on a page.
Throughout this chapter you refer to one large example application that includes all the techniques you’ve seen in this and the last chapter. This application, PCSDemoSite, is available in the downloadable code for this chapter. It’s a little too large to include all the code here, but you don’t need to have it running in front of you to learn about the techniques it illustrates. The relevant sections of code are examined as and when necessary, and the additional code (mostly dummy content or simple code you’ve already seen) is left for you to examine at your convenience.
PCSDemoSite
Web application programming is an area that is subject to continuous change and improvement. In the previous two chapters,... more
Web application programming is an area that is subject to continuous change and improvement. In the previous two chapters, you have seen how to use ASP.NET to create fully functional Web applications, and you may think that you have seen all the tools that you need to create your own Web applications. However, if you spend much time looking at current Web sites, you may have noticed that more recent Web sites are significantly better, in terms of usability, than older Web sites. Many of today’s best Web sites provide rich user interfaces that feel almost as responsive as Windows applications. They achieve this by using client-side processing, primarily through JavaScript code, and increasingly through a technology known as Ajax.
This change of direction is possible because the browsers that clients use to browse Web sites, and the computers that clients use to run browsers, have become more powerful. The current generation of Web browsers, such as Internet Explorer 7 and Firefox, also support a wide variety of standards. These standards, which include JavaScript, enable Web applications to provide functionality far in advance of what was previously possible using plain HTML. You have already seen some of this in previous chapters for example the use of cascading style sheets (CSS) to style Web applications.
Ajax as you will discover shortly is not a new technology. Rather, it is a combination of standards that makes it possible to realize the rich potential functionality of current Web browsers.
Perhaps the key defining feature of Ajax-enabled Web applications is the ability for the Web browser to communicate with the Web server in out-of-band operations; this is known as asynchronous, or partial-page, postbacks. In practice, this means that the user can interact with server-side functionality and data without needing a full-page refresh. For example, when a link is followed to move to the second page of data in a table, Ajax makes it possible to refresh just the table’s content rather than the entire Web page. This means that there is less traffic required across the internet, which leads to a more responsive Web application. You will see this example in practice later in this chapter, as well as many more examples that illustrate the power of Ajax in Web applications.
You will be using Microsoft’s implementation of Ajax in the code in this chapter, known as ASP.NET AJAX. This implementation takes the Ajax model and applies it to the ASP.NET framework. ASP.NET AJAX provides a number of server controls and client-side techniques that are specifically targeted at ASP.NET developers and enable you to add Ajax functionality to your Web applications with surprisingly little effort.
In the first part of this chapter, you will learn more about Ajax and the technologies that make Ajax possible. In the second part of this chapter, you will learn about ASP.NET AJAX and its component parts, as well as the functionality that ASP.NET AJAX offers. In the third and largest part of this chapter, you will see how to use ASP.NET AJAX in your Web applications, by using both server-side and client-side code.
Chapters 32 through 34 as well as Chapter 36 discuss how you can use C# to write powerful, efficient,... more
Chapters 32 through 34 as well as Chapter 36 discuss how you can use C# to write powerful, efficient, and dynamic Web pages using ASP.NET and XML Web services. For the most part, the clients accessing ASP.NET pages will be users running Internet Explorer or other Web browsers such as Opera or FireFox. However, you might want to add Web-browsing features to your own application, or need your applications to programmatically obtain information from a Web site. In this latter case, it is usually better for the site to implement a Web service. However, if you are accessing public Internet sites, you might not have any control over how the site is implemented.
This chapter covers facilities provided through the .NET base classes for using various network protocols, particularly HTTP and TCP, to access networks and the Internet as a client. In particular, this chapter covers:
Downloading files from the World Wide Web
Using the new Web Browser control in a Windows Forms application
Manipulating IP addresses and performing DNS lookups
Socket programming with TCP, UDP, and socket classes
The two namespaces of most interest for networking are System.Net and System.Net.Sockets. The System.Net namespace is generally concerned with higher-level operations, for example, downloading and uploading files, and making Web requests using HTTP and other protocols, whereas System.Net.Sockets contains classes to perform lower-level operations. You will find these classes useful when you want to work directly with sockets or protocols such as TCP/IP. The methods in these classes closely mimic the Windows socket (Winsock) API functions derived from the Berkeley sockets interface.
System.Net
System.Net.Sockets
This chapter takes a fairly practical approach, mixing examples with a discussion of the relevant theory and networking concepts as appropriate. This chapter is not a guide to computer networking but an introduction to using the .NET Framework for network communication.
You also take a look at using the new WebBrowser control in a Windows Forms environment and how it can make accomplishing some specific Internet access tasks easier for you to accomplish.
WebBrowser
However, the chapter starts with the simplest case, sending a request to a server and storing the information sent back in the response. (As is the case with the other chapters, you can download the sample code for this chapter from the Wrox Web site at www.wrox.com.)
Web services are a way of performing remote method calls over HTTP that can make use of Simple... more
Web services are a way of performing remote method calls over HTTP that can make use of Simple Object Access Protocol (SOAP). In the past, this issue was fraught with difficulty, as anyone who has any DCOM (Distributed COM) experience knows. The act of instantiating an object on a remote server, calling a method, and obtaining the result was far from simple, and the necessary configuration was even trickier.
SOAP simplifies matters immensely. This technology is an XML-based standard that details how method calls can be made over HTTP in a reproducible manner. A remote SOAP server is capable of understanding these calls and performing all the hard work for you, such as instantiating the required object, making the call, and returning a SOAP-formatted response to the client.
The .NET Framework makes it very easy for you to use of all this. As with ASP.NET, you are able to use the full array of C# and .NET techniques on the server, but (perhaps more importantly) the simple consumption of Web services can be achieved from any platform with HTTP access to the server. In other words, it is conceivable that Linux code could, for example, use ASP.NET Web services, or even Internet-enabled fridges. To quote a real-world example, in the past I have had great success combining ASP.NET Web services with Macromedia Flash to create data-enabled flash content.
In addition, Web services can be completely described using Web Service Description Language (WSDL), allowing the dynamic discovery of Web services at runtime. WSDL provides descriptions of all methods (along with the types required to call them) using XML with XML schemas. A wide variety of types are available to Web services, which range from simple primitive types to full DataSet objects; this makes it possible to marshal full in-memory databases to a client. This can result in a dramatic reduction in load on a database server.
In this chapter, you will do the following:
Look at the syntax of SOAP and WSDL, then move on to see how they are used by Web services.
Learn how to expose and consume Web services.
Work through a complete example building on the meeting room booking application from Chapter 32, “ASP.NET Pages,” and Chapter 33, “ASP.NET Development,” to illustrate the use of Web services.
Learn how to exchange data using SOAP Headers.
This chapter explores .NET Remoting. .NET Remoting can be used for accessing objects in another application domain (for... more
This chapter explores .NET Remoting. .NET Remoting can be used for accessing objects in another application domain (for example, on another server). .NET Remoting provides a faster format for communication between .NET applications on both the client and the server side.
In this chapter, you develop .NET Remoting objects, clients, and servers by using the HTTP, TCP, and IPC channels. First, you configure the client and server programmatically before you change the application to use configuration files instead, where only a few .NET Remoting methods are required. You also write small programs to use .NET Remoting asynchronously and for calling event handlers in the client application.
The .NET Remoting classes can be found in the namespace System.Runtime.Remoting and its subnamespaces. Many of these classes are in the core assembly mscorlib, and some needed only for cross-network communication are available in the assembly System.Runtime.Remoting.
System.Runtime.Remoting
mscorlib
The .NET Remoting topics covered in this chapter include the following:
An overview of .NET Remoting
Contexts, which are used to group objects with similar execution requirements
Implementing a simple remote object, client, and server
The .NET Remoting architecture
.NET Remoting configuration files
Hosting .NET Remoting objects in ASP.NET
Using Soapsuds to access the metadata of remote objects
Calling .NET Remoting methods asynchronously
Calling methods in the client with the help of events
Using the CallContext to automatically pass data to the server
CallContext
The chapter starts with a discussion of what .NET Remoting really is.
Enterprise Services is the name of the Microsoft application server technology that offers services for distributed... more
Enterprise Services is the name of the Microsoft application server technology that offers services for distributed solutions. Enterprise Services is based on the COM+ technology that has already been in use for many years. However, instead of wrapping .NET objects as COM objects to use these services, .NET offers extensions for .NET components to take direct advantage of these services. With .NET you get easy access to COM+ services for .NET components.
Enterprise Services also has a great integration story with WCF. You can use a tool to automatically create a WCF service front end to a serviced component, and you can invoke a WCF service from a COM+ client.
This chapter covers the following topics:
When to use Enterprise Services
What services you get with this technology
How to create a serviced component to use Enterprise Services
How to deploy COM+ applications
How to use transactions with Enterprise Services
How to create a WCF front-end to Enterprise Services
How to use Enterprise Services from a WCF Client
The WCF part of this chapter requires that you have .NET Framework 3.0 installed and the Visual Studio 2005 extensions for .NET 3.0.
This chapter is using the sample database Northwind, which you can download from the Microsoft downloads page: www.microsoft.com/downloads.
www.microsoft.com/downloads
System.Messaging is a namespace that includes classes for reading and writing messages with the Message Queuing facility... more
System.Messaging
System.Messaging is a namespace that includes classes for reading and writing messages with the Message Queuing facility of the Windows operating system. Messaging can be used in a disconnected scenario where the client and server needn’t be running at the same time.
This chapter looks at the following topics:
An overview of message queuing
Message Queuing architecture
Message queue administrative tools
Programming Message Queuing
Course order sample application
In the last four chapters, you learned several different ways of using services across the network:... more
In the last four chapters, you learned several different ways of using services across the network: Web services using ASP.NET, .NET Remoting, Message Queuing, and Enterprise Services with DCOM as the native communication protocol. Every technology has its advantages and disadvantages. Writing Web services using ASP.NET, the services can be used from different platforms, while .NET Remoting and DCOM are bound to the Microsoft platform. Comparing performance with these communication technologies, DCOM often is the fastest, followed by .NET Remoting and ASP.NET. The extension mechanisms are very different in that ASP.NET Web services can be extended by using SOAP headers, whereas .NET Remoting uses sinks. Extending DCOM is not supported. You get some overlapping features, but using them in tandem is often the best choice. For example, with many applications Web services are used as a front end to serviced components. All these technologies have different programming models, which requires many skills from the developer.
.NET Framework 3.0 offers a new communication technology that includes all the features from the predecessors and combines it into one programming model.
In particular, this chapter discusses the following topics:
A simple service and client
Contracts
Service implementation
Binding
Hosting
Clients
Duplex communication
Using Visual Studio 2005 with WCF, you must have the Visual Studio 2005 Extensions for .NET 3.0 installed.
This chapter provides an overview of Windows Workflow (known as WF throughout the rest of this chapter),... more
This chapter provides an overview of Windows Workflow (known as WF throughout the rest of this chapter), which provides a model where you can define and execute processes using a set of building blocks called activities. WF provides a Designer that, by, default is hosted within Visual Studio, which allows you to drag and drop activities from the toolbox onto the design surface to create a workflow template.
This template can then be executed by creating a WorkflowInstance and then running that instance. The code that executes a workflow is known as the WorkflowRuntime, and this object can also host a number of services that the running workflows can access. At any time, there may be several workflow instances executing, and the runtime deals with scheduling these instances, and saving and restoring state; it can also record the behavior of each workflow instance as it executes.
A workflow is constructed from a number of activities, and these activities are executed by the runtime. An activity might send an e-mail, update a row in a database, or execute a transaction on a back-end system. There are a number of inbuilt activities that can be used for general-purpose work, and you can also create your own custom activities and plug these into the workflow as necessary.
This chapter will discuss the different types of workflow that are available out of the box, and describe some of the inbuilt activities that are available with WF. You’ll see how to use some of the standard activity types, and also create two custom activities to show how to extend WF. This chapter begins with the canonical example that everyone uses when faced with a new technology Hello World and also describes what you need to get workflows running on your development machine.
Microsoft’s Active Directory is a directory service that provides a central, hierarchical store for user information,... more
Microsoft’s Active Directory is a directory service that provides a central, hierarchical store for user information, network resources, services, and so on. It is also possible to extend the information in this directory service in order to store custom data that is of interest for the enterprise. For example, Microsoft Exchange Server and Microsoft Dynamics use Active Directory intensively to store public folders and other items.
Before the release of Active Directory, Exchange Server used its own private store for its objects. It was necessary for a system administrator to configure two user IDs for a single person: a user account in the Windows NT domain to enable a logon and a user in Exchange Directory. This was necessary because of the additional information required by users (such as e-mail addresses, phone numbers, and so on), and the user information for the NT domain was not extensible to add the required information. Now the system administrator just has to configure a single user for a person in Active Directory; the information for a user object can be extended so that it fits the requirements of Exchange Server. You can also extend this information.
user
If you require the user information to be extended with a skills list, storing user information in the Active Directory makes this possible. Here it would easily be possible to track down a C# developer by searching for the required C# skill.
This chapter looks at how you can use the .NET Framework to access and manipulate the data in a directory service using classes from the System.DirectoryServices namespace.
This chapter uses Windows Server 2003 with Active Directory configured. You can also use Windows 2000 Server or other directory services with small modifications to the code presented here.
The architecture of Active Directory, including features and basic concepts
Some of the tools available for administration of Active Directory, and their benefit to programming
How to read and modify data in Active Directory
Searching for objects in Active Directory
Accessing a DSML Web service to search for objects
After discussing the architecture and how to program Active Directory, you create a Windows application where you can specify properties and a filter to search for user objects. Similar to other chapters, you can also download the code for the examples in this chapter from the Wrox Web site at www.wrox.com.
C# is the programming language designed for .NET. More than 50 languages exist for writing .NET applications,... more
C# is the programming language designed for .NET. More than 50 languages exist for writing .NET applications, for example, Eiffel, Smalltalk, COBOL, Haskell, Pizza, Pascal, Delphi, Oberon, Prolog, Ruby, and many more. Microsoft alone delivers the languages C#, Visual Basic, C++/CLI, J#, and JScript.NET.
Every language has advantages and disadvantages; some things can be done easily with one language but are complicated with another one. The classes from the .NET Framework are always the same, but the syntax of the language abstracts various features from the Framework. For example, the C# using statement makes it easy to use the objects implementing the IDisposable interface. Other languages need more code for the same functionality.
using
IDisposable
The most commonly used .NET languages from Microsoft are C# and Visual Basic. C# was newly designed for .NET with ideas from C++, Java, Pascal, and other languages. Visual Basic has its roots in Visual Basic 6 and was extended with object-oriented features for .NET.
C++/CLI is an extension to C++ that is an ECMA standard (ECMA 372). The big advantage of C++/CLI is the ability to mix native code with managed code. You can extend existing native C++ applications and add .NET functionality, and you can add .NET classes to native libraries so that they can be used from other .NET languages such as C#. It is also possible to write completely managed applications with C++/CLI.
This chapter shows you how to convert .NET applications from one language to another. If you see sample code with Visual Basic or C++/CLI, you can easily map this to C#, and the ot