C# 如何在c sharp中向列表中添加class属性的对象

C# 如何在c sharp中向列表中添加class属性的对象,c#,properties,C#,Properties,我无法将类实例正确添加到列表中。它只添加最后一个对象。当我调试时,列表词汇表只显示添加最后一个类实例。所以通过第二个循环,它有两个第二个对象的条目,通过第三个循环,它有三个第三个对象的条目。我做错了什么。这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace myVocabulary {

我无法将类实例正确添加到列表中。它只添加最后一个对象。当我调试时,列表词汇表只显示添加最后一个类实例。所以通过第二个循环,它有两个第二个对象的条目,通过第三个循环,它有三个第三个对象的条目。我做错了什么。这是我的密码

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

namespace myVocabulary
{
    public class Word
    {
        string _original_word;
        string _translated_word;

        public string Original_Word
        {
            get
            {
                return this._original_word;
            }
            set
            {
                this._original_word = value;
            }
        }

        public string Translated_Word
        {
            get
            {
                return this._translated_word;
            }
            set
            {
                this._translated_word = value;
            }
        }
    }
}

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

namespace myVocabulary
{
    class Program
    {
        static List<Word> vocabulary = new List<Word>();
        static void Main(string[] args)
        {
            Word entry = new Word();

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Enter word");
                entry.Original_Word = Console.ReadLine();
                Console.WriteLine("Enter Translation");
                entry.Translated_Word = Console.ReadLine();
                vocabulary.Add(entry);
            }           

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间我的词汇表
{
公共类词
{
字符串-原始单词;
字符串-翻译的单词;
公共字符串原字
{
得到
{
返回此文件。_原件_单词;
}
设置
{
这个。_原始_单词=值;
}
}
公共字符串
{
得到
{
返回此项。_已翻译的_单词;
}
设置
{
这个。翻译的单词=值;
}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间我的词汇表
{
班级计划
{
静态列表词汇表=新列表();
静态void Main(字符串[]参数)
{
词条=新词();
对于(int i=0;i<5;i++)
{
Console.WriteLine(“输入单词”);
entry.Original_Word=Console.ReadLine();
Console.WriteLine(“输入翻译”);
entry.Translated_Word=Console.ReadLine();
添加(条目);
}           
}
}
}

试试这个-您需要在循环的每次迭代中创建一个新的
Word
对象,否则您只是重复覆盖同一个实例

namespace myVocabulary
{
    class Program
    {
        static List<Word> vocabulary = new List<Word>();
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Word entry = new Word();
                Console.WriteLine("Enter word");
                entry.Original_Word = Console.ReadLine();
                Console.WriteLine("Enter Translation");
                entry.Translated_Word = Console.ReadLine();
                vocabulary.Add(entry);
            }           

        }
    }
}
名称空间我的词汇表
{
班级计划
{
静态列表词汇表=新列表();
静态void Main(字符串[]参数)
{
对于(int i=0;i<5;i++)
{
词条=新词();
Console.WriteLine(“输入单词”);
entry.Original_Word=Console.ReadLine();
Console.WriteLine(“输入翻译”);
entry.Translated_Word=Console.ReadLine();
添加(条目);
}           
}
}
}

试试这个-您需要在循环的每次迭代中创建一个新的
Word
对象,否则您只是重复覆盖同一个实例

namespace myVocabulary
{
    class Program
    {
        static List<Word> vocabulary = new List<Word>();
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Word entry = new Word();
                Console.WriteLine("Enter word");
                entry.Original_Word = Console.ReadLine();
                Console.WriteLine("Enter Translation");
                entry.Translated_Word = Console.ReadLine();
                vocabulary.Add(entry);
            }           

        }
    }
}
名称空间我的词汇表
{
班级计划
{
静态列表词汇表=新列表();
静态void Main(字符串[]参数)
{
对于(int i=0;i<5;i++)
{
词条=新词();
Console.WriteLine(“输入单词”);
entry.Original_Word=Console.ReadLine();
Console.WriteLine(“输入翻译”);
entry.Translated_Word=Console.ReadLine();
添加(条目);
}           
}
}
}

没问题-乐意帮忙没问题-乐意帮忙