C#: Intro and cross-platform development

This entry is part 1 of 71 in the series C#

C# has been around for more than a decade now.

Microsoft developed C# as an answer to Java.  Java is known for its write once, run anywhere paradigm.

With C#, and with the open source .Net framework, you can indeed run C# code in Mac and Linux.

To run C# program in other platforms such as Mac and Linux, you need the Mono software platform.

Mono is an open source, cross-platform implementation of C#.  The CLR or the Common Language Runtime, an interpreter, is binary compatible with Microsoft.NET

Mono is a software platform designed to allow developers to easily create cross-platform applications. It is an open source implementation of Microsoft’s .Net Framework based on the ECMA standards for C# and the Common Language Runtime.

You can for example, develop iOS apps using C# by using the Xamarin software development tools.

Posted in

C#: MS Visual Studio Express 2013 for Windows Desktop

This entry is part 2 of 71 in the series C#

Microsoft provides the best IDE for developing Windows applications using C#, C++ or Visual Basic.

As of now, I am still using Windows 7 and Microsoft Visual Studio Express 2013 for Windows Desktop is the free version for Windows 7.

We will be using this version for most of our series here.

You can download the version through this link.

The page in the link looks something like the screenshot below.

linkms

 

The Microsoft Framework 4.5 is also packaged into this download and so after installation, you will get the Express 2013 and the Framework.

Now, you have the complete tools to build your first Windows program.

 

Posted in

C#: Build the first console application

This entry is part 3 of 71 in the series C#

After you have downloaded the Visual Studio Express, launch it and you will see the screenshot similar to below.

Visual Studio

 

Now click on New Project….

You will see the screenshot below:

console

 

 

This IDE allows us to build C++, Visual Basic and C# program.

Select C# and click on Console Application.

You might also want to rename the application as ConsoleHelloApplication.

console_template

 

You could see that the default code for the console application has already been created for us.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 

C#: Print a line using WriteLine

This entry is part 4 of 71 in the series C#

To continue using the earlier template that we have generated, we add 2 lines to the code.

Console.WriteLine("Hello, World");
Console.ReadLine();

So the whole program will be as follow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

The WriteLine() will print out the Hello, World text.

Without the ReadLine(), the program will exit immediately by itself and so we add this line so that we can see the console output.

Click on the Start button and you will see an output similar to below:

console

 

 

C#: using System

This entry is part 5 of 71 in the series C#

In our earlier example, there was a couple of using statements created by the default C# template in MS Visual Studio.

Using in C# is called a directive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

As the program needs only to print out a simple Hello, World line, we can simplify the program by using only using System.

So the code below will also work:

using System;

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

The unused Usings can also be removed by right clicking on the page.  Look for Organized Usings -> Removed Unused Usings.

usings

System is a namespace in C# and actually, we have defined our program with a namespace of ConsoleHelloApplication.

With the line using System, we can use the Console class to write out the line under the System namespace.

The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.

If we do not include the System namespace, the code will be as below where System.Console is explicitly written out.

namespace ConsoleHelloApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello, World");
            System.Console.ReadLine();
        }
    }
}