Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

protected: odniesienie praktyczne

protected: odniesienie praktyczne: protected udostępnia składową typowi i klasom pochodnym przez właściwy odbiornik.

Wersja: C# language; verified against C# 14 reference

Składnia

protected /* grammar-specific form */

protected udostępnia składową typowi i klasom pochodnym przez właściwy odbiornik.

Stosuj do kontrolowanych punktów rozszerzenia w zaprojektowanej hierarchii.

Rozszerza powierzchnię kompatybilności niemal jak public; trudno później zmienić kontrakt.

protected internal i private protected mają inne zbiory odbiorców.

Kompletny program rzeczywiście używa protected; oczekiwany stdout to dokładnie 5.

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 Child().Read());
class Parent { protected int Value => 5; } sealed class Child : Parent { public int Read() => Value; }

Wynik

5

Kompletny program rzeczywiście używa `protected`; oczekiwany stdout to dokładnie `5`.

Źródła