首页 > 解决方案 > 如何在 C# 中打印列表对象的所有属性?我想在案例 2 中打印 boklista 的所有对象

问题描述

using System;
using System.Collections.Generic;

namespace bokhylla
{
//*******************************************************************
class Bok
{
    private string title = "";
    private string skribent = "";
    private string typ = "";

    public string Title
    {
        get { return title; }

        set { title = value; }
    }
    public string Skribent
    {
        get { return skribent; }

        set { skribent = value; }
    }
    public string Typ
    {
        get { return typ; }

        set { typ = value; }
    }


}
class Roman : Bok
{
    private int antalSidor = 0;
    public int AntalSidor
    {
        get { return antalSidor; }

        set { antalSidor = value; }
    }
    public Roman()
    {
        Typ = "Roman";
    }
}
class Tidskrift : Bok
{
    private string manad = "";
    public string Manad
    {
        get { return manad; }

        set { manad = value; }
    }
    public Tidskrift()
    {
        Typ = "Tidskrift";
    }

}
class NovellSamling : Bok
{
    private int antalNovell = 0;
    public int AntalNovell
    {
        get { return antalNovell; }

        set { antalNovell = value; }
    }
    public NovellSamling()
    {
        Typ = "Novellsamling";
    }
}
//*************************************************************************
class Program
{
    static List<Bok> BokLista = new List<Bok>();
    static void Main(string[] args)
    {
        bool running = true;
        while (running)
        {
            Console.WriteLine("\tHej och välkommen!");
            Console.WriteLine("\t[1] Registera ny bok");
            Console.WriteLine("\t[2] Visa böcker");
            Console.WriteLine("\t[3] Avsluta");

            if (Int32.TryParse(Console.ReadLine(), out int val))
            {
                switch (val)
                {
                    case 1:
                        Console.WriteLine("\tÄr boken en [1]roman, [2]novellsamling eller [3]tidskrift");
                        Int32.TryParse(Console.ReadLine(), out int boktyp);
                        if (boktyp == 1)
                        {
                            Console.WriteLine("\tSkriv bokens title: ");
                            Roman bok1 = new Roman();
                            bok1.Title = Console.ReadLine();
                            Console.WriteLine("\tSkriv bokens skribent: ");
                            bok1.Skribent = Console.ReadLine();
                            Console.WriteLine("\tSkriv bokens antal sidor: ");

                            bool siffra1 = false;
                            while (!siffra1)
                            {
                                if (Int32.TryParse(Console.ReadLine(), out int sidor))
                                {
                                    bok1.AntalSidor = sidor;
                                    siffra1 = true;
                                }
                                else
                                {
                                    Console.WriteLine("\tSkriv gärna antal sidor med siffror: ");
                                }
                            }
                            BokLista.Add(bok1);
                        }

                        if (boktyp == 2)
                        {
                            Console.WriteLine("\tSkriv bokens title: ");
                            NovellSamling bok2 = new NovellSamling();
                            bok2.Title = Console.ReadLine();
                            Console.WriteLine("\tSkriv bokens skribent: ");
                            bok2.Skribent = Console.ReadLine();
                            Console.WriteLine("\tSkriv bokens antal novell: ");

                            bool siffra2 = false;
                            while (!siffra2)
                            {
                                if (Int32.TryParse(Console.ReadLine(), out int novell))
                                {
                                    bok2.AntalNovell = novell;
                                    siffra2 = true;
                                }
                                else
                                {
                                    Console.WriteLine("\tSkriv gärna antal novell med siffror: ");

                                }
                            }
                            BokLista.Add(bok2);
                        }
                        break;

                    **case 2:
                        foreach (var item in BokLista)
                        {
                            Console.WriteLine(item);
                        }**


                        break;



                }
            }
            else
            {

            }

        }


    }
}

}

//如何在c#中打印列表对象的所有属性?我想在案例 2 中打印 boklista 的所有对象。

标签: c#listclassobjectprinting

解决方案


您必须使用点 ( .) 运算符编写每个属性:

...
case 2:
      foreach (var item in BokLista)
           {
              Console.WriteLine(item.Title);
              Console.WriteLine(item.Skribent);
              Console.WriteLine(item.Typ);
           }

推荐阅读