C# 如何以静态方法打印集合项?

C# 如何以静态方法打印集合项?,c#,C#,试试这个: PrintFamily(fam1); private static void PrintFamily(Family family) { //I'm stuck here } 你的代码中有很多东西需要改进。您需要尊重您的属性名称的pascal大小写。此外,应该更喜欢对象初始值设定项,而不是部分实例化对象,然后进入它们的属性。它删除了很多行,并且对于初始化的内容和在一个地方完成的内容更有意义 private static void PrintFamily(Family fam

试试这个:

PrintFamily(fam1);

private static void PrintFamily(Family family)
{
    //I'm stuck here
}

你的代码中有很多东西需要改进。您需要尊重您的属性名称的pascal大小写。此外,应该更喜欢对象初始值设定项,而不是部分实例化对象,然后进入它们的属性。它删除了很多行,并且对于初始化的内容和在一个地方完成的内容更有意义

private static void PrintFamily(Family family)
{
     Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
     Console.WriteLine("Prents : ");
     Console.WriteLine($"{family.father.name} - {family.father.job} - { fam1.father.licNumber}");
     Console.WriteLine($"{family.mother.name} - {family.mother.job} - {family.mother.licNumber}");

    if(familiy.Children!=null && familiy.Children.Any())
    {

     Console.WriteLine("Kids");

        foreach{var child in family.Children}
        {
             Console.WriteLine($"{child .name} - {child .age}");
        }
    }
}
publicstaticvoidmain(字符串[]args)
{
列表族=新列表();
成人父亲=新成人{Name=“Jim”,年龄=34岁,Job=“程序员”,LicNumber=“234454”};
成人父亲=新成人{Name=“Amy”,年龄=33岁,Job=“护士”,LicNumber=“888”};
FamilyFam1=新家庭{昵称=“一家”,FamilyId=1,父亲=父亲,母亲=母亲};
}
私有静态无效显示FamilyMemberInformation(familyMember)
{
Console.WriteLine($“{family.昵称}({family.FamilyId})”;
控制台。WriteLine(“Prents:”);
WriteLine($“{fam1.father.name}-{fam1.father.job}-{fam1.father.licNumber}”);
WriteLine($“{fam1.mother.name}-{fam1.mother.job}-{fam1.mother.licNumber}”);
}
“您能告诉我如何打印孩子吗?”

唉。。杰夫·塞申斯去年5月在墨西哥边境犯下了同样的错误。如果你这样定义孩子,他们会迷路的

您已经定义了一个FamilyId在个人记录中也使用FamilyId,如

        public static void Main(string[] args)
    {
        List<Family> Families = new List<Family>();
        Adults father = new Adults { Name = "Jim", Age = 34, Job = "Programmer", LicNumber = "2344454" };
        Adults father = new Adults { Name = "Amy", Age = 33, Job = "Nurse", LicNumber = "88888" };
        Family fam1 = new Family { Nickname = "Family One", FamilyId = 1, Father = father, Mother = mother };
    }

    private static void DisplayFamilyMemberInformation(Family familyMember)
    {
        Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
        Console.WriteLine("Prents : ");
        Console.WriteLine($"{fam1.father.name} - {fam1.father.job} - { fam1.father.licNumber}");
        Console.WriteLine($"{fam1.mother.name} - {fam1.mother.job} - {fam1.mother.licNumber}");
    }
确保你把孩子和家庭联系起来,就像这样

class Person
{
    public string name;
    public int age;
    public int FamilyId; // THIS !
}
。。这将允许您报告孩子们,如

        Person Child1 = new Person();
        Child1.name = "Bob";
        Child1.age = 3;
        Child1.FamilyId = fam1.FamilyId; // THIS ! connect it
        Children.Add(Child1);
静态无效报告(列出族,列出子项)
{
foreach(族中的变量族)
{
Console.WriteLine($“{family.昵称}({family.FamilyId})”;
控制台。WriteLine(“Prents:”);
Console.WriteLine($“{family.father.name}-{family.father.job}-{family.father.licNumber}”);
Console.WriteLine($“{family.mother.name}-{family.mother.job}-{family.mother.licNumber}”);
Console.WriteLine(“儿童”);
foreach(儿童中的儿童)
if(child.FamilyId==family.FamilyId)
Console.WriteLine(“Child:+Child.name”);
}
}

我希望我的方法采用“家族”类型参数。也就是说,如果我在这个例子中通过了FAM1,那么我希望这个方法可以打印所有FAM1的信息,我的样本所做的一切都是好的,所以如果你需要打印孩子,那么我可以更新Wow John!成功了:)太感谢你了,伙计!我花了大约2个小时在这个问题上,但解决方案是如此简单:D我很感激:)哇!没问题-如果你同意,就把它标记为已回答:-)好的luckHi Kevin,我感谢你的输入。我是一个完全的初学者,正在尽我最大的努力。我将检查您的代码并学习。再次感谢你。凯文,你能给正在学习C#基础知识的人一些建议吗?我喜欢你的代码是多么干净。提前感谢您~改进的最佳方法是继续努力,继续使用GitHub,尝试找到一些开源项目,您可以尝试为之做出贡献,也可以自己开始。你越是练习你的技能,你就会变得越好,我会练习我的代码KAtas!或者在我的例子中,代码TULI(TULI=Katas,但在韩国语ITF Taekwon do中)将所有的Console.WriteLine()移动到PrintFamily()并在foreach
        Person Child1 = new Person();
        Child1.name = "Bob";
        Child1.age = 3;
        Child1.FamilyId = fam1.FamilyId; // THIS ! connect it
        Children.Add(Child1);
    static void Report(List<Family> Families, List<Person> children)
    {
        foreach (var family in Families)
        {
            Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
            Console.WriteLine("Prents : ");
            Console.WriteLine($"{family.father.name} - {family.father.job} - { family.father.licNumber}");
            Console.WriteLine($"{family.mother.name} - {family.mother.job} - {family.mother.licNumber}");
            Console.WriteLine("Kids");
            foreach (Person child in children)
              if (child.FamilyId == family.FamilyId)
                 Console.WriteLine("Child: " + child.name);
         }
    }