Use C # to write 17 kinds of Hello World programs

xiaoxiao2021-03-06  13

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 o

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 Constructionor

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 (IndexOfficRangeexception E)

{

Console.writeline (E.TOString ());

}

}

}

10. CREANG A DLL AND USING IT IN AEN Application

Using 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 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]);

// 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 World

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

}

}

转载请注明原文地址:https://www.9cbs.com/read-50480.html

New Post(0)