Movatterモバイル変換


[0]ホーム

URL:


Jump to content
Wikiversity
Search

C Sharp/First Program

From Wikiversity
<C Sharp

Hello World

[edit |edit source]

We will create the famous "Hello World!" example in a C# console application.

Using Visual Studio 2008

[edit |edit source]
  • Create a new project (File->New->Project)
  • Select Visual C# inProject types
  • Select Console Application from theVisual Studio installed templates
  • Click OK

Using Visual Studio Code (VS Code)

[edit |edit source]
  • Open the terminal in VS Code (View -> Terminal).
  • Navigate to the directory where you want to create your project.
  • Run the following command to create a new console application:
    dotnet new console -n HelloWorld
  • This will create a new folder namedHelloWorld with the necessary files.

Using JetBrains Rider

[edit |edit source]
  • Open Rider.
  • Click onNew Solution in the welcome screen.
  • SelectConsole Application under.NET Core.
  • Choose a location for your project and name itHelloWorld.
  • ClickCreate.


Now we're ready to write some code. The project should have a file calledProgram.cs. Open that file to edit the code that will execute.

Program.cs

[edit |edit source]
usingSystem;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){}}}

Note that you don't need thestring[] args part of your loop. In fact, you can omit it like so:

usingSystem;namespaceConsoleApplication1{classProgram{staticvoidMain(){}}}

Note: In the end of your C# program, for VSCode or Visual Studio, you need to addConsole.ReadKey(); or else the program will close after executing the last line of code in thestatic void Main() orstatic void Main(string[] args) loop.

Remarks

[edit |edit source]
usingSystem;

Ensures that the program will use the System namespace. The System namespace includes the console class, which is necessary for input/output operations with the console. It is important to note that each line of code terminates with a semi-colon (unless an open curly bracket is used).

namespaceConsoleApplication1

Places the following code into the ConsoleApplication1 namespace. Later on the programmer may choose to create a namespace with their name or company name. It is important that this namespace name be unique so that it does not conflict with other namespaces when unintended.

{

An open curly bracket signifies a block of code. All the code that falls between { and its corresponding } will be affected by the line of code prior to the opening bracket. In this case, these two brackets ensure that all the rest of the code falls within the namespace ConsoleApplication1.

staticvoidMain(string[]args)
staticvoidMain()

The main entry point into the program. When a console application runs it looks for the method entitled Main(string[] args). After the method is found the program will begin to execute at the first line after the Main entry point.

Console Output

[edit |edit source]

The following code outputs a line of text to the console.

Console.WriteLine("Hello World!");

This code needs to be inserted just after the entry point of the program.

...staticvoidMain(string[]args){Console.WriteLine("Hello World!");}

The code can be compiled and run by pressing F5 or by Debug->Start Debugging. The code will output Hello World! to the console, and then quickly shut down. To pause the program we can ask the program to request that the user hit a key.

...staticvoidMain(string[]args){Console.WriteLine("What is your name? ");stringname=Console.ReadLine();Console.WriteLine("Hello {0}!",name);Console.ReadKey();// wait for user input}

Note: Double forward slash (//) is one method for creating comments in the code. Any text after the // is ignored by the compiler until the next line. Comments are used for many reasons. Here we explain the purpose of code that may be unclear just by reading the code.

Congratulations! We've created a fully functional C# program using the console. Check outWhere To Go Next to find the next lesson.

Hello World with Windows Forms

[edit |edit source]

Using Visual Studio 2003, we will create the famous "Hello World!" example in CSharp (Also referred to as C#).

First Create a new project.

Do this by following these steps:

1) Start Visual Studio

2) Select the Menu item: "File"

3) Select the Sub-Menu item: "New"

4) Select the Sub-Menu item: "Project"

5) Select Project Types: "Visual C# Projects", Selecting Templates: "Windows Application"

6) For Name: type "HelloWorld", then select the 'OK' button.

Congratulations you have created a new project!

Now, let's create our "Hello World!" example.

1) In the Solution Explorer on the right, you will notice your "HelloWorld" project has several files. Using your mouse, double-click the Form1.cs file. You will see that Form1 now displays in your viewer in [Design] Mode.

2) Now Select this [Design] mode Form1 with your mouse. Right-click and select "Properties".

3) You will see a list of what are called Events. These events are messages your windows form can respond to. Select the "Load" event and double-click this.

4) This will take you to [Code] Mode. You will see the following code displayed after double clicking the "Load" event:

privatevoidForm1_Load(objectsender,System.EventArgse){}

5) in between the two curly braces (ie: "{ }") type the following:

MessageBox.Show(this,"Hello World!");

6) Here's what you should see:

privatevoidForm1_Load(objectsender,System.EventArgse){MessageBox.Show(this,"Hello World!");}

7) Run the program to see "Hello World!" by Pressing and holding down the two keys: CTRL F5

8) This will start your program and you will see a message box display "Hello World!"

Congratulations you have made your first C# program!--Darshan Arney 21:58, 17 October 2006 (UTC)

Where To Go Next

[edit |edit source]
Topics inC#
BeginnersIntermediateAdvanced
Part of theSchool of Computer Science
Retrieved from "https://en.wikiversity.org/w/index.php?title=C_Sharp/First_Program&oldid=2712894"
Category:

[8]ページ先頭

©2009-2026 Movatter.jp