C# basics

Hello World!

Programming languages often have their own version of the Hello World! program that serves as an introduction to the basics of the language.

By modifying C#’s version slightly, the code below will not only look familiar to those who record and edit macros, it will also run in Tekla Structures.

using System.Windows.Forms;

namespace Tekla.Technology.Akit.UserScript
{
    // A version of the classic "Hello, World! program
    static class Script
    {
        public static void Run(Tekla.Technology.Akit.IScript RunMe)
        {
            MessageBox.Show("Hello, World!");
        }
    }
}

Now let’s examine the code line by line.

The Using Keyword

By using the using keyword, the first line imports all types within the System.Windows.Forms namespace. For example, the MessageBox class used later in the source code is defined in the System.Windows.Forms namespace, meaning it can be used without writing the full name of the type. A program can include multiple lines of using statements, for example:

using System;
using System.Windows.Forms;

Namespaces

Namespaces are heavily used in C# programming in two ways. First, .NET uses namespaces to organize its many classes, and secondly, declaring your own namespaces can help you control the scope of class and method names in larger programming projects. Use the namespace keyword to declare a namespace, as in the following example:

namespace Tekla.Technology.Akit.UserScript
{
    ...
}

Comments

Comments are used for explaining code, which the compilers ignore when compiling. Single-line comments are indicated by the // symbol.

// A version of the classic "Hello, World!" program

Whereas multi-line comments start with /* and terminate with the */ as shown below:

/* This program demonstrates
   The basic syntax of C# programming 
   Language */

Classes

Everything that follows between the pair of curly braces describes that class and demarcate the boundaries of a code block. Curly braces are used to group statements, which are commonly grouped into methods (functions), methods into classes, and classes into namespaces. In example below, they are marking the start and end of the Script class.

static class Script
{
    ...
}

Methods

The line below declares the class member method called Run, where the program begins execution.

public static void Run(Tekla.Technology.Akit.IScript RunMe)

The line below displays the message box. The program calls the MessageBox method Show, which displays a message box with the contents “Hello, World!”.

MessageBox.Show("Hello, World!");