Coding with csharp

Basic Concepts about C#
C# is a managed, static strong-typed language with a C like syntax and object-oriented features similar to Java. All in all one can say that C# is very close to Java to start with. There are some really great features in the current version of C#, but in this first tutorial we exclude them.

Managed code in C#
The managed means two things: First of all we do not need to care about the memory anymore. This means that people coming from C or C++ can stop worrying about freeing the memory allocated for their objects. We only create objects and do something with them. Once we stopped using them, a smart program called the Garbage Collector (GC) will take care of them. The next figure shows how the Garbage Collector works approximately.
We do not have much control about the point in time when this is happening. Additionally the GC will do some memory optimization, however, this is usually not done directly after freeing the memory.

Static strong typed language
The static strong-typed language means that the C# compiler needs to know the exact type of every variable and that the type-system must be coherent. There is no cast to a void datatype, which lets us basically do anything, however, we have a data type called Object on top, which might result in similar problems.

Following are the list of data types:

  • bool (1 byte) used for logical expressions (true, false)
  • char (2 bytes) used for a single (unicode) character
  • short (2 bytes) used for storing short integers
  • int (4 bytes) used for computations with integers
  • long (8 bytes) used for computations with long integers
  • float (4 bytes) used for computations with single precision floating point numbers
  • double (8 bytes) used for computations with double precision floating point numbers
  • decimal (16 bytes) used for computations with fixed precision floating point numbers

Reference and value types

A very important concept for understanding C# is the difference between reference and value types. Once we use a class, we are using a reference type. On the other side, any object that is a struct will be handled as a value type. The important difference is that reference types will be passed by reference, i.e., we will only pass a copy of the position of the object, and not a copy of the object itself. Any modification on the object will result in a modification on the original object.

The whole concept is quite similar to the concept of pointers in C. The only difference is that we do not actually have access to the address and we do not have to reference the variable to get access to the values behind it. One could say that C# handles some of the things automatically that we would have to write by hand in C/C++.


using System;
class Program
{
static void Main()
{
//A string is a class, which can be instantiated like this
string s = "Hi there";
Console.WriteLine(s);//Hi there
ChangeString(s);
Console.WriteLine(s);//Hi there
ChangeString(ref s);
Console.WriteLine(s);//s is now a null reference
}
static void ChangeString(string str)
{
str = null;
}
static void ChangeString(ref string str)
{
str = null;
}
}

Please find below a very nice example for ref and out :


using System;
class Program
{
static void Main()
{
int val = 0;

Example1(val);
Console.WriteLine(val); // Still 0.

Example2(ref val);
Console.WriteLine(val); // Now 2.

Example3(out val);
Console.WriteLine(val); // Now 3.
}
static void Example1(int value)
{
value = 1;
}
static void Example2(ref int value)
{
value = 2;
}

static void Example3(out int value)
{
value = 3;
}
}

Output
0
2
3

Syntax Rules for C#
Now that we introduced the basic concepts behind C#, as well as elementary data types and available operators, we just need one more thing before we can actually go ahead and write actual C# programs. We need to know how to control the program flow, i.e., how to introduce conditions and loops.

This is pretty much the same as in C, as we are dealing with a C-style syntax. That being said we have to follow these rules:

  • Conditions can be introduced by using if or switch.
  • A loop is possible by using for, while, do-while.
  • A loop will be stopped by using the break keyword (will stop the most inner loop).
  • A loop can skip the rest and return to the condition with the continue keyword.
  • C# also has an iterator loop called foreach.
  • Another possibility is the infamous goto statement – we will not discuss this.
  • There are other ways of controlling the program flow, but we will introduce those later.