简体   繁体   English

如何仅将 class 的值插入另一个列表 class C#?

[英]How to Insert only values of a class into another list class C#?

image that you defined a class as below;您定义 class 的图像如下;

public class Liste
{
    public int valueInt { get; set; }
    public List<string> valueString = new List<string>();
}

and I defined a varible which is also a list;我定义了一个变量,它也是一个列表;

  public List <Liste> oray2 = new List <Liste>();
  public Liste oray3 = new Liste();

I would like to add value to a oray2 List manually,我想手动为oray2列表添加价值,

        oray3.valueInt = 10;
        oray3.valueString.Add("Text1");
        oray3.valueString.Add("Text2");
        oray3.valueString.Add("Text3");

        oray2.Add(oray3);

        oray3.valueString.Remove("Text2");

This also effects oray2 List.这也会影响 oray2 列表。 So it seems所以它看起来

oray2.Add(oray3);

is not adding values to oray2 class but oray2[0] seems linked to oray3 class.没有向oray2 class 添加值,但oray2 [0] 似乎与oray3 class 相关联。

So What is the best and efficient way to add values of oray3 to oray2 without a link between oray3 and oray2[0] so resulting changing in oray3 will not affect oray2 list values?那么在oray3和oray2 [0]之间没有链接的情况下,将oray3的值添加到oray2的最佳和有效方法是什么,因此导致oray3的更改不会影响oray2列表值?

My best solution;我最好的解决方案;

oray3=null;

or或者

oray3=new Liste();

worked like a charm.像魅力一样工作。

I see two choices: set oray3 to a new Liste object and set its properties rather than reuing the reference, or copy oray3 to a new Liste object and add that to the list.我看到两个选择:将oray3设置为newListe object 并设置其属性而不是重新使用引用,或者将oray3复制到新的Liste object 并将其添加列表中。

It's not clear why you're reusing oray3 in the first place to kniw which of those is better.目前尚不清楚为什么您首先要重用oray3以了解其中哪个更好。

Just make Liste a struct instead of a class ie只需使Liste成为struct而不是class

public struct Lex
{
    public Lex() 
    { 

    }

    public int valueInt { get; set; } = 0;
    public List<string> valueString = new List<string>();
}

Classes in C# a reference type and so are passed by reference whereas structs are value types and so are passed by value. C# 中的类是引用类型,因此按引用传递,而结构是值类型,因此按值传递。

Reference types pass around a reference to the declared variable so when they are assigned to another variable and changed, both variables are updated.引用类型传递对已声明变量的引用,因此当它们被分配给另一个变量并发生更改时,两个变量都会更新。

Value types simply copy their values when assigned so changes only occur on the variable that the changes were made to.值类型在赋值时简单地复制它们的值,因此更改只发生在进行更改的变量上。

More info on value type and reference types here: https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type此处有关值类型和引用类型的更多信息: https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type

I think this is what you want to do.我认为这就是你想要做的。 This way a new oray3 object is created each time you call the GetOray3() .这样每次调用GetOray3()时都会创建一个新的oray3 object 。

List<Liste> oray2 = new List<Liste>();

oray2.Add(GetOray3());
oray2.Add(GetOray3());
oray2.Add(GetOray3());

static Liste GetOray3()
{
    Liste oray3 = new Liste();

    oray3.valueInt = 10;
    oray3.valueString.Add("Text1");
    oray3.valueString.Add("Text2");
    oray3.valueString.Add("Text3");

    return oray3;
}

public class Liste
{
    public int valueInt { get; set; }
    public List<string> valueString = new List<string>();
}

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

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