База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / C#

this: практическа справка

this: практическа справка: this сочи текущата инстанция или верижно извиква друг конструктор.

Версия: C# language; verified against C# 14 reference

Синтаксис

this /* grammar-specific form */

this сочи текущата инстанция или верижно извиква друг конструктор.

Използвайте го за различаване на поле от параметър и последователно верижно конструиране.

Не е достъпно в static контекст; изтичане на this от конструктор разкрива незавършен обект.

В struct this има стойностна семантика и спазва readonly ограниченията.

Пълната програма действително използва this; точният очакван stdout е 4.

Примери

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 Box(4).Read()); sealed class Box { private readonly int value; public Box(int value) => this.value = value; public int Read() => this.value; }

Резултат

4

Пълната програма действително използва `this`; точният очакван stdout е `4`.

Източници