Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮   
     ❯   

C#Syntax


C# Syntax

In the previous chapter, we created a C# file called Program.cs, and we used the following code to print "Hello World" to the screen:

Program.cs

using System;namespace HelloWorld{  class Program  {    static void Main(string[] args)    {      Console.WriteLine("Hello World!");        }  }}

Result:

Hello World!

Try it Yourself »

Example explained

Line 1:using System means that we can use classes from theSystem namespace.

Line 2: A blank line. C# ignores white space. However, multiple lines makes the code more readable.

Line 3:namespace is used to organize your code, and it is a container for classes and other namespaces.

Line 4: The curly braces{} marks the beginning and the end of a block of code.

Line 5:class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class Program.

Don't worry if you don't understand howusing System,namespace andclass works. Just think of it as something that (almost) always appears in your program, and that you will learn more about them in a later chapter.

Line 7: Another thing that always appear in a C# program is theMain method. Any code inside its curly brackets{} will be executed.You don't have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial.

Line 9:Console is a class of theSystem namespace, which has aWriteLine() method that is used to output/print text. In our example, it will output "Hello World!".

If you omit theusing System line, you would have to writeSystem.Console.WriteLine() to print/output text.

Note: Every C# statement ends with a semicolon;.

Note: C# is case-sensitive; "MyClass" and "myclass" have different meaning.

Note: UnlikeJava, the name of the C# file does not have to match the class name, but they often do (for better organization).When saving the file, save it using a proper name and add ".cs" to the end of the filename. To run the example above on your computer, make sure that C# is properly installed: Go to theGet Started Chapter for how to install C#. The output should be:

Hello World!




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2026 Movatter.jp