Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 15 - C# Tutorial - for and foreach loops in c#


Part 15 - C# Tutorial - for and foreach loops


7 comments:

  1. Hi Venkat,

    I am playing with the foreach loop to tray and make it more flexible in the code below. You'll notice i've commented out a variable k. When I used this variable my code runs correctly. I have now however tried to achieve the code without the variable k and instead just using the i varable in the foreach loop to the same affect, but it gives me the incorrect answer. Can you explain this?

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Enter Number:");
    int count = int.Parse(Console.ReadLine());

    int[] number = new int[count];
    int sum = 0;
    //int k =0;

    foreach (int i in number)
    {
    number[i] = (i+1)*10;
    //number[i] = (i + 1) * 10;
    //k++;
    sum += number[i];
    Console.WriteLine("Number: {0}", number[i]);
    Console.WriteLine("Sum: {0}", sum);
    Console.WriteLine("");
    }
    }
    }

    ReplyDelete
    Replies
    1. There is no need to use number array for processing instead use variable i in the foreach loop.

      Delete
  2. Hi Venkat,

    You're doing an amazing job with the videos. Thanks a lot!

    I think there might be an additional difference between the for and the foreach loop, and please correct me if I'm wrong.

    If you iterate through a collection of items using the foreach loop, you can't modify the actual items. You can only make use of the information they contain.

    To modify the actual items, I think one should use a for loop instead because referring to the items by their indexes allows the developer to modify them.

    What do you think?

    ReplyDelete
  3. Venkat! it was difficult to me for selecting my future field but once i dig into your tutorials it makes me easy to select DOT-NET as my future field thanks a lot Venkat!

    ReplyDelete
  4. Dear Venkat, You videos are very helpful for me, I am a non-developer and comes from Infra support, I can easily write basic C# programs with by seeing your videos. Thanks a lot

    ReplyDelete
  5. Hello Venkat, Your videos have been so helpful to my programming career.

    Currently I have develop any Easy topup for Airtime solution as a plugin into and inventory management system. The problem I am having now is the the program is very slow because of a for loop iteration that all items must be check to see if the ate associated to and AIRTIME item.

    Sir, I want to know what I can use to replace the for and my program will still work normal.

    Below is my source code:

    // VTU Item Check Iteration
    for (i = 0; i <= dt.Rows.Count - 1; i++)
    {
    dr = dt.Rows[i];
    RefNo = dr["REFERENCIA"].ToString();
    ItemCode = long.Parse(dr["CODARTICULO"].ToString());
    Amount = double.Parse(dr["TOTAL"].ToString());
    codvendor = dr["CODVENDEDOR"].ToString();
    txtUser.Text = DataClass.RetrieveOneICG(codvendor, "VENDEDORES", "CODVENDEDOR", "NOMVENDEDOR", GenSettingClass.ICGconString);

    MyDbContext db = new MyDbContext();
    ProductEntity data = db.Products.Where(d => d.ICGItemCode == ItemCode).FirstOrDefault();
    if (data != null)
    {
    ListViewItem itm = new ListViewItem((lvStock.Items.Count + 1).ToString());
    itm.SubItems.Add(data.ProductName);
    itm.SubItems.Add(DataClass.getProductTypeName(data.ProdTypeCode));
    itm.SubItems.Add(Amount.ToString());
    itm.SubItems.Add(data.Code.ToString());
    itm.SubItems.Add(data.ProdID.ToString());
    lvStock.Items.Add(itm);
    }
    }

    ReplyDelete
  6. Hi, the code seems to be good, all you need to do is to create index in table

    ReplyDelete

It would be great if you can help share these free resources