How to define, initialize and print a list of objects in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{

    class Actors
    {
        public int age { get; set; }
        public string name { get; set; }
        public override string ToString()
        {
            return "Actor: " + name + " " + age;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Actors> list = new List<Actors>()
            {
                new Actors(){ age = 83, name = "Michael Caine"},
                new Actors(){ age = 76, name = "Al Pacino"}
            };

            foreach (var item in list)
                Console.WriteLine(item);
        }
    }
}


/*
run:
      
Actor: Michael Caine 83
Actor: Al Pacino 76

*/

 



answered Dec 27, 2016 by avibootz
edited Dec 27, 2016 by avibootz
0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{

    class Actors
    {
        public int age { get; set; }
        public string name { get; set; }
        public override string ToString()
        {
            return "Actor: " + name + " " + age;
        }
    }

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

            List<Actors> list = new List<Actors>();
            list.Add(new Actors(){ age = 83, name = "Michael Caine" });
            list.Add(new Actors(){ age = 76, name = "Al Pacino" });

            foreach (var item in list)
                Console.WriteLine(item);
        }
    }
}


/*
run:
      
Actor: Michael Caine 83
Actor: Al Pacino 76

*/

 



answered Dec 27, 2016 by avibootz

Related questions

2 answers 237 views
5 answers 323 views
1 answer 122 views
5 answers 322 views
1 answer 196 views
...