Sams Teach Yourself C# Web Programming in 21 Daysby Phil Syme, Peter Aitken
Learn how to how to use C# for Internet programming with the hands-on techniques and clear explanations. This book discusses some C# features that allow rapid development of solutions such as garbage collection, simplified type declarations, and scalability support. The book explains key concepts in a simple and practical manner. Web Forms and Web Controls usher in
Learn how to how to use C# for Internet programming with the hands-on techniques and clear explanations. This book discusses some C# features that allow rapid development of solutions such as garbage collection, simplified type declarations, and scalability support. The book explains key concepts in a simple and practical manner. Web Forms and Web Controls usher in an elegant way to make dynamic Web pages. The book covers these topics with how-to code examples and projects. One of the newest developments in Internet programming is the use of XML and the SOAP communication protocol. .NET Web Services harness these two technologies, and is covered in later sections of the book.
Read an Excerpt
Day 1: Introducing Web Programming with .NETFor the past few months, you've been hearing all the hoopla and marketing buzz surrounding .NET. You've heard claims that .NET will totally change the way all programming is done, that .NET will revolutionize the Internet. Like many major new technologies, .NET is a significant improvement from the current state of the art and makes Web programming a much easier task than many current technologies that you can choose from.
Today's lesson will focus on the basics, including an explanation of the fundamental concepts that you will use throughout the rest of this book. By i'teading today's lesson, you will see how to create simple .NET Web programs using C#, and you will build a good foundation that you can use to understand the concepts of future lessons.
Today you will learn the following:
- What the .NET framework is
- Fundamental concepts in .NET programming, such as the common language runtime, assemblies, and namespaces
- How to configure your development environment so that you can develop .NET Web applications
- How to write a basic Web service and dynamic Web page
What Is the .NET Framework?Over the past few years, the importance of the Internet has grown enormously for almost all areas of computing and information processing. A direct result of this growth is that programmers are asked to create applications that take advantage of the Internet's capabilities, whether for order processing, broadband content delivery, online collaboration, or any of a host of uses. Unfortunately, the development tools previously available to developers, such as Visual C++ 6 and Visual Basic 6, had their origins in the pre-Internet world. Any Internet capabilities they had were afterthoughts, tacked on to meet developer demands. Some Internet programming technologies were actually quite impressive, and many terrific applications have been created with them. Even so, that Internet capabilities weren't built into these programming tools from the ground up led to unavoidable problems with development efficiency, bugs, and program maintenance.
Microsoft's answer to this dilemma was to start essentially from scratch, creating a new framework of developer tools that was designed from day 1 with fully integrated Internet support. The .NET framework is not only about Internet programming; it also provides for more traditional desktop application development. However, there's no doubt that the excitement about .NET is mostly about its Internet capabilities-and we can tell you from experience, they are really great!
.NET has two main parts:
- The .NET framework Software Development Kit, an extensive set of classes and interfaces, along with various supporting elements, that's designed to work together to meet just about any imaginable development need. The .NET framework must be installed on any system that will be used to run .NET applications or to develop them. The .NET SDK includes compilers for three languages: the venerable C++, a vastly improved Visual Basic, and-most important for this book-anew language called C# (C-sharp).
- The Visual Studio development environment. This sophisticated tool provides a comprehensive set of programmer tools, such as code editors, interface designers, and debuggers, that greatly simplifies the task of creating .NET applications. Strictly speaking, you don't need Visual Studio to develop .NET programs, but few programmers will want to do without its time- and effort-saving capabilities.
Fundamental ConceptsThis book teaches you how to create Internet applications using the .NET framework and the C# language. At least 90% of the material you need to learn is related to the .NET framework, not to C#. We therefore don't try to teach C# but assume you have at least a fundamental knowledge of the language. When needed, however, we do explain advanced C# language concepts when they are used in the examples.
To completely understand the code listings in the following chapters, you need to understand a few key terms, including the common language runtime (CLR), assemblies, namespaces, and event handlers. Today's lesson will give you enough information about each topic so that you have a good foundation to build your .NET knowledge.
The Common Language RuntimeThe common language runtime, or CLR, is the infrastructure that .NET uses to execute all your applications. The CLR includes a huge set of class libraries that you can use in your applications. Some interesting non-Internet-related classes include utilities that allow you to manipulate the Windows NT event log, Active Directory, cryptographic services, performance counters, COM+ services, object serialization, and Windows native services. If you aren't familiar with these terms, don't worry! This book will focus on the large amount of Internet programming-related classes, such as Web-based controls, Web Service classes, and Web security classes.
The CLR also takes care of compiling "intermediate language," or IL, code into native machine code. The basic process works as follows:
- The C# compiler converts your code into IL. In terms of functionality and language complexity, IL is somewhere between assembly language and C. IL is optimized for quick compilation into machine code. Normally, you won't deal with IL in any of your development efforts. You can think of IL as an intermediate product of the compilation process.
- Your Internet application is called by a user, or more precisely, by the Web server in response to a user's request.
- The CLR takes over and compiles the IL into machine code. This happens function by function. If a function is used, it's compiled into machine code. If not, it remains as IL. The odds of an error occurring during this process are extremely small, equivalent to a compiler crash.
The CLR contains other services, including support for running multiple .NET applications inside one process with a guarantee that they won't interfere with each other. This is a result of a concept called type safety. Any .NET language must guarantee type-safe code, which ensures that arrays and collections will never be accessed incorrectly, among other things. The C# compiler always enforces type safety when it compiles your code into IL.
AssembliesAll .NET code is compiled into assemblies. From the outside, an assembly looks exactly like a dynamic link library (DLL) or an executable program (EXE). The first few bytes inside each assembly contain machine code that invokes the common language runtime when the assembly is accessed by another program or by a user.
Inside the assembly is a mix of IL and machine code, depending on how much of the assembly has been compiled by the CLR. Assemblies also contain metadata. The metadata for a .NET program includes
- Information about every public class or type, including the name, what classes the class is derived from, and every interface that the class implements
- Information about every public method in each class, including the name and return value
- Information about every public parameter for every method, including each parameter's name and type
- Information about every public enumeration, including the names and values for each enumeration member
- Information about an assembly's specific version
By having such rich data in each assembly, the .NET framework doesn't need to use the Registry to run assemblies. If a .NET program needs to use another .NET assembly, they...
Meet the Author
PHIL SYME has been writing applications with C++ and Visual Basic since the release of Windows 3.1. He has helped create several enterprise scale projects developed for Fortune 500 companies that use Microsoft technologies. Phil has also co-authored articles published in IEEE symposiums. Phil lives in Baltimore, Maryland, and can be reached at email@example.com.
PETER AITKEN has been writing about computers and programming for more than 10 years, with some 30 books and more than 1.5 million copies in print as well as hundreds of magazine and trade publication articles. His book titles include Sams Teach Yourself Internet Programming with Visual Basic in 21 Days and Sams Teach Yourself C in 21 Days. A regular contributor to Office Pro magazine and the DevX Web site, Peter is the proprietor of PGA Consulting, providing custom application and Internet development to business, academia, and government since 1994. You can reach him at firstname.lastname@example.org.
and post it to your social network
Most Helpful Customer Reviews
See all customer reviews >