Description:  This document is a tutorial in a series of tutorials for programmers learning about the .NET Framework development environment.  What you will learn is what C# is, how it fits into the .NET Framework.  You will also learn how to write a simple C# program as well as gain a taste for the possibilities of this new platform.

Requirements:  You should be familiar with at least one programming language, such as C++, Pascal, PERL, Java or Visual Basic.  You should have some comfort with object oriented concepts such as instantiating and using objects.  You should be comfortable with general computer science concepts.  To do the exercises and run the examples you need a PC running Windows with the .NET Framework installed.

Table of Contents

Table of Contents. 1

Figures and Exercises. 3

1      Introducing C#. 4

1.1       HelloWorld a-la C#. 4

1.2       C# and the .NET Framework Class Library (FCL) 6

1.3       Minimal Requirements to Use C#. 6

2      C# Applications. 7

2.1       Creating Console Assemblies. 8

2.2       Creating GUI Assemblies. 10

2.3       Creating Code Library Assemblies. 12

3      Visual Studio.NET. 16

3.1       Creating a Project 16

3.2       Working with Projects. 18

4      Language Concepts. 18

4.1       Syntax Ground Rules. 19

4.2       Primitive Types. 19

4.3       Expressions and Operators. 21

4.4       Methods. 22

4.5       Reference Types and Value Types. 23

4.6       Arrays of Data. 24

4.7       Conditional Statements. 25

4.8       Loops. 26

4.9       Error Handling. 26

5      Simple Object Oriented Programming. 27

5.1       Using Types. 27

5.2       Typecasting. 29

5.3       Extending Types. 30

6      C# in Action. 31

6.1       C#Pad.exe – Sample Application. 31


Figures and Exercises

Figure 1‑1  HelloGUI.cs. 5

Figure 2‑1  Rabbits.cs. 8

Figure 2‑2  Tribbles.cs. 11

Figure 2‑3  FibObj.cs. 13

Figure 2‑4  FibTest.cs. 14

Figure 3‑1  New Project in VS.NET. 17

Figure 3‑2  Solution Explorer 18

Figure 4‑1  Primitive Types. 20

Figure 4‑2  Primitive Code Snippit (Excerpted from Language.cs) 21

Figure 4‑3  C# Operators. 22

Figure 4‑4  Methods Code Snippit (Excerpted From Language.cs) 23

Figure 4‑5  Array Declaration Code Snippit (Excerpted from Language.cs) 24

Figure 4‑6  if Code Snippit (Excerpted from Language.cs) 25

Figure 4‑7  switch Code Snippit (Excerpted from Language.cs) 25

Figure 4‑8  Looping Code Snippit (Excerpted from Language.cs) 26

Figure 4‑9  Exception Code Snippit (Excerpted from Language.cs) 27

Figure 5‑1  FileCopy.cs. 28

Exercise 1‑1  Building and Running HelloGUI.cs. 7

Exercise 2‑1  Build and Run a Console Application. 9

Exercise 2‑2  Build and Run a GUI Application. 12

Exercise 2‑3  Build a Code Library. 15

Exercise 2‑4  Build an App to Test a Code Library. 15

 


1      Introducing C#

C# (or C-Sharp) is a new programming language.  C# is used to write software that runs on the .NET Framework.  Although C# is not the only language that you can use to target the .NET Framework, C# is one of the most popular because of its simplified C-based syntax.

Usually, an introductory text to a computer language begins by telling you what you can do with a language as well as where the software you build with the language can run.  I would like to address these points right away with C#, since they can be a little confusing.

In brief, C# (unlike C++, PERL, COBOL, Pascal, etc.) is a language that targets one and only one platform.  This platform is the .NET Framework.  However, the .NET Framework itself is a computing platform that is designed to be hosted by any operating system.  At the time of this writing the .NET Framework runs on Windows operating systems, and I know of two other major OSs for which a version of the .NET Framework is being developed.  So you can see that although C# is designed to target only the Framework, the Framework itself is flexible enough to run your C# programs on many types of systems.

The relationship between C# and the .NET Framework is somewhat unique.  In a way it is similar to the relationship between Java and the Java Virtual Machine, however there are several major differences.  First, C# is not the only language that can be used to write .NET Framework applications (called Managed Applications).  Second, .NET or managed, applications run in native machine-language and are not interpreted.  Third, C# or managed applications do not run in a sandbox.

What you should take away from this introduction, as a programmer learning C#, is that C# is a programming language (that you will find simple to master); however, much of what you can do with C# is really more a part of the .NET Framework itself.  C# is a language with syntax so simple that most programmers will be comfortable with it in no time, but the .NET Framework is a platform so powerful that developers will continue to learn the ins and outs of it for years.