C# 在c语言中向列表添加多个数据#
嗨,我正在尝试向列表中添加一些数据,包括字符串和整数。我想把学生的名字、姓氏和电话号码存储在同一个列表中,这样我就可以使用class了。这是我的密码C# 在c语言中向列表添加多个数据#,c#,list,C#,List,嗨,我正在尝试向列表中添加一些数据,包括字符串和整数。我想把学生的名字、姓氏和电话号码存储在同一个列表中,这样我就可以使用class了。这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace educationsystem { public class student { public stri
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间教育系统
{
公立班学生
{
公共字符串名称{get;set;}
公共字符串lastname{get;set;}
公共长电话{get;set;}
}
列出学生=新建列表();
添加(新学生{name=“mahta”,lastname=“sahabi”,phone=“3244”});
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“请从下面的数字中选择一个:“+”\n”);
Console.WriteLine(“1.添加新学生”+“\n”);
Console.WriteLine(“2.添加新课程”+“\n”);
Console.WriteLine(“3.添加新等级”+“\n”);
Console.WriteLine(“4.显示最好的学生”+“\n”);
Console.WriteLine(“5.学生平均”+“\n”);
Console.WriteLine(“6.退出”+“\n”);
字符串输入=Console.ReadLine();
如果(输入=“1”)
{
Console.Clear();
列表等级=新列表();
Console.WriteLine(“请输入学生姓名”);
字符串名称=Console.ReadLine();
Console.WriteLine(“请输入学生的姓氏”);
字符串lastname=Console.ReadLine();
Console.WriteLine(“请输入学生电话号码”);
long phone=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(name+“”+lastname+“”+phone);
}
否则如果(输入=“2”)
{
}
否则如果(输入=“3”)
{
}
否则如果(输入=“4”)
{
}
否则如果(输入=“5”)
{
}
Console.ReadKey();
}
}
}
但它不起作用。我的名单名是学生,但系统无法识别。你能帮我吗?你应该把你的名单放在你的班级里 在c#中,使变量全局化是不可能的。 全局变量可以用PHP和c等语言完成++
class Program
{
public List<student> Students = new List<student>();
}
类程序
{
公共列表学生=新列表();
}
如果你想让学生从你的课堂上进入全局,你可以让它成为静态的
public static List<student> Students = new List<student>();
公共静态列表学生=新列表();
你应该把你的名单放在你的班级里
在c#中,使变量全局化是不可能的。
全局变量可以用PHP和c等语言完成++
class Program
{
public List<student> Students = new List<student>();
}
类程序
{
公共列表学生=新列表();
}
如果你想让学生从你的课堂上进入全局,你可以让它成为静态的
public static List<student> Students = new List<student>();
公共静态列表学生=新列表();
哪些学生或成绩列表?哪些学生或成绩列表?如果我的答案解决了您的问题,请单击大复选框将其作为答案。我应该如何在输出中打印我的列表?Students.ForEach((student)=>{Console.WriteLine($“name={student.name}\nlastName{student.lastname}\nphone={student.phone}”););你是最好的如果我的答案解决了你的问题,点击大复选框接受它作为答案。我应该如何在输出中打印我的列表?Students.ForEach((student)=>{Console.WriteLine($“name={student.name}\nlastName{student.lastname}\nphone={student.phone}”);});你是最棒的