Skip to content

CSharp Operator Increment

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Інкремент і декремент

Довідник C# · Практичне керівництво

Префікс повертає нове значення; постфікс повертає попереднє. Обидві форми змінюють змінну.

Простий приклад (Example.cs)

Збережіть цей приклад окремо як Example.cs. Запускайте його незалежно від інших прикладів на сторінці.

Example.cs:

// PROJECT-EXAMPLE
int count = 2;
System.Console.WriteLine(count++);
System.Console.WriteLine(++count);

Результат:

2
4

Синтаксис

++value / value++ / --value / value--

Приклад 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        int value = 2;
        Console.WriteLine(value++); Console.WriteLine(++value);
        Console.WriteLine(value--); Console.WriteLine(--value);
    }
}

Приклад 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        int[] values = [10, 20, 30];
        int index = 0;
        while (index < values.Length) Console.WriteLine(values[index++]);
    }
}

Офіційне джерело: Microsoft Learn.

Clone this wiki locally