Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

protected: practical reference

protected: practical reference: protected exposes a member to its type and derived classes through an appropriate receiver.

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

Syntax

protected /* grammar-specific form */

protected exposes a member to its type and derived classes through an appropriate receiver.

Use it for controlled extension points in a deliberately designed hierarchy.

It expands the compatibility surface almost like public and is hard to retract later.

protected internal and private protected grant access to different sets of callers.

The complete program actually uses protected; its exact expected stdout is 5.

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

Result

5

The complete program actually uses `protected`; its exact expected stdout is `5`.

Sources