Tuesday, June 15, 2010

OO Hello World - C#



So, here we go, the first of a series of hello world programs posts is here! And guess which language is the first one? C#. Why did I decide to show C# first? Well, basically because:

1. Is the most widely used programming language targeting the .NET CLR.
2. Is the one I use at work.
3. Is the one most of you already know, hence lets just show it and move on to the "less common" ones!

If you are new to C# you can jump to the second part of this post C# Info to get some extra info about the language, but don't forget to come back and see the code! :D

By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
using System;

class Greet
{
    string name;
    public Greet(string name)
    {
        this.name = name[0].ToString().ToUpper() + name.Substring(1, name.Length - 1);
    }
    public void Salute()
    {
        Console.WriteLine("Hello {0}!", name);
    }
}

// Greet the world!
class Program
{
    static void Main(string[] args)
    {
        Greet g = new Greet("world");
        g.Salute();
    }
}
Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
using System;
namespace GreetProgram
{
    internal class Greet
    {
        private string name;
        public Greet(string name)
        {
            this.name = name[0].ToString().ToUpper() + name.Substring(1, name.Length - 1);
        }
        public void Salute()
        {
            Console.WriteLine("Hello {0}!", this.name);            
        }
    }

    // Greet the world!
    internal class Program
    {                
        static void Main(string[] args)
        {
            Greet g = new Greet("world");
            g.Salute();            
        }        
    }
}

The Program Output:


 








C# Info:
“C# (pronounced "C sharp") is a programming language that is designed for building a variety of applications that run on the .NET Framework. C# is simple, powerful, type-safe, and object-oriented. The many innovations in C# enable rapid application development while retaining the expressiveness and elegance of C-style languages.” Taken from (http://msdn.microsoft.com/en-us/library/kx37x362.aspx)

Appeared:
2001
Current Version:
Developed by:
Microsoft
Creator:
Anders Hejlsberg
Influenced by:
C (Dennis Ritchie), C++ (Bjarne Stroustrup) and Java (James Gosling)
Predecessor Language

Predecessor Appeared

Predecessor Creator

Runtime Target:
CLR
Latest Framework Target:
4.0
Mono Target:
2.6
Allows Unmanaged Code:
Yes
Source Code Extension:
“.cs”
Keywords:
102
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
No
Standard:
ECMA-334, ISO/IEC 23270
Latest IDE Support:
Visual Studio 2010 Express
SharpDevelop 3.2/4.0 (beta)
MonoDevelop 2.2
Language Reference:
Extra Info:


No comments:

Post a Comment