简体   繁体   English

在c#中显示不同类型的列表的正确方法是什么?

[英]What is the proper way to display a List of different types in c#?

Ok, so basically I am creating a project that requires to Create, Read, Update, and Delete. 好的,所以基本上我正在创建一个需要创建,读取,更新和删除的项目。 However, I am not referring to a database server, but a List of type. 但是,我不是指数据库服务器,而是类型列表。 I am trying to figure out how to display the list of Dvd type. 我试图弄清楚如何显示Dvd类型的列表。 My first goal is to be able to display the list of DVDs before starting to implement the update and delete method. 我的第一个目标是能够显示DVD列表,然后再开始执行update和delete方法。 Here are my classes: 这是我的课程:

DvdController.cs DvdController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;
using DvdManager.Data;

namespace DvdManager.Controllers
{
    public class DvdController
    {
        List<Dvd> Dvds = new List<Dvd>(10);       


        public void Run()
        {
            Console.WriteLine("Welcome To Dvd Manager");

            CreateDvd();
        }

        private void CreateDvd()//Create
        {
            Dvds.Add(new Dvd(0, "Batman", 2010, "Bruce", 4));
            Dvds.Add(new Dvd(1, "Superman", 2009, "John", 4));
            Dvds.Add(new Dvd(2, "Wonderwoman", 2012, "Omar", 4));
            DisplayDvds();
        }

        private void DisplayDvds() //Read List<Dvd> dvds
        {
            for (int i = 0; i < Dvds.Count; i++)
            {
                Console.WriteLine(Dvds[i]);
            }
        }

        private void SearchDvds() //List
        {

        }

        private void EditDvd(int id, Dvd dvd) //Update
        {

        }

        private void RemoveDvd(int id) //Delete
        {


        }
    }
}

Dvd.cs Dvd.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;    

namespace DvdManager.Models
{
    public class Dvd
    {
        public Dvd(int id, string title, int releaseyear, string director, float rating)
        {
            Id = id;
            Title = title;
            ReleaseYear = releaseyear;
            Director = director;
            Rating = rating;
        }

        int Id { get; }
        string Title { get; set; }
        int ReleaseYear { get; set; }
        string Director { get; set; }
        float Rating { get; set; }


    }
}

The problem is that it displays: 问题是它显示:

DvdManager.Models.Dvd DvdManager.Models.Dvd

But, what I want it to do is display its value by Id, Title, ReleaseYear, Director, Rating. 但是,我要它执行的操作是按ID,标题,ReleaseYear,主任,等级显示其值。 How do I succeed in doing this? 我如何成功做到这一点?

You're printing the default string representation of your class as returned by its ToString . 您正在打印类的默认字符串表示形式,该表示形式由其ToString返回。 If you want a different output, you can override ToString yourself. 如果需要其他输出,则可以自己覆盖ToString Eg: 例如:

public override string ToString()
{
   return string.Format
              ("DVD [Id: {0}; Title: {1}; ReleaseYear: {2}; Director: {3}; Rating: {4}]",
               Id, Title, ReleaseYear, Director, Rating);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM