Use C # To write different "Hello World" Program
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 ());
}
}
}
10. Creating a DLL and using it in an application
Using system;
Namespace hellolibrary
{
Public class hellomessage
{
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 property
Using 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 delegates
Using 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. 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 ();
}
}
15. 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 );
}
}
}
16. 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 );
}
}
17. 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,200 0 );
}
}