我创建了一个名为CustomDataList
的类,并且具有以下代码:
private List<string> students = new List<string>();
public List<string> GetList()
{
return students;
}
public void AddStudent()
{
students.Add("Morgan");
students.Add("Loren");
students.Add("Martin");
students.Add("Ariana")
students.Add("Nikkita");
foreach(var element in students)
{
Console.WriteLine(students);
}
}
我想向控制台显示我创建的列表。我该如何进行?如何在Main方法中调用AddStudent()
方法?
答案 0 :(得分:0)
我有样品测试,可以尝试以下方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var customDataList = new CustomDataList();
customDataList.AddStudent();
foreach(var element in customDataList.GetList())
{
Console.WriteLine(element);
}
}
}
public class CustomDataList {
private List<string> students = new List<string>();
public List<string> GetList()
{
return students;
}
public void AddStudent()
{
students.Add("Morgan");
students.Add("Loren");
students.Add("Martin");
students.Add("Ariana");
students.Add("Nikkita");
}
}
}
答案 1 :(得分:0)
只需使用CustomDataList
方法创建Main
的实例。
var datalist=new CustomDataList ();
datalist.AddStudent();