CKTIS / C#
class: odniesienie praktyczne
class: odniesienie praktyczne: class deklaruje referencyjny typ obiektowy z dziedziczeniem pojedynczej klasy bazowej.
Wersja: C# language and .NET 10 reference runtime
Składnia
class /* grammar-specific form */class deklaruje referencyjny typ obiektowy z dziedziczeniem pojedynczej klasy bazowej.
Stosuj, gdy potrzebujesz tożsamości, współdzielonej mutacji lub polimorfizmu.
Przypisanie kopiuje referencję, nie obiekt; equality domyślnie jest referencyjne.
Zmienna lub referencja typu klasowego może mieć wartość null; sama klasa nie jest null. Klasa może być abstract, sealed lub generic i wymaga inicjalizacji invariantu.
Kompletny program rzeczywiście używa class; oczekiwany stdout to dokładnie 1.
Przykłady
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;
Console.WriteLine(new Counter().Next());
sealed class Counter { private int value; public int Next() => ++value; }
Wynik
1
Kompletny program rzeczywiście używa `class`; oczekiwany stdout to dokładnie `1`.