c# - 如何在 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 的所有对象。
解决方案
您必须使用点 ( .
) 运算符编写每个属性:
...
case 2:
foreach (var item in BokLista)
{
Console.WriteLine(item.Title);
Console.WriteLine(item.Skribent);
Console.WriteLine(item.Typ);
}
推荐阅读
- php - Codeigniter4 - 默认情况下将数组数据返回为 JSON 而不是 XML
- google-apps-script - Google Apps 脚本在编码时不显示方法
- python - 使用枚举遍历多个列表
- python-3.x - 在 sqlite3 和 python Tkinter 中插入电子邮件字符串时出错
- javascript - animate.css 不会连续触发两次
- javascript - 如果(mousePressed)不工作(Processing.js)
- sql - DB2 SQL 联合斗争
- javascript - 如何将变量从 webview Javascsript 传递到 Xamarin Forms 代码?
- google-apps-script - 获取要导出的特定范围
- javascript - 尝试使用 css 和 javascript 创建轮盘赌