Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类的WriteLine_C#_.net_List_Class - Fatal编程技术网

C# 类的WriteLine

C# 类的WriteLine,c#,.net,list,class,C#,.net,List,Class,我正在制作一个SchoolApp程序来学习C#,我有一个主要的功能,我正在努力让它发挥作用: namespace SchoolApp { class Program { public static void Main(string[] args) { School sch = new School("Local School"); sch.AddStudent(new Student { Name =

我正在制作一个SchoolApp程序来学习C#,我有一个主要的功能,我正在努力让它发挥作用:

namespace SchoolApp
{
    class Program
    {
        public static void Main(string[] args)
        {
            School sch = new School("Local School");
            sch.AddStudent(new Student { Name = "James", Age = 22 }); // this function
            sch.AddStudent(new Student { Name = "Jane", Age = 23 });
            Console.WriteLine(sch); // this implementation
        }
    }
}
学校班级:

namespace SchoolApp
{
    class School
    {
        public string name;

        public School()
        {
        }

        public School(string the_name)
        {
            name = the_name;
        }

        public void AddStudent(Student student)
        {

        }
    }
}
 List<Student> student_list = new List<Student>();
学生班级: 命名空间学校应用程序

{
    class Student
    {
        public int Age;
        public string Name;
        public Student()
        {
            Age = 0;
            Name = "NONAME";
        }

        public string _name 
        {
            get
            {
                return this.Name;
            }
            set
            {
                Name = value;
            }
        }
        public int _age
        {
            get
            {
                return this.Age;
            }
            set
            {
                Age = value;
            }
        }

    }
}
我是C#的新手,对于如何制作AddStudent以及如何使用WriteLine编写课程,我感到非常困惑。输出应符合以下要求:

学校:本地学校 詹姆斯,22岁 简,23岁

有人建议使用列表,我遇到了同样的问题,对它了解不够


编辑:谢谢大家的快速回答。我让它工作,现在将开始深入阅读答案,这样我就不必再问相关问题了

听起来像是要添加一个
学校
和相应的
学生

public class Student
{
    public string StudentName { get; set; }
    public int Age { get; set; }
}

public class School : Student
{
    public string SchoolName { get; set; }
}

List school=newlist();
添加(新学校(){SchoolName=“USA”,StudentName=“har”,年龄=25});
添加(新学校(){SchoolName=“USM”,StudentName=“japz”,年龄=25});
foreach(学校中的var项目)
{
Console.WriteLine(“学校名称:{0}名称:{1}年龄:{2}”,item.SchoolName,item.StudentName,item.Age);
}

在你的
学校
课堂上,你似乎想要一份学生的私人名单。列表很简单,它采用了一种通用类型,在您的案例中是
Student
,您可以在其中添加和删除项目


在您的
学校
课堂上使用:

 private List<Student> students; 
私人名单学生;
在AddStudent代码中,您可以简单地使用:

students.Add(student);  // student is the variable being passed in and students is the list.  NOTE: You will have to make sure students is intalized to a new List of Student.  students = new List<Student>();
student.Add(student);//student是传入的变量,students是列表。注意:您必须确保将学生添加到新的学生列表中。学生=新列表();
看看下面的链接。MSDN是合适的文档,但dotnetperls适合于这样简单的东西

有人建议使用
List
我遇到了同样的问题,对它了解不够


是的,这门课适合这种情况。它有一个.Add方法,允许您向其中添加对象。然后您可以循环浏览此列表的元素(使用
foreach
语句),然后使用console.WriteLine方法将每个元素的详细信息打印到控制台。

您可以将扩展方法“WriteStudent”写入控制台类,并接受student作为参数。在方法内部,您可以使用
控制台。Write(“School:+objParameter.Name)
控制台。WriteLine(t)
执行t到字符串的隐式转换。所以您需要为所有类重写
ToString()
,或者显式地从类中创建字符串

例如:

class School
    {
        public string name;
        List<Student> Students = new List<Student>();

        ...
        public override string ToString()
        {
           var stringValue = name;
           foreach(var student in Students)
           {
              stringValue += student.ToString();
           }
           return stringValue;
        }
    }

class Student
{
    public int Age;
    public string Name;
   ...
    public override string ToString()
    {
        return string.Format("{0} {1}", Name, Age);
    }

}
班级学校
{
公共字符串名称;
列出学生=新建列表();
...
公共重写字符串ToString()
{
var stringValue=名称;
foreach(学生中的var学生)
{
stringValue+=student.ToString();
}
返回字符串值;
}
}
班级学生
{
公共信息;
公共字符串名称;
...
公共重写字符串ToString()
{
返回string.Format(“{0}{1}”,名称,年龄);
}
}
班级学校
{
公共字符串名称;
私人名单学生=新名单();
公立学校()
{
}
公立学校(输入学校名称)
{
名称=名称;
}
公立学校学生(学生)
{
学生。添加(学生);
}
公共重写字符串ToString()
{
var sb=新的StringBuilder();
某人(姓名);
foreach(学生中的var学生)
{
某人(学生ToString());
}
使某人返回字符串();
}
}
班级学生
{
...
公共重写字符串ToString()
{
返回“学生:“+姓名+”,“+年龄;
}
}

您有两个基本选项:

  • 为每个学生添加“学校”属性(变量)
  • 为每个学校添加“学生列表”属性
  • 如果您喜欢第二种方法:

    您需要向学校类添加一个新变量:

    namespace SchoolApp
    {
        class School
        {
            public string name;
    
            public School()
            {
            }
    
            public School(string the_name)
            {
                name = the_name;
            }
    
            public void AddStudent(Student student)
            {
    
            }
        }
    }
    
     List<Student> student_list = new List<Student>();
    
    另外请注意,如果希望打印的顺序是特定的,则必须在学生和学校班级中都添加ToString()方法


    希望有帮助,祝你好运。

    首先,你需要一个容器来存储
    学校
    类中
    学生
    类的实例。然后,在
    AddStudent
    方法中,您只需使用List类本身公开的方法将学生添加到集合中:

    class School
    {
        public string name;
        private List<Student> students = new List<Student>();
    
        // ...
    
        public void AddStudent(Student student)
        { 
            students.Add(student);
        }
    }
    
    并在您的
    Main
    中使用它,就像:

    sch.Print();
    
    除了需要成为
    学校
    成员的
    列表
    ,在主要打印方法中添加了循环(
    foreach
    ),您还应该查看以下内容:

    • 你的课程应该公开吗?主程序可能不在同一命名空间中
    • 您是否考虑过将自动属性与getter和setter一起使用?

    • 您的命名约定,


    所有这些都是因为你说你是新来的,所以我想不妨给你一些建议。

    我该如何避免呢?我试过了,但还是遇到了一些错误。错误1'SchoolApp.School.ToString()':并非所有代码路径都在此处返回一个值:public override string ToString()这非常有效。现在来深入研究一下。非常感谢你。
    public void Print()
    {
        Console.WriteLine(name);
        foreach (Student s in students)
            Console.WriteLine(s.Name + ", " + s.Age);
    }
    
    sch.Print();
    
    public int Age { get; set; }
    public String Name { get; set; }