This is a "Hello World" type of of program for C#. It demonstrates the classes,arrays,strings and other C# elements.
The program implements a quick sort algorithm for string array.
It is a console based application.
Points of interest
The program skeleton
using System;
namespace StringSort
{
class StringSort
{
...
[STAThread]
static void Main(string[] args)
{
...
}
}
}
So, what do we have here: a good old Java style main class with the static Main method accepting command line arguments.
The program imports the System namespace (needed for console,arrays and other stuff).
This using directive is similar to #include in C but the difference is that you don't have to specify the path
to the namespace , the runtime will find it based on it's assembly lookup rules.
When you add you own namespace (say custom controls) you must add it to the list of referenced namespaces. [STAThread] attribute is for programs that work with COM/Interop
and denotes the appartment model (meaningless for other programs).
Static methods
Are the same as in C++ or Java.
Arrays
Are objects with quite a colleciton of methods like Length,Sort etc.
So in fact we could have implemented the whole sorting as calling strings.Sort();
Note the initialization in the declaration:
static string [] Strings = new string[] {"22","413","1","1","3","8"}; Last but not least - the brackets operator (duh!).
Strings, ints and other value types
The story with types is that there are 2 major categories:
Value types (int,float,structs) and reference types (classes,interfaces,delegates,arrays).
The value types (including structs) are allocated on the stack while reference oneces are in the heap and are grbadge collected.
Common control constructs
Lets get this out of the way quickly: if,for,do,while,switch are all here.
The swicth statement can accept strings and enums in addition to ints and breaks are mandatory.
There's a foreach construct for iterating through collections.
Console
Supports the primitive console i/o functionality.
Note in Console.Write("{0}\n",Strings[i]); that {0} is the first parameter here.
Thanks to reflection there is no need to specify the type.
Copyright (C) 2002, Ilia Tulchinsky
If you have any questions, bug reports, suggestions or just thoughts worth sharing contact me at
ilia@tulchinsky.com.