在c#中向列表中添加多个数据

本文关键字:添加 数据 列表 | 更新日期: 2023-09-27 18:13:49

嗨,我试图添加一些数据到我的列表,包括字符串和整数。我想在同一个列表中存储学生的姓名、姓氏和电话号码,所以我使用class。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 namespace educationsystem
{
   public class student 
        {
            public string name { get;set;}
            public string lastname {get;set;}
            public long phone {get;set;}
        }
           List<student> Students = new List<student>();
           Students.Add(new student {name= "mahta",lastname= "sahabi",phone= "3244"});
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("please choose one of the numbers below : "+"'n");
        Console.WriteLine("1.adding new student"+"'n");
        Console.WriteLine("2.adding new course"+"'n");
        Console.WriteLine("3.adding new grade"+"'n");
        Console.WriteLine("4.showing the best student"+"'n");
        Console.WriteLine("5.getting students average"+"'n");
        Console.WriteLine("6.exit"+"'n");
         string input = Console.ReadLine();

             if (input == "1")
            {
                Console.Clear();
                List<int> grades = new List<int>();
                    Console.WriteLine("please enter the students name");
                    string name = Console.ReadLine();
                    Console.WriteLine("please enter the students last name");
                    string lastname = Console.ReadLine();
                    Console.WriteLine("please enter the students phone number");
                    long phone = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine(name +" "+ lastname +" " + phone);


            }
            else if (input == "2")
            {
            }
            else if (input == "3")
            {
            }
            else if (input == "4")
            {
            }
            else if (input == "5")
            {
            }

        Console.ReadKey();
}
 }
       }

,但它不工作。我名单上的名字是学生,但是系统认不出来。你能帮我一下吗?

在c#中向列表中添加多个数据

应该将列表放在类中在c#中不可能使变量全局化。全局变量可以在PHP和c++等语言中实现

class Program
{
    public List<student> Students = new List<student>();
}

如果你想让学生全局访问你的类,你可以把它设置为静态

public static List<student> Students = new List<student>();