بسم الله الر حمن الر حيم

Berikut ini, akan saya tuliskan cara-cara menulis dan mengkompile hello world pada bahasa C#:

Example 1
000: // HelloWorld\Hello1.cs
001: public class Hello1
002: {
003:     public static void Main()

004:     {
005:         System.Console.WriteLine("Hello, World!");
006:     }
007: }
Output
Hello, World!
Code Discussion
  • Every Main method must be contained inside a class (Hello1 in this case).
  • The System.Console class contains a WriteLine method that can be used to display a string to the console.
Example 2

To avoid fully qualifying classes throughout a program, you can use the using directive as shown below:

000: // HelloWorld\Hello2.cs
001: using System;
002:
003: public class Hello2
004: {
005:     public static void Main()
006:     {
007:         Console.WriteLine("Hello, World!");
008:     }
009: }
Output
Hello, World!
Example 3

If you need access to the command line parameters passed in to your application, simply change the signature of the Main method to include them as shown below. This example counts and displays the command line arguments.

000: // HelloWorld\Hello3.cs
001: using System;
002:
003: public class Hello3
004: {
005:   public static void Main(string[] args)
006:   {
007:     Console.WriteLine("Hello, World!");
008:     Console.WriteLine("You entered the following {0} command line arguments:", args.Length );
009:     for (int i=0; i < args.Length; i++)
010:     {
011:        Console.WriteLine("{0}", args[i]);
012:     }
013:   }
014: }
Sample Run

Run the program using some arguments like this:

Hello3 A B C D

The output will be:

Hello, World!
You entered the following 4 command line arguments:
A
B
C
D
Example 4

To return a return code, change the signature of the Main method as shown below:

000: // HelloWorld\Hello4.cs
001: using System;
002:
003: public class Hello4
004: {
005:   public static int Main(string[] args)
006:   {
007:     Console.WriteLine("Hello, World!");
008:     return 0;
009:   }
010: }
Output
Hello, World!
Alhamdulillah, semoga bermanfaat,,,

About ifqo

Kepingin melawan arus, agar tidak tenggelam,,,

Tinggalkan Komentar/Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s