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;
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;
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);
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.