Write different "Hello World" programs using C #
1. A Beginners Hello WorldPublic Class HelloWorld
{
Public static void main ()
{
System.Console.writeline ("Hello World");
}
} 2. Slightly Improved Versionusing System;
Public class helloworld
{
Public static void main ()
{
Console.writeline ("Hello World");
}
} 3. Command line arguments concernsing system;
Public class helloworld
{
Public static void main (string [] args)
{
Console.writeline (args [0]);
}
} 4. from constructorusing system;
Public class helloworld
{
Public HelloWorld ()
{
Console.writeline ("Hello World");
}
Public static void main ()
{
HelloWorld HW = New HelloWorld ();
}
} 5. More Oousing System;
Public class helloworld
{
Public void helloworld ()
{
Console.writeline ("Hello World");
}
Public static void main ()
{
HelloWorld HW = New HelloWorld ();
Hw.helloworld ();
}
} 6. from another classesing 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. InheritanceAbstract 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 constructorusing 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 Handlingusing System;
Public class helloworld
{
Public static void main (string [] args)
{
Try
{
Console.writeline (args [0]);
}
Catch (IndexOfficRangeexception E)
{
Console.writeLine (E.TOString ());
}
}
} 10. CREANG A DLL AND USING IT IN AN ApplicationUsing System;
Namespace Hellolibrary
{
Public Class Hellometage
{
Public String Message
{
get
{
Return "Hello, World !!!";
}
}
}
}
// ------
Using system;
Using hellolibrary;
Namespace HelloApplication
{
Class HelloApp
{
Public static void main (string [] args)
{
HellOMessage M = New HellOMessage ();
}
}
11. USING PropertyUsing System;
Public class helloworld
{
Public String strhelloworld
{
get
{
Return "Hello World";
}
}
Public static void main ()
{
HelloWorld HW = New HelloWorld ();
Console.writeLine (cs.strhelloworld);
}
12. USING DELEGATESUSING SYSTEM;
Class HelloWorld
{
Static void writehelloWorld () {
Console.writeline ("HelloWorld");
}
Static void main () {
SimpledeLegate D = New SimpleDelegate (WriteHelloWorld);
d ();
}
} 13. 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 ();
}
14.4. USING Interfacesusing 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 ();
}
} 15. Dynamic Hello WorldUsing 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]);
// instantiarating 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);
}
}
16. Unsafe Hello WorldUsing System;
Public class helloworld
{
UNSAFE PUBLIC VOID WRITEHELLOWORLD (CHAR [] Chrarray)
{
Fixed (Char * Parr = Chrarray)
{
CHAR * PCH = PARR;
For (int i = 0; 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); } } 17. USING InteropServicesUsing System; Using system.Runtime.InteropServices; Class class1 { [DLLIMPORT ("kernel32")]] Private static extern Int Beep; Static void main (string [] args) { Console.writeline ("Hello World"); BEEP (1000, 2000); } }