簡體   English   中英

C# 如何循環遍歷列表並將字符串添加到列表中?

[英]C# How to loop through the list and add string into the list?

這是我第一次在這里發布問題。 我可以知道如何遍歷列表並將字符串添加到列表中嗎?

這是我的代碼,它在單元測試中失敗了。

using System;
using System.Collections.Generic;

namespace ItemTracker
{
    public class Book: Item
    {
        private List<string> _authors;
        private string _title;
        private int _yearPublished; 

        public Book(string id, double price, Category category, List<string> authors, string title, int 
                   yearPublished):base(id, price, category)
        {
            _authors = new List<string>();
            _authors = authors;
            foreach (string a in _authors)
            {
                _authors.Add(a);
            }
            _title = title;
            _yearPublished = yearPublished;
        }

        public List<string> Authors
        {
            get {return _authors;}
            set { _authors = value;}
        }

        public override string View() 
        {
            return "Author:" + _authors + "\nTitle:" + _title + "\nYear Published:" + _yearPublished;
        }
    }
}

這是我的單元測試:

    [Test()]
    public void TestBook() 
    {
        List<string> a = new List<string>();
        a.Add("J.K. Rowling");
        Book book = new Book("B1001", 39.9, Category.Book, a,"Harry Potter", 1997);
        Assert.IsTrue(book.View() == "Author: J.K. Rowling" + "\nTitle: Harry Potter" + "\nYear 
               Published: 1997");
    }

您正在打印 object 名稱List<string>而不是authors的姓名。 這是您的單元測試失敗的原因之一。 試試這個。

    public override string View() 
    {
        var allAuthors = string.Join( " ", _authors );
        return "Author: " + allAuthors + "\nTitle: " + _title + "\nYear Published: " + _yearPublished;
    }

正如其他人也指出的那樣,您應該從ctor中刪除該循環並使用:

_authors = authors;

或者,如果您想要一份副本:

_authors = new List<string>( authors );

無需循環。這也應該阻止您獲得InvalidOperationException ,因為您在iterating List<string>時正在修改它。

此外,您正在對易碎的字符串進行直接比較。 如果您在某處添加額外的空格,則string比較將失敗..

關於構造函數。 如果您想遍歷作者參數並將每個條目添加到 _authors 私人列表中,只需刪除_authors = authors;
並遍歷authors而不是_authors
像這樣:

public Book(string id, double price, Category category, List<string> authors, string title, int yearPublished) : base(id, price, category)
        {
            _authors = new List<string>();
            foreach (string a in authors)
            {
                _authors.Add(a);
            }
            _title = title;
            _yearPublished = yearPublished;
        }

此外, View()方法不會返回您期望它返回的字符串。
它只使用從object class 派生的ToString()方法。
因此,您需要顯式循環_authors列表並首先構建字符串:

        public override string View() 
        {
            string authorString = "";
            foreach(var a in _authors)
            {
               authorString += $"{a} ";
            }
            return "Author:" + authorString + "\nTitle:" + _title + "\nYear Published:" + _yearPublished;
        }

或構建您自己的Authors class 並以您的方式實現ToString()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM