# Foreach

[MSDN Reference](https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx)

"The foreach statement is used to iterate through the collection to get the information that you want", but should not be used to add or remove items from the source collection. "If you need to add or remove items from the source collection, use a for loop."

Example: Given a Monster constructor: Monster(int n)

```java
List<Monster> monsterList = new List<Monster>();  //Initialize List<T> using List<T> constructor

monsterList.add(new Monster(5));  //call Monster Constructor
monsterList.add(new Monster(6));
monsterList.add(new Monster(7));

foreach( Monster m in monsterList){
    Debug.Log(m.ToString());
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/f20_bkup_v2/c-language/c_language/foreach.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
