Contents
Introduction
C# ( pronounced like see sharp ) is a new great programming language
from Microsoft. It is a kind like Java but several common Java flaws were fixed
(for example lack of enums, lack of the ability to override operators, lack of
function pointers). C# sources are compiled into the intermediate language IL
and in the runtime the C# virtual machine compiles IL into the native code. Just
like in other modern programming languages, in C# you do not write methods to
destroy objects. Instead, the environment use so called garbage collector
that inspects all objects and frees the memory for unused ones.
The .NET Framework is everything you need to build your own C#
applications. The Framework can be downloaded from Microsoft HomePage and it is
approximately 20MB of data. There's also a richer version that is called .NET
Framework SDK (about 120MB) with a tons of examples and full docs. Both
versions are FREE to use.
After succesful instalation, .NET Framework places itself into the special
directory in the SYSTEM subdirectory and then you are able to invoke csc.exe
that is C# compiler.
The exists an open-source project that heads to moving C# to Linux. More info
at the project homepage: go-mono [^].
The simplest C# program could be like this one:
using System;
namespace Project1
{
public class Ex1
{
public static void Main(string[] args)
{
Console.WriteLine("The first C# program!");
}
}
}
Just type the source into the file named ex1.cs and invoke the
compiler csc.exe ex1.cs.
The compiler answer is:
D:\EXAMPLE1>C:\WINNT\Microsoft.NET\Framework\v1.0.3705\csc.exe ex1.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
D:\_
and the output file is 3072 bytes long.
C# object model is really close to SmallTalk model, where every object
inherits from the most general type called "object". The type "object"
implements a few methods and thus all objects inherit those methods. One of them
is called "ToString()" and is really useful. It is used to convert an object to
its string representation. Let's see a trivial example:
using System;
namespace Project1
{
public class A
{
public int field;
public A() {}
}
public class Ex1
{
public static void Main(string[] args)
{
A a = new A();
a.field = 7;
Console.WriteLine( a.ToString() );
}
}
}
The output of the program is:
Project1.A
This is just because if you do not override the ToString() method, it always
returns the name of the type. Let's then fix our example:
(...)
public class A
{
public int field;
public A() {}
public override string ToString()
{
return "I am an object of type A and I hold the value " +
field.ToString();
}
}
As expected, the output is:
I am an object of type A and I hold the value 7
Why we do not have to override the ToString() method for type int when we call
field.ToString()? Simple, it has already been overridden.
So, how to convert the string value to int value? This is also simple. The
type "int" has a static method "Parse()" that you can use.
using System;
namespace Project1
{
public class Ex1
{
public static void Main(string[] args)
{
string a = "456";
int b = int.Parse(a);
Console.WriteLine( b.ToString() );
}
}
}
The same idea can be used to parse string into the floating point types.
Top 