Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / C#

this: référence pratique

this: référence pratique: this désigne l’instance courante ou chaîne la construction vers un autre constructeur.

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

Syntaxe

this /* grammar-specific form */

this désigne l’instance courante ou chaîne la construction vers un autre constructeur.

Utilisez-le pour distinguer un champ d’un paramètre et garder un chaînage cohérent.

Il n’est pas disponible en contexte static ; laisser fuir this d’un constructeur expose un objet inachevé.

Dans une struct, this suit la sémantique de valeur et les restrictions readonly.

Le programme complet utilise réellement this ; sa sortie stdout exacte attendue est 4.

Exemples

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

Résultat

4

Le programme complet utilise réellement `this` ; sa sortie stdout exacte attendue est `4`.

Sources