Introduction
I've attempted to write the traditional 'Hello World' in different styles. This explores the different possibilities of addressing a problem - 'Hello World' with different features of C# language and .NET framework.
1. A Beginner Hello World
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("HELLO WORLD");
}
}
2. Slightly improved version
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("HELLO WORLD");
}
}
3. Command Line Arguments
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine(args[0]);
}
}
4. From Constructor
using System;
public class HelloWorld
{
public HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
}
}
5. More OO
using System;
public class HelloWorld
{
public void HelloWorld()
{
Console.WriteLine("HELLO WORLD");
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.HelloWorld();
}
}
6. From another class
using System;
public class HelloWorld
{
public static void Main()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass();
hwh.WriteHelloWorld();
}
}
public class HelloWorldHelperClass
{
public void WriteHelloWorld()
{
Console.WriteLine("Hello World");
}
}
7. Inheritance
using System;
abstract class HelloWorldBase
{
public abstract void WriteHelloWorld();
}
class HelloWorld: HelloWorldBase
{
public override void WriteHelloWorld()
{
Console.WriteLine("Hello World");
}
}
class HelloWorldImp
{
static void Main()
{
HelloWorldBase hwb = new HelloWorld();
hwb.WriteHelloWorld();
}
}
8. Static Constructor
using System;
public class HelloWorld
{
private static string strHelloWorld;
static HelloWorld()
{
strHelloWorld = "Hello World";
}
void WriteHelloWorld()
{
Console.WriteLine(strHelloWorld);
}
public static void Main()
{
HelloWorld hw = new HelloWorld();
hw.WriteHelloWorld();
}
}
9. Exception Handling
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
try
{
Console.WriteLine(args[0]);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e.ToString());
}
}
}
10. Creating a DLL and using it in an application
Kumaresh RajalingamPosted Feb 14, 2016, 12:52 AM
Good Thinking
muthu kumareditedPosted Jul 31, 2010, 6:46 AMEdited Jul 31, 2010, 6:49 AM
abstract class HelloWorldBase { public abstract void writeHelloWorld(); } class HelloWorld : HelloWorldBase { public override void writeHelloWorld() { Console.WriteLine("Hello World"); } } class HelloWorldImp { static void Main() { HelloWorldBase hwb = HelloWorld; HelloWorldBase.writeHelloWorld(); } } How can you create object(instantiation) for abstract classes. This artcile has been published in 2004 but no comments are available.Hasnt anybody viewed this article(I'm Puzzled). Explanation of different ways was indeed a good task.