我想在 observablecollection 中复制一个列表项。当我做:
TreasureCards[TreasureCards.Count - 1] = TreasureCards[CardPosition];
Run Code Online (Sandbox Code Playgroud)
它创建了特定列表项的副本,但随后它们在我的 UI 中链接。因此,如果我更改新重复项的名称,它会更改原始名称。我知道我可以一个一个地完成每个属性(见下文),但有没有办法只复制整个项目?
TreasureCards[TreasureCards.Count - 1].Name = TreasurecCards[CardPosition].Name;
TreasureCards[TreasureCards.Count - 1].Type= TreasurecCards[CardPosition].Type;
// etc
Run Code Online (Sandbox Code Playgroud) 我有一个名为的对象post,我声明了一个名为newPosttype post对象的新变量
var newPost = post
但是当我修改post对象的任何属性时,表示对象的Modified Influences属性newPost.
我该如何解决这个问题?
根据这个答案,我成功复制了我的对象.
但是,性能是我当前项目中的最高优先级.所以我做了一些单元测试并检查了执行所需的时间.结果是平均2秒,而我期待最大200毫秒!我发现90%的时间花在了Object复印机上.
有没有办法提高这种表现?还有其他任何复制对象的方法吗?有谁知道复制数组或列表有多快?我会考虑使用这样的.
请考虑此代码
some v1=new some();
v1.x=10;
some v2=v1;
v1.x=15;
Console.Write(v2.x);//Show 15
Run Code Online (Sandbox Code Playgroud)
当我在v1上更改x属性时为什么要在v2上更改x的值?
我有一个带有两个Datalog类变量的Form
public partial class ModifyDataForm : Form
{
public DataLog DLog;
private DataLog copyCurrent;
public ModifyDataForm(DataLog log, int selectIndex = 0)
{
InitializeComponent();
DLog = (DataLog)log.Clone();
copyCurrent = (DataLog)log.Clone();
}
}
Run Code Online (Sandbox Code Playgroud)
当我更新DLog的值时,copyCurrent的值也会改变,为什么?
我更新变量的函数如下
private void smooth_Click(object sender, EventArgs e)
{
int NValues; int POrder;
if (getSmoothParameters(out NValues, out POrder))//parameters are valid
{
float[] yvalues = DataLog.convertStringArrayToFloats(DLog.Data[labelIndex]);
float[] newyvalues = Filters.smooth.SavitzkyGolay(yvalues, NValues, POrder);
//I am updating the values of DLog here,
//but the values of copyCurrent also changes
DLog.Data[labelIndex] = Array.ConvertAll(newyvalues, …Run Code Online (Sandbox Code Playgroud) 我的 Web 应用程序正在缓存对象列表,以避免一遍又一遍地从数据库中获取它们。在一页上,从缓存中获取列表,然后将其他对象添加到列表中并显示在数据绑定控件中。这些额外的对象并不意味着要被缓存,但仍然以某种方式缓存。
当您使用 Cache.Get() 方法时,它是否应该返回对象引用或该对象的副本?
我想在VB.NET中实现一个扩展方法,它将克隆类型为T的对象.
说,我想要
Dim cust1 as New Customer() //...
Dim cust2 as New Customer() //...
cust2 = cust1.Clone()
''
' My extension method '
''
<Runtime.CompilerServices.Extension()> _
Public Function Clone(Of T As {Class, New})(ByVal obj As T) As T
Dim objClone As New T
' clonning stuff '
' objClone = CType(GetAnObjClone(), T) '
Return objClone
End Function
Dim c As MyObject
Dim cc As MyObject = c.Clone() ' does work!!! cool... '
Run Code Online (Sandbox Code Playgroud)
要删除的问题.
List<Person> allPersons= ReadallPersons()
Person aPerson=allPersons[0];
aPerson.Name="test1";
allPersons.Add(aPerson)
Run Code Online (Sandbox Code Playgroud)
allPersons有一个单人对象,我将该人物对象分配给'aPerson'对象.只需将人名重命名为'test1'并再次将其添加到列表中.
现在,如果我们检查列表,则两个人的对象的名称都被指定为'test1'.
这有什么问题?
我们怎么能解决这个问题呢?
我有结构
struct User
{
public int id;
public Dictionary<int, double> neg;
}
List<User> TempUsers=new List<users>();
List<User> users = new List<User>();
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行此代码时
TempUsers=users.ToList();
TempUsers[1].neg.Remove(16);
Run Code Online (Sandbox Code Playgroud)
用户的否定字典aslo删除值为16的键
我有点困惑为什么这里的逻辑不起作用,我觉得我已经盯着这段代码太久了,以至于我在这里遗漏了一些东西。我有这个方法被另一个方法调用:
private async Task<bool> Method1(int start, int end, int increment, IEnumerable<ModelExample> examples)
{
for (int i = start; i<= end; i++)
{
ModelExample example = examples.Where(x => x.id == i).Select(i => i).First();
example.id = example.id + increment; //Line X
// do stuff
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我调试了上面的代码,似乎当“Line X”执行时,不仅 example.id 单独更改,而且现在列表“examples”中的示例已更新为新的 id 值。我不知道为什么?我希望列表“examples”在整个 for 循环中保持不变,我很困惑为什么更新 example.id 的值也会在列表中更新它?
(即,如果“Line X”之前的列表有一个 id = 0 的条目,在“Line X”之后,同一条目的 id 更新为 1,我如何在此处保持变量“examples”不变?)
任何帮助表示赞赏,谢谢。