answersLogoWhite

0


Best Answer

Foreach is for an IEnumerable, not just an array (unless you are using C# 1.1 or earlier). A collection (List, Dictionary, etc) can also generate a sequence from foreach-statement. In fact, if you have a class that is NOT an array, but it implements all the required methods of IEnumerable, it may be applied as:

public class Storage: IEnumerable {...}

Storage myStorage = new Storage();

...

Foreach (Thing stuff in myStorage)

{...}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it mandatory to use array in foreach loop C-sharp can foreach be used without array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is array is value type or reference type in CSharp?

refernce type


Array is value type or reference type in CSharp?

Array is a class name, hence ought to be a value type.


What purpose is foreach function in php?

foreach is a simple loop mostly used to read an array line by line; $arr = array("one", "two" , "three"); foreach($arr as $c) { echo $c; } it'll output : onetwothree also try looking up the while loop


How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


What are the advantages of using a foreach loop over a for or while loop for processing list elements?

foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.


What is a foreach loop?

A foreach loop takes an array, and goes through the loop you specify - each time, it goes to the next index of the array. When it has gone through every index, the script continues. This can be useful to retrieve data from a database, and insert it into a table, list, or into general content space, or to easily manipulate all the data in an array. The syntax varies depending on the programming language you are using.


How do you print an array in php?

There are a few methods however the following is the method that will allow you to do the most with the information afterwards. foreach($array as $key => $value){ echo '[' . $key . '] ' . $value; #$key becomes the array key and value because what the current array item has inside. }


How do you add different logo to my page?

if (class_exists('MultiPostThumbnails')) { //$types = array('post', 'page'); $types = array('page'); foreach($types as $type) { new MultiPostThumbnails(array( 'label' => 'Header Banner', 'id' => 'secondary-image', 'post_type' => $type ) ); } }


How do you create a loop using PHP?

There are a number of different ways to loop using PHP. They're as follows:ForForeachWhileDo-whileBelow are some examples of how each of these types of loop work.ForForeachWhileDo-while


How can you make a C Sharp Program Read a Multiline String Line by Line and Return an Output?

You can get the block of text and then split it by line and put it into an array. You can then iterate through the array of strings to edit each line e.g: String multiline_string; String[] string_array; multiline_string.split("\n"); foreach(string line in multiline_string) { console.writeline(line); }


Difference between for and foreach?

difference between for and for each loop..?The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. there is need to specify the loop bounds( minimum or maximum).int j = 0;for (int i = 1; i


What is the difference between array in c and c sharp language?

The syntax to access a particular element in an array are the same in both languages: For example: assume array is an array of 10 int(egers): to get the first element: array[0] (both are 0 based indexing] int i = 0; while (i < array.Length) { // do something to array[i] } int i = 0; int length = sizeof(array) / sizeof(int); while (i < length) { // do something to array[i] } However, an array in C# is also enumerable (C does not have this feature) in C#, you may loop thru the array by: foreach (int number in array) { // do something to array[i] } Plus, C# is an Object-Oriented Language, so that an array may be of some object types, not just those primitiives data types in C: object[] objectArray; // any object derived from Object may be placed into objectArray, not just struct. In another variation, an array may be of Delegate type in C#(sort of like function pointers in C)