Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

class: practical reference

class: practical reference: class declares a reference object type with single base-class inheritance.

Version: C# language and .NET 10 reference runtime

Syntax

class /* grammar-specific form */

class declares a reference object type with single base-class inheritance.

Use it when identity, shared mutation, or polymorphism is required.

Assignment copies a reference, not the object; equality is reference-based by default.

A variable or reference of a class type can be null; the class itself is not null. A class can be abstract, sealed, or generic and must initialize its invariant.

The complete program actually uses class; its exact expected stdout is 1.

Examples

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; }

Result

1

The complete program actually uses `class`; its exact expected stdout is `1`.

Sources