Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

General Structure of a C# Program

  • 2025-07-01
Feedback

In this article

C# programs consist of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements.

using System;Console.WriteLine("Hello world!");namespace YourNamespace{    class YourClass    {    }    struct YourStruct    {    }    interface IYourInterface    {    }    delegate int YourDelegate();    enum YourEnum    {    }    namespace YourNestedNamespace    {        struct YourStruct        {        }    }}

The preceding example usestop-level statements for the program's entry point. Only one file can have top-level statements. The program's entry point is the first text line of program text in that file. In this case, it's theConsole.WriteLine("Hello world!");.You can also create a static method namedMain as the program's entry point, as shown in the following example:

// A skeleton of a C# programusing System;namespace YourNamespace{    class YourClass    {    }    struct YourStruct    {    }    interface IYourInterface    {    }    delegate int YourDelegate();    enum YourEnum    {    }    namespace YourNestedNamespace    {        struct YourStruct        {        }    }    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Hello world!");        }    }}

In that case the program starts in the opening brace ofMain method, which isConsole.WriteLine("Hello world!");

Building and running C# programs

C# is acompiled language. In most C# programs, you use thedotnet build command to compile a group of source files into a binary package. Then, you use thedotnet run command to run the program. (You can simplify this process becausedotnet run compiles the program before running it if necessary.) These tools support a rich language of configuration options and command-line switches. Thedotnet command line interface (CLI), which is included in the .NET SDK, provides manytools to generate and modify C# files.

Beginning with C# 14 and .NET 10, you can createfile based programs, which simplifies building and running C# programs. You use thedotnet run command to run a program contained in a single*.cs file. For example, if the following snippet is stored in a file namedhello-world.cs, you can run it by typingdotnet run hello-world.cs:

#!/usr/local/share/dotnet/dotnet runConsole.WriteLine("Hello, World!");

The first line of the program contains the#! sequence for Unix shells. The location of thedotnet CLI can vary on different distributions. On any Unix system, if you set theexecute (+x) permission on a C# file, you can run the C# file from the command line:

./hello-world.cs

The source for these programs must be a single file, but otherwise all C# syntax is valid. You can use file based programs for small command-line utilities, prototypes, or other experiments. File based programs allowpreprocessor directives that configure the build system.

Expressions and statements

C# programs are built usingexpressions andstatements. Expressions produce a value, and statements perform an action:

Anexpression is a combination of values, variables, operators, and method calls that evaluate to a single value. Expressions produce a result and can be used wherever a value is expected. The following examples are expressions:

  • 42 (literal value)
  • x + y (arithmetic operation)
  • Math.Max(a, b) (method call)
  • condition ? trueValue : falseValue (conditional expression)
  • new Person("John") (object creation)

Astatement is a complete instruction that performs an action. Statements don't return values; instead, they control program flow, declare variables, or perform operations. The following examples are statements:

  • int x = 42; (declaration statement)
  • Console.WriteLine("Hello"); (expression statement - wraps a method call expression)
  • if (condition) { /* code */ } (conditional statement)
  • return result; (return statement)

The key distinction: expressions evaluate to values, while statements perform actions. Some constructs, like method calls, can be both. For example,Math.Max(a, b) is an expression when used inint result = Math.Max(a, b);, but becomes an expression statement when written alone asMath.Max(a, b);.

For detailed information about statements, seeStatements. For information about expression-bodied members and other expression features, seeExpression-bodied members.

Related Sections

You learn about these program elements in thetypes section of the fundamentals guide:

C# Language Specification

For more information, seeBasic concepts in theC# Language Specification. The language specification is the definitive source for C# syntax and usage.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo