![]() |
C#.Net Tutorial #1
Once in a while, something happens in the computer industry happens that simply cannot be ignored and when it is created by the biggest software company in the world, everyone has to sit up and take notice. The .NET (pronounced DOT NET) initiative grew out of work being done in the Visual J++ department which had created a set of libraries called WFC (Windows Foundation Classes) allowing Java applications to be compiled for the Windows (and only the Windows) platform. This got up the noses of the creators of Java and so it fizzled out. Then there came C# (pronounced C Sharp) which is a hybrid language based on VB, Java, C and C++ and is a wholly Microsoft created product built to run on the .NET platform created from the WFC. Why should you change? Because it is the language Microsoft itself uses and may well be the language that Windows itself is written in the future (possibly the one after Vista) when the .NET framework becomes a central part of the Windows OS rather than just an addon. Why not stick with VB6? Because there is no guarantee that the new version of Windows (whatever it happens to be called) will run it properly. So what is all the fuss about? Our first C# program:-
using System;
class hello
{
// This is the usual Hello World! program
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
Doesn't look to bad does it? Compare this with a Java program that does the same thing:-
public class TestMain
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
It should be noted that output is to the text console and doesn't produce a GUI window (thats for later). So now you have to compile it and for that you will need a compiler. Now, you're probably getting a bit worried that this is going to cost you money. I mean, surely Microsoft wouldn't give it away for free! Well, actually it seems that they do. The bit you have to pay for is the IDE (Integrated Development Environment) which makes life easier but actually isn't required to write modest sized programs. So where is it? If you have installed version 1.1 of the dotnet framework from your Windows XP CD then you've already got all the compilers you need to follow these tutorials (Win98 users can download the dotnetfx file and install it to run the .NET framework on Win98 and hence use these tutorials. You will also need MDAC 2.7 which is available as a download from the same page but you may already have it if you have been using VB6. You CANNOT run Visual Studio.Net on Win98). You now open a Command Console (in the WinXP Accessories menu) and type:-
path=c:\windows\microsoft.net\framework\v1.1.4322\
dir *.exe
Your version number may be a bit different but the above is correct for my machine running SP2. The dir command will list all the files and amongst it all you will see the following files:-
jsc.exe
vjc.exe
csc.exe
vbc.exe
This opens up some very interesting possibilities. Not only is there a C# compiler (csc.exe) but a Visual J# vjc.exe (the successor to J++), a Visual Basic Compiler (vbc.exe) and finally a Javascript compiler (jsc.exe). Does this mean I can write .NET applications in Javascript? Apparently so although I haven't had much chance to play with it (I can feel another set of tutorials coming on but not yet). And it hasn't cost you a penny! So how to use this compiler? Create a batch file (called cc.bat) and fill it with the following commands:-
cd\
cd csc
path=c:\windows\microsoft.net\framework\v1.1.4322\
csc %1.cs
Then create a folder called csc and put the batch file in it. Create another file called hello.cs and put the above C# code in it. Save and run from the command line:-
csc hello
and press the return key. It compiles and deposits a hello.exe in the csc folder. Now just run the hello.exe by typing:-
hello
and the famous words will be displayed. Congratulations, you've created your very first C# program! It isn't very exciting I admit but its a start.
To summarise the steps.
Now we'll go through the code a line at a time. Bear with me, it is relevent and will help you understand what is to follow when we get to the more interesting stuff.
using System;
This is telling the compiler to include the system library which it needs to compile a console program. Note that there is a semicolon on the end - this tells the compiler that this is the end of a statement.
class hello
{
This starts a class which is a form of subroutine if you are familar with VB or function in C although there is more to it than that but will suffice as an explanation for the moment. It then uses an open bracket much like C and Java.
// This is the usual Hello World! program
This is a comment again with a similar syntax to C++ and Java. It doesn't need to have a semi-colon or anything on the end as the compiler will read right up till the end of the line automatically.
public static void Main()
{
The familiar Main function beloved of C programmers makes it to C# and is the beginning of the program. Main() doesn't need a terminating semicolon because there is an opening bracket showing that a block of code is starting. Now we actually do something:-
Console.WriteLine("Hello World!");
}
}
This writes to the console window and the brackets close the main program and the class. Brackets don't have terminating semicolons but the writeline statement does. And that's all there is to it. Well, alright there is much work to be done to get a GUI window displaying but I'll take you gently through the syntax of the language using console programs and work our way to the GUI stuff.