Rilasciato · in miglioramento
Guida a C# · 1/6
Per ora questo capitolo è disponibile solo in inglese.
C# is a statically typed, object-oriented language from Microsoft that runs on .NET, an open-source, cross-platform runtime. The same language and toolchain cover command-line tools, web APIs, desktop apps, Unity games, and cloud services. In this chapter you will install the .NET SDK, create and run a first project, look inside the project file, and set up an editor.
To build C# code you need the .NET SDK. It bundles the compiler, the dotnet command-line tool, the base class libraries, and the runtime. Machines that only run apps can get by with the runtime alone, but your development machine needs the full SDK.
winget on Windows, Homebrew on macOS, and your distribution's package manager or Microsoft's package feed on Linux.Open a fresh terminal afterwards and check the installation:
dotnet --version
dotnet --list-sdks
dotnet --infodotnet --version prints the SDK currently in use, and dotnet --list-sdks shows every installed SDK. Multiple SDKs can live side by side; each project declares which framework version it targets.
dotnet new scaffolds projects from templates. The console app template is called console:
dotnet new console -o HelloApp
cd HelloApp
dotnet runThe -o HelloApp option creates a HelloApp folder and puts the project inside it. You get two files, HelloApp.csproj and Program.cs, and dotnet run prints Hello, World!. The whole program is a single line:
// Program.cs
Console.WriteLine("Hello, World!");There is no class and no Main method because of top-level statements: the compiler generates the entry point around the statements in this file for you. Command-line arguments arrive in a predefined args variable:
if (args.Length == 0)
{
Console.WriteLine("Usage: dotnet run -- <name>");
return;
}
var name = args[0];
Console.WriteLine($"Hello, {name}! Today is {DateTime.Now:yyyy-MM-dd}.");Run it with dotnet run -- Alice. Everything after -- is passed to your program; options before it belong to dotnet run itself.
The .csproj file is an MSBuild XML document describing what to build and how. The template generates something like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>| Setting | What it does |
|---|---|
Sdk="Microsoft.NET.Sdk" | Uses the standard build rules for .NET projects. Web projects use Microsoft.NET.Sdk.Web. |
OutputType | Exe produces an executable; leave it out to build a class library (.dll). |
TargetFramework | The .NET version you target. The template fills in a value that matches your SDK. |
ImplicitUsings | Automatically imports common namespaces such as System, System.IO, and System.Linq. |
Nullable | Turns on nullable reference type analysis (covered in chapter 3). |
SDK-style projects include every .cs file under the project folder automatically, so adding a new source file never requires editing the project file.
These are the commands you will use most often:
dotnet build # build the Debug configuration
dotnet build -c Release # build the Release configuration
dotnet run # build, then run
dotnet watch # rebuild and rerun whenever files change
dotnet clean # remove build outputsCompiled output lands in bin/Debug/<target framework>/, and intermediate files go to obj/. Both folders can be regenerated at any time, so keep them out of source control. Running dotnet new gitignore drops a .NET-flavored .gitignore into the current folder.
C# has excellent tooling. Any of the following gives you completion, refactoring, and debugging out of the box:
Whatever you choose, dotnet format keeps code style consistent. Commit an .editorconfig at the repository root so indentation, naming rules, and warning levels are shared by the whole team.
dotnet --version.dotnet new console and run it with dotnet run.Program.cs form the entry point, and arguments come in through args..csproj file is an MSBuild document that sets the target framework, output type, and compiler options.dotnet format to keep style consistent.
0 commenti
Accedi · Accedi per lasciare un commento.
Scrivi tu il primo commento.