views:

42

answers:

2

Below codes run perfectly but i want to re generate simply

 static void YeniMethodListele()
        {
            Calısan calisan = new Calısan(){ ID=1, Ad="yusuf", SoyAd="karatoprak"};
            List<Calısan> myList = new List<Calısan>();
            myList.Add(calisan);
            MyCalısan myCalısan = new MyCalısan() { list = myList };
            //myCalısan.list.Add(calisan);

            foreach (Calısan item in myCalısan.list)
            {
                Console.WriteLine(item.Ad.ToString());
            }
        }
    }

   public class Calısan
    {
        public int ID { get; set; }
        public string Ad { get; set; }
        public string SoyAd { get; set; }

    }

   public class MyCalısan
    {
        public List<Calısan> list { get; set; }
        public MyCalısan()
        {
            list = new List<Calısan>();
        }
    }
A: 

i find it. it is good. sorry i can not descripe my question sorry:

 static void YeniMethodListele()
        {
            List<Calısan> myList = new List<Calısan>();
            myList.Add(new Calısan() { Ad = "yusuf", SoyAd = "karatoprak", ID = 1 });
            foreach (Calısan item in myList)
            {
                Console.WriteLine(item.Ad.ToString());
            }
        }
Phsika
It is very confusing on your own answer too. I am not sure what you are trying to achieve. You add a "Calisan" instance to the list and then you display the Ad string. Looks like you are trying to refactor some code but what exactly is the purpose of the method in the first case? Are you trying to unit test the display of your list? Oh BTW. item.Ad is already a string. You do not need to convert it to string again. Is YeniMethodListele = NewListMethod in Turkish?
Syd
A: 

Here is a sample of a couple of ways to create the list a little more simply. Note the small change to the Calısan object to give it a default constructor and an overloaded constructor.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ////Calısan calisan = new Calısan() { ID = 1, Ad = "yusuf", SoyAd = "karatoprak" };
            MyCalısan myCalısan = new MyCalısan();

            //option 1:
            //==========
            myCalısan.list.AddRange(new[] { new Calısan() { ID = 1, Ad = "yusuf", SoyAd = "karatoprak" }, new Calısan() { ID = 2, Ad = "blah", SoyAd = "jiggy" } });

            //option 2:
            //=========
            myCalısan.list.AddRange(new[] { new Calısan(1, "yusuf", "karatoprak"), new Calısan(2, "blah", "jiggy") });

            ////myCalısan.list.Add(calisan);

            foreach (Calısan item in myCalısan.list)
            {
                Console.WriteLine(item.Ad.ToString());
            }

            Console.ReadKey();
        }
    }

    public class Calısan
    {
        public Calısan() { }

                            public Calısan(int id, string ad, string soyad)
    {
        ID = id;
        Ad = ad;
        SoyAd = soyad;
    }

        public int ID { get; set; }
        public string Ad { get; set; }
        public string SoyAd { get; set; }

    }

    public class MyCalısan
    {
        public List<Calısan> list { get; set; }
        public MyCalısan()
        {
            list = new List<Calısan>();
        }
    }
}
slugster