Home » C# programs » C# basic programs

C# | printing an integer array using foreach loop

C# Example of foreach loop: Here, we are going to learn how to print an integer array using foreach loop in C#?
Submitted by Pankaj Singh, on December 25, 2018

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
    }

Program:

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.