C# | printing an integer array using foreach loop

Learn, how to print an integer array using foreach loop in C#?
Submitted by Pankaj Singh, on December 25, 2018 [Last updated : March 19, 2023]

Given an integer array and we have to print its elements using "foreach loop" in C#.

Syntax for foreach loop

foreach (element in iterable-item)
{
    // body of foreach loop
}

C# program to print an integer array using foreach loop

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] data = { 12, 45, 67, 879, 89 };
            foreach(int item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

Output

12
45
67
879
89

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.