17 C # Hello World programs I understand

Source: Internet
Author: User

Pure syntax Learning

Use C # To write 17 Hello World programs
--------------------------------------------------------------------------------
 
1. Beginner A Beginners Hello World
Public class HelloWorld
{
Public static void Main ()
{
System. Console. WriteLine ("hello world ");
}
}

2. Slightly improved versionSlightly increased
Using System; (that's it? Will the namespace be used ?)

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) // parameters are passed.
{
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 (); // will the class be used? Constructor?
}
}

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 (); // further object-oriented? Will it be used?
}
}

6. From another class
Using System;
Public class HelloWorld
{
Public static void Main ()
{
HelloWorldHelperClass hwh = new HelloWorldHelperClass (); // calls other classes in the class?
Hwh. writeHelloWorld ();
}
}

Public class HelloWorldHelperClass
{
Public void writeHelloWorld ()
{
Console. WriteLine ("Hello World ");
}
}

7. Inheritance
Abstract class HelloWorldBase // abstract class
{
Public abstract void writeHelloWorld ();
}
Class HelloWorld: HelloWorldBase // inheritance ---- you have to take it seriously. You can abstract it and design the system architecture!
{
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 () // static structure
{
StrHelloWorld = "Hello World ";
}
Void writeHelloWorld ()
{
Console. WriteLine (strHelloWorld );}

Public static void Main ()
{
HelloWorld hw = new HelloWorld (); // do I need it?
Hw. writeHelloWorld (); // I usually think it's ridiculous-it's so cool.

}
}

9. Exception Handling
Using System;

Public class HelloWorld
{
Public static void Main (string [] args)
{
Try
{
Console. WriteLine (args [0]);
}
Catch (IndexOutOfRangeException e) // exception handling will be performed, but how can we better Recycle resources? Exception: What should I do next? I used to make such a mistake. How can I use GC better? I am still not very familiar with it.
{
Console. WriteLine (e. ToString ());
}
}
}

10. Creating a DLL and using it in an application// Do components?
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 // attributes will be used
{
Return "Hello World ";
}
}

Public static void Main ()
{
HelloWorld hw = new HelloWorld ();
Console. WriteLine (cs. strHelloWorld );
}
}

12. Using Delegates// Delegate!
Using System;
Class HelloWorld
{
Static void writeHelloWorld (){
Console. WriteLine ("HelloWorld ");
}
Static void Main (){
SimpleDelegate d = new SimpleDelegate (writeHelloWorld); // delegate ?!?!
D (); // The syntax is indeed written in this way, but the meaning cannot be understood. It is not helpful to understand it.
}
}

13. Using Attributes // I won't! Tutorial!
# 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// Interface will be used
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 // I will not again! Tutorial go to again!
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// I usually do not pay attention to this! So far, I still don't quite understand how Unsafe!
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
{// COM
[DllImport ("kernel32")]
Private static extern int Beep (int dwFreq, int dwDuration );

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

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.