CKTIS / C#
var: praktická reference
var: praktická reference: var žádá kompilátor, aby z inicializátoru odvodil statický typ lokální proměnné.
Verze: C# language; verified against C# 14 reference
Syntaxe
var /* grammar-specific form */var žádá kompilátor, aby z inicializátoru odvodil statický typ lokální proměnné.
Použijte jej, když je typ zřejmý nebo název proměnné lépe sděluje roli.
Neznamená dynamic a odvozený typ se po přiřazení nemůže změnit.
Vyžaduje inicializátor a typ nelze odvodit jen z null či netypované method group.
Úplný program skutečně používá var; přesně očekávaný stdout je Int32.
Příklady
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var value = 7; Console.WriteLine(value.GetType().Name);
Výsledek
Int32
Úplný program skutečně používá `var`; přesně očekávaný stdout je `Int32`.