none
access List<Bok> objects. RRS feed

  • Question

  • Hi,

    static void Main(string[] args)
            {
                List<Bok> Lista;

    How do I access this by Foreach(Bok item in Lista)

    {

    }

    ??

    This does not work

    Wednesday, September 19, 2018 5:46 PM

All replies

  • Maybe the code below will help you understand how lists and loops work:

    public class Bok
    { 
        public string Property = string.Empty;
    }
           
    private static void Main(string[] args)
    {
        List<Bok> Lista = new List<Bok>(); // You have to create new List<Bok> object.
    
        var bok1 = new Bok { Property = "bok1" };
        var bok2 = new Bok { Property = "bok2" };
        var bok3 = new Bok { Property = "bok3" };
    
        Lista.Add(bok1);
        Lista.Add(bok2);
        Lista.Add(bok3);
    
        foreach (Bok item in Lista)
        {
            Console.WriteLine(item.Property);
        }
    }


    Wednesday, September 19, 2018 6:42 PM