Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach issue in c#

I’m writing a simple console note app, and having some issues with "foreach" function. When I enter "View", the notes should be displayed in order, but for each note I get "0", instead ‘0’ ‘1’ ‘2’.

notes pic

using System;
using System.Collections.Generic;

namespace note_app_console

{
    class Program
    {
        static void Main(string[] args)

        {
            List <String> notes = new List<string>();
            Console.WriteLine("Notes");
            int userInput;
            

            //main loop
            do
            {
                string addNote;
                //selecting action from menu
                userInput = Convert.ToInt32(Console.ReadLine());

                switch (userInput)
                {
                    case 1:

                        Console.WriteLine("Enter the note content: ");
                        addNote = Console.ReadLine();
                        notes.Add(addNote);
                        Console.WriteLine("Note added.");
                        break;

                    case 2:

                        Console.WriteLine("Your notes: ");
                        foreach (string i in notes)
                        {
                            int indexNote = i.IndexOf(i);
                            Console.WriteLine($"{Convert.ToString(indexNote)}. {i}");
                            

                        }

                        break;

                        
                }
                
            } while (userInput != 4);
        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Three options here

  1. Use the notes.IndexOf()

    foreach (string item in notes)
    {
        int indexNote = notes.IndexOf(item);
        Console.WriteLine($"{indexNote}. {item}");                                                   
    }
    
  2. Use a counter

    int counter = 0;
    foreach (string item in notes)
    {
        Console.WriteLine($"{counter}. {item}");                                                   
        counter++;
    }
    
  3. Use a for loop

    for(int i=0; i<notes.Count; i++)
    {
        Console.WriteLine($"{i}. {notes[i]}");                                                   
    }
    

Note that there is no need to convert the integer into a string in the WriteLine() statement as the string interpolation does that automatically.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading