Introduction
There’s been considerable material on programming C# within the Visual Studio .NET environment, but not as much on C# as the first programming language based on the Common Language Infrastructure (CLI).
This online C# book provides an introduction to C#, the programming language. In addition, the book also takes a look at the CLI as well as the Common Language Runtime (CLR), using C# to demonstrate the various aspects of this standards-based development environment.
We’ll explore CLI/CLR in more depth in other chapters, but for now, to get you started, we’ll take a look at the basics necessary to get you up and running with C#.
The Basics
You can’t work with a programming language without learning the basics of creating an application in that language. Some languages, such the version of Basic found in Visual Basic, are a language and development environment tightly bound into one tool. Others, such as C++, can be bound to a development environment (ala Visual C++), or can be worked with using command line compilers and standard text editors, such as C++ within a Unix environment.
Fundamentally, you need to learn how to create the simplest form of an application in the language, compile it, and then run it. Once you have this information, you can then vary your program as you learn new and different aspects of the language.
This chapter provides an overview of how to create and compile a C# application using the minimum environment — in this case, using the .NET Framework SDK that’s downloadable, for free, from the Microsoft web site.
The Structure of a C# Application
Before installing the C# support environment, let’s take a look at a minimal C# application.
A C# application can consist of one or more modules — separately compiled functional units that don’t have a main entry point — libraries (comparable to COM components), and at least one application entry point. A C# file has an extension of .cs, and may, or may not, contain a Main application method.
A minimal C# application can have the following format:
using System; class someClass { public static void Main() { } }
As you can see, a minimal application doesn’t require a lot of code, requiring only the following:
- A reference to the System namespace
- A class
- A main function within the class, that’s called when the application’s run
Applications written in C# access external functionality through the use of namespaces — a container for classes, structs, types, and other useful programming goodies (namespaces are described in greater detail in a later chapter, “C# Scoping and Namespaces”).
C# files contain one or more class definitions. In addition, the files can also contain other C# elements such as interface definitions, delegates, and enumerations. A stand-alone application also has a Main function, called when the application is run. However, to create and run the application, you first have to install the environment.
The C# Environment
As with Java, C# is dependent on a runtime environment being present in order to support the language. Java depends on the Java Virtual Machine (VM) and C# is dependent on the Common Language Runtime (CLR), a set of services providing security, type safety, cross-language exception handling, garbage collection, and other features. An application that contains code meant to be managed by the CLR is known as managed code; code outside of the CLR control is know, appropriately enough, as unmanaged code.
There are five different CLR hosts that support managed code:
- ASP.NET (for server-side Web access)
- Internet Explorer (for code implemented within the boundaries of the browser)
- VBA (for script executed within the confines of Outlook or Office or other VBA enabled applications)
- Windows Form Designer
- Windows Shell EXE (for applications launched by a command line)
The last CLR runtime host is the one we’ll be using throughout this online book, primarily because this host allows us to focus on C# as a language, rather than C# as being part of ASP.NET or Web Services, and so on. Once you have a command of the language, then you can explore the other more complex uses of the language, such as those just mentioned.
In addition, you’ll be using the command line compiler when creating each of the test applications rather than creating the examples within Visual Studio. Again, this puts the focus on the language rather than the development environment. However, to run the application, you first have to compile it, which means you have to have both the infrastructure and compiler installed on your machine.