وقت بخیر استاد
من یک متد در یک کلاس univers دارم و میخام این کلاس رو داخل جنریک لیست قرار بدم تا مقادیر رو بگیره و داخل حلقه foreach نشون بده اما نمیدونم کد داخل foreach چطور بنویسم
string name;
            string age;
            List<universe> collection = new List<universe>();
            for (int i = 0; i < 3; i++)
            {
                universe main = new universe();
                Console.WriteLine("name");
                name = Console.ReadLine();
                Console.WriteLine("age");
                age = Console.ReadLine();
                main.Galaxy(name, age);
                collection.Add(main);
            }
            foreach (universe b in collection)
            {
                Console.WriteLine(b.Galaxy(name, age));
            }Console.WriteLine(b.name , b.age)) اینجوری هم مینویسم نمیشه
سلام
الان این کد مشکل چیه ؟
اگر ارور داره متن رو قرار بدید
Use of unassigned local variable name
Use of unassigned local variable age
به نظر من چون name و age داخل متدgalaxyهستند نمیتونم از داخل collectionمستقیم فراخوانیشون کنم
     foreach (universe b in collection)
            {
                Console.WriteLine(b.Galaxy(b.name, b.age));
            }
    for (int i = 0; i < 3; i++)
            {
                universe main = new universe();
                Console.WriteLine("name");
                main.name = Console.ReadLine();
                Console.WriteLine("age");
                main.age = Console.ReadLine();
                main.Galaxy(name, age);
                collection.Add(main);
            }
استاد این دقیقا مثل همون مثالیه که شما در اخر قسمت 13 با جنریک لیست زدین
فقط اونجا شما از پروپرتی استفاه کردین اینجا من از این متد.
class universe
    {
        public string Galaxy(string name ,string age)
        {
            return name + " " + age;
        }
        
    }
کد های کامل رو قرار بدید
 class Program
    {
        static void Main(string[] args)
        {
            
            
            List<universe> collection = new List<universe>();
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("name");
                Main.name = Console.ReadLine();
                Console.WriteLine("age");
                Main.age =Console.ReadLine();
                universe main = new universe();
                main.Galaxy(name, age);
                collection.Add(main);
            }
            foreach (universe b in collection)
            {
                Console.WriteLine(b.Galaxy(name,age));
            }
            Console.ReadKey();
        }
    }
 class universe
    {
        public string Galaxy(string name,string age) 
        {
            return name + " " + age;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<universe> collection = new List<universe>();
            for (int i = 0; i < 3; i++)
            {
                universe main = new universe();
                Console.WriteLine("name");
                main.name = Console.ReadLine();
                Console.WriteLine("age");
                main.age = int.Parse(Console.ReadLine());
                
                
                collection.Add(main);
            }
            foreach (universe b in collection)
            {
                Console.WriteLine(b.Galaxy(b.name, b.age.ToString()));
            }
            Console.ReadKey();
        }
    }
    class universe
    {
        public string name { get; set; }
        public int age { get; set; }
        public string Galaxy(string name, string age)
        {
            return name + " " + age;
        }
    }
مرسی لطف کردین