Skip to content

IT CSharp Keyword file

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C#: file

Manuale C# · Guida pratica

Limita la visibilità del tipo al file di output corrente. Questo è un modificatore di tipo di livello superiore, non un membro della classe.

Esempio semplice (Example.cs)

Salvare questo esempio separatamente come Example.cs. Eseguirlo indipendentemente dagli altri esempi di questa pagina.

Example.cs:

// PROJECT-EXAMPLE
System.Console.WriteLine(Helper.Double(3));
file static class Helper { public static int Double(int x) => x * 2; }

Risultato:

6

Sintassi

file class Helper { ... }

Esempio 1: forma base

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine(Helper.Double(3));
    }
}
file static class Helper { public static int Double(int value) => value * 2; }

Esempio 2: un'altra forma pratica

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Token value = new(7);
        Console.WriteLine(value.Value);
    }
}
file readonly record struct Token(int Value);

Fonte ufficiale: Microsoft Impara.

Clone this wiki locally