17 Kinds of Hello world!

Source: Internet
Author: User
Tags abstract command line constructor exception handling


Writing different "Hello World" programs using C #

1. A Beginners Hello World
public class HelloWorld
{
public static void Main ()
{
System.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
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 ();
}
}
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 ());
}
}
}
Creating a DLL and using it in the application
Using System;

Namespace Hellolibrary
{
public class HelloMessage
{
public string Message
{
Get
{
Return "Hello, the world!!!";
}
}
}
}

//------

Using System;
Using Hellolibrary;

Namespace Helloapplication
{
Class HelloApp
{

public static void Main (string[] args)
{
HelloMessage m = new HelloMessage ();

}
}
}
Using Property
Using System;
public class HelloWorld
{
public string Strhelloworld
{
Get
{
Return to "Hello World";
}
}

public static void Main ()
{
HelloWorld HW = new HelloWorld ();
Console.WriteLine (Cs.strhelloworld);
}
}
Using delegates
Using System;
Class HelloWorld
{
static void Writehelloworld () {
Console.WriteLine ("HelloWorld");
}
static void Main () {
SimpleDelegate d = new SimpleDelegate (Writehelloworld);
D ();
}
}
Using Attributes
#define Debugging

Using System;
Using System.Diagnostics;

public class Helloworld:attribute
{
[Conditional ("debugging")]

public void Writehelloworld ()
{
Console.WriteLine ("Hello World");
}

public static void Main ()
{
HelloWorld HW = new HelloWorld ();

Hw.writehelloworld ();
}
}
Using interfaces
Using System;

Interface Ihelloworld
{
void Writehelloworld ();
}

public class Helloworld:ihelloworld
{
public void Writehelloworld ()
{
Console.WriteLine ("Hello World");
}

public static void Main ()
{
HelloWorld HW = new HelloWorld ();

Hw.writehelloworld ();
}
}
Dynamic Hello World
Using System;
Using System.Reflection;

Namespace Helloworldns
{

public class HelloWorld
{
public string Writehelloworld ()
{
return "HelloWorld";
}

public static void Main (string[] args)
{
Type HW = Type.GetType (args[0]);

Instantiating a class dynamically

object[] Nctorparams = new object[] {};
Object nobj = Activator.CreateInstance (HW,
Nctorparams);

Invoking a method

object[] Nmthdparams = new object[] {};
String Strhelloworld = (string) hw. InvokeMember (
"Writehelloworld", Bindingflags.default |
BindingFlags.InvokeMethod, NULL,
Nobj, Nmthdparams);

Console.WriteLine (Strhelloworld);
}
}
}
Unsafe Hello World
Using System;

public class HelloWorld
{
unsafe public void Writehelloworld (char[] chrarray)
{
Fixed (char *parr = chrarray)
{
char *pch = Parr;
for (int i=0; i<chrarray.length; i++)
Console.Write (* (pch+i));
}
}

public static void Main ()
{
HelloWorld HW = new HelloWorld ();
char[] Chrhelloworld = new char[]
{' H ', ' e ', ' l ', ' l ', ' o ', ', ' W ', ' o ', ' r ', ' L ', ' d '};
Hw.writehelloworld (Chrhelloworld);
}
}
-Using InteropServices
Using System;
Using System.Runtime.InteropServices;

Class Class1
{
[DllImport ("kernel32")]
private static extern int Beep (int dwfreq, int dwduration);

static void Main (string[] args)
{
Console.WriteLine ("Hello World");
Beep (1000, 2000);
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.