Introduction to C Sharp

Electronics people often need to write short PC programs, for instance to evaluate test and measurement data. Windows PC users will find the .NET Framework particularly handy for this task. It works with various languages, eliminates most of the burden of programming and doesn’t cost a penny – not even the development environment. Here’s a basic introduction to the C# language and its many advantages.

Today’s electronics enthusiasts increasingly need to be software developers as well. An obvious situation is programming microcontrollers. Frequently the need arises for a short program written from scratch, perhaps for outputting control data to an interface card or for displaying measurement data on screen. Alternatively, an existing program may need to be adapted or expanded.

Software development is to a large extent bound up with the target platform and the operating system. In this series of articles we shall be looking at the development of programs running under Microsoft Windows XP and Vista. The underlying foundation is a runtime library that goes by the name of .NET Framework (pronounced ‘dotnet’), which is supplied with Vista and is offered gratis as a retrofi t for XP. Anyone using older versions of Windows need not go away empty-handed, as the support pages of Microsoft may offer a solution. Finally, it’s well worth mentioning that an Open Source version of the .NET Framework has appeared in the meantime and this so-called ‘Mono’ framework even runs on Linux computers!

There’s one basic rule in .NET: everything’s an Object! That’s the fundamental difference from classic programming, in which data is separated from functions (which use and deploy data). By comparison, in object-oriented programming data and functions form self-contained units-called Objects. All Objects used in a program are arranged in Classes that specify which data can be included in an Object and which functions can be deployed in it. Let’s take a simple example: a person possesses the Characteristics ‘size’ (a floating point number), ‘place of birth’ (a character string) and ‘age’ (an integer). We can also attribute to this person a range of activities that are called Methods here. These, for example, could include the Methods ‘Sleeping’, ‘Eating’ and ‘Learning’. In our program we could defi ne a Class by the name of ‘Person’.

A concrete Object arises when we form an Instance from a Class (by means of the keyword ‘new’). Using the Class ‘Person’ we can create as many Instances as we wish. All these Objects then make use of the above-mentioned Characteristics and Methods.

The introduction of the .NET Framework brought a significant change to software development for Windows. Greatly simplified, we’re talking about a powerful library of Base Classes, offering a wide variety of ‘oven ready’ approaches for setting problems and getting answers, saving programmers a mountain of work. In the process the .NET Framework has averted a lot of long-winded discussion about the ‘best’ programming language. The choice of language has become a secondary consideration, as the Base Classes provide this themselves, and a common type system exists. Regardless of the language of the source code, a program is always translated into the same ‘intermediate’ code, which is subsequently executed by the .NET run-time environment (more correctly, it is recompiled when the program starts). The use of this ‘Common Intermediate Language’ brings with it security advantages and also enables programming that is substantially independent of the operating system. Porting across involves no more than changing the (lean) runtime environment.

Some of the language elements in C# may be familiar from other programming languages. All syntax questions can be resolved using the online help.

1. C# treats upper and lower case letters as different, so the variable ‘total’ would be handled differently from the variable ‘Total’.

2. Single-line comments are started with //, whereas /*…*/ indicates a comment with several lines of text.

3. Important fundamental data types include: byte, int, float, double, decimal and string.

4. Variables are declared and initialised by prefixing the data type:

int Total = 5;
string text = "one character string";

5. Basic operators: +, -, *, /

6. The flowing loops exist: for, while, do and foreach. The last named iterates for example Objects assembled in listings (Collections).

7. Selection instructions are: If, else if, else and switch (…) case.

8. User-defined data types are declared with the keyword ‘struct’ (user-defi ned Classes are introduced with Class):

struct Dataset
{
int Alter;
string Name;
}

Variants of C from Microsoft For the.NET Framework there are languages like Visual Basic for .NET (from Microsoft) or Delphi for .NET (from Embarcadero Technologies).

These two languages mentioned have been merely adapted for .NET (VB.NET is certainly not completely compatible with classic VB, although VB6 source code is fairly simple to adapt). On the other hand C# (pronounced ‘C Sharp’) has been developed by Microsoft from scratch specifically for the task. The complexity of the C and C++ languages, with their heavy learning curves, fi nds no place in C#. For example, pointer arithmetic (relished by few programmers) has fallen by the wayside. On the other hand, plenty of effective and user-friendly concepts from other languages have been incorporated. And another thing in favour of this variant of C: there exists an extremely compact .NET Framework for programming microcontrollers, which even includes a gratis compiler, in fact for C#!