GNU/Linux Programmer's Solver - C#

Hello World program in C#. Basic structure, comments and string literals
PROGRAMMING C# HELLO WORLD
// This is a comment
/* This is a comment along
   multiple lines */

class Program {
    static void Main(string[] args) {
    System.Console.WriteLine ("Hello World!"); // This is a comment
    System.Console.WriteLine (@"This is a string
using multiple text lines
in C#" );
    }
}

C# programs are like Java programs that have had the keywords’ names changed. Just like Java compiles to an intermediate code that the CLR (Common Language Runtime) can handle. CLR is equivalent to the Java virtual machine. As with other languages that compile to intermediate code, it is possible to generate native code, although in that case, we lose the possibility of cross-platform execution.

C# programs use the .cs extension. The compiled binaries usually have a .exe extension, which was the extension of programs in MS-DOS and Windows, the main operating systems of the company that developed this language, Microsoft.

DID YOU KNOW

Originally, the language was called COOL (C-Like Object Oriented Language), but Microsoft decided to change the name when it officially presented it.

As we can see, the comments are defined the same as in C and Java, and the program structure is identical to the Java version, but changing System.out to System.Console and String to string. C# uses the same solution as Java to decide whether to add a carriage return to the line we print with Write, but instead of using the ln prefix like almost all other languages that follow this convention, C# adds the complete word Line.

C# allows us to define multi-line strings by prepending the @ character to the string, as we can see in the example.

C# and the entire .Net platform are strongly integrated into Windows operating systems. On other operating systems, we can use the open-source project mono to compile and run our C# programs. In Debian derivatives, we can install it with the following command.

$ sudo apt install mono-xsp

Once the environment is installed, we can compile and execute our program as follows:

$ mono-csc hello.cs
$ ./hello.exe

The origin of C# is intimately related to the development of the .Net framework. The first version of the class libraries of this environment were developed with a compiler called Simple Managed C (SMC) since the class libraries were implemented in what MS called managed code… in other words… bytecode. In January 1999, Anders Hejlsberg led a group tasked with designing a new language for the .Net platform. That language was called COOL (C-Like Object Oriented Language or C-Like Object Oriented Language). The name was eventually changed to C# when the .Net platform was officially announced in July 2000.

From its inception, the language was surrounded by much controversy due to its similarity to Java. The truth is that the first version of C# at that time was like Java, but with some keywords changed. Since then, both Java and C# have evolved and the difference between them is much clearer today.

Summary

  • Interpreted language compilable to bytecode
  • Extension: cs
  • Line terminator: ;
  • Groups instructions with: { instructions }
  • Multi-line comments: /* comment */
  • Single-line comments: //
  • String delimiter: "string"
  • You can print text with System.Console.WriteLine (Adds carriage return)
  • Supports multi-line strings: @"string multiline"
  • Does not Support HERE-DOCS
  • Entry Point: Main function

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - TCL

Hello World program in TCL. Basic structure, comments and string literals

Read

PROGRAMMING TCL HELLO WORLD

GNU/Linux Programmer's Solver - Go

Hello World program in Go. Basic structure, comments and string literals

Read

PROGRAMMING GO HELLO WORLD

Return to Home Page