CKTIS / C#
interface: referência prática
interface: referência prática: interface define um contrato de membros sem identidade de instância e pode oferecer implementações predefinidas.
Versão: C# language; verified against C# 14 reference
Sintaxe
interface /* grammar-specific form */interface define um contrato de membros sem identidade de instância e pode oferecer implementações predefinidas.
Usa-a para pequenos papéis intercambiáveis entre tipos diferentes.
Alterar uma interface public afeta todos os implementadores; default methods complicam o dispatch.
Uma class pode implementar várias interfaces; a implementação explícita só é visível pelo contrato.
O programa completo usa realmente interface; o stdout exato esperado é 3.
Exemplos
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;
IValue value = new Value(); Console.WriteLine(value.Read());
interface IValue { int Read(); } sealed class Value : IValue { public int Read() => 3; }
Resultado
3
O programa completo usa realmente `interface`; o stdout exato esperado é `3`.