i understood that foreach statement's function call part only executes once :

form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication32
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			foreach (string s in deneme())
			{
				listBox1.Items.Add(s);
			}
		}
		private string[] deneme()
		{
			return new string[]{"serkan","sendur","akilli"};
		}
	}
}

form1.designer.cs :

namespace WindowsApplication32
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.FormattingEnabled = true;
			this.listBox1.Location = new System.Drawing.Point(23, 34);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(222, 160);
			this.listBox1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.listBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.ListBox listBox1;
	}
}

the list box is filled with values : serkan, sendur, akilli
just once, there is no infinite loop as i expected. do you know why?

Recommended Answers

All 5 Replies

You are in the Form Load event which is executed once.
In it you call a function zeneem(or thename:icon_biggrin: )
zeneem returns a string array, on which you do a foreach string in that array, and tell it to do something. So I think it is normal you see all your strings only once.
You could have done it without function call like:

string[] MyStrings = new string[]{"serkan","sendur","akilli"};
foreach (string s in MyStrings)
    {
        listBox1.Items.Add(s);
    }

Foreach works on the string array, not on the function call.

>Foreach works on the string array, not on the function call.
yeah that is what i concluded and surprised. i have been using c# for 5 years to learn this today!!

I learn C# for 2 years now, still learning every day!
Perhaps next time you can learn me something.

>>Foreach works on the string array, not on the function call.
>yeah that is what i concluded and surprised.

Right. No offense, but that's pretty obvious when you consider that the expression for foreach requires some form of enumerable collection. foreach is really just syntactic sugar that's converted to a more traditional loop using an enumerable interface:

foreach ( string s in deneme() )
{
  listBox1.Items.Add ( s );
}

becomes:

{
  IEnumerator e = deneme().GetEnumerator();

  try {
    string s;

    while ( e.MoveNext() ) {
      s = (string)e.Current;
      listBox1.Items.Add ( s );
    }
  }
  finally {
    // System.String is a sealed type; no disposal
  }
}

You can see from the expansion that deneme is only called once to retrieve the enumerable collection, then the loop traverses the collection. No magic, just clever compiler tricks to make your life easier.

yeah but i always considered the last portion in the foreach parenthesis as the last portion in the for paranthesis, since you are able to execute function in the last portion of for statement(like incrementing some int variable), i thought they worked similar.

Thanks for replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.