gpt4 book ai didi

C# 返回 ConcurrentBag 的副本

转载 作者:太空狗 更新时间:2023-10-29 17:50:34 24 4
gpt4 key购买 nike

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

namespace Crystal_Message
{
class Message
{
private int messageID;
private string message;
private ConcurrentBag <Employee> messageFor;
private Person messageFrom;
private string calltype;

public Message(int iden,string message, Person messageFrom, string calltype, string telephone)
{
this.messageID = iden;
this.messageFor = new ConcurrentBag<Employee>();
this.Note = message;
this.MessageFrom = messageFrom;
this.CallType = calltype;
}

public ConcurrentBag<Employee> ReturnMessageFor
{
get
{

return messageFor;
}

}

public int MessageIdentification
{
get { return this.messageID; }

private set
{
if(value == 0)
{
throw new ArgumentNullException("Must have Message ID");
}

this.messageID = value;
}

}

public string Note
{
get { return message; }

private set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Must Have a Message");

}
this.message = value;


}

}

public Person MessageFrom
{
get { return messageFrom; }

private set
{
this.messageFrom = value;
}

}

public string CallType
{
get { return this.calltype; }

private set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentNullException("Please specify call type");
}

this.calltype = value;

}

}

public void addEmployee(Employee add)
{
messageFor.Add(add);
}


public override string ToString()
{
return "Message: " + this.message + " From: " + this.messageFrom + " Call Type: " + this.calltype + " For: " + this.returnMessagefor();
}

private string returnMessagefor()
{
string generate="";

foreach(Employee view in messageFor)
{
generate += view.ToString() + " ";
}

return generate;
}

public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}

Message testEquals = obj as Message;

if((System.Object)testEquals == null)
{
return false;
}

return (this.messageID == testEquals.messageID) && (this.message == testEquals.message) && (this.messageFor == testEquals.messageFor) && (this.messageFrom == testEquals.messageFrom) && (this.calltype == testEquals.calltype);

}

public bool Equals(Message p)
{
if ((Object)p == null)
{
return false;
}

return (this.messageID == p.messageID) && (this.message == p.message) && (this.messageFor == p.messageFor) && (this.messageFrom == p.messageFrom) && (this.calltype == p.calltype);

}

public override int GetHashCode()
{


unchecked
{
return this.messageID.GetHashCode() * 33 ^ this.message.GetHashCode() * 33 ^ this.messageFor.GetHashCode() * 33 ^ this.messageFrom.GetHashCode() * 33 ^ this.calltype.GetHashCode();
}


}

}
}

我有一个 Message 类,用户可以在其中为多个人留言。我有一个 setter/getter ,但是,是否按照我正确练习的方式返回一个 ConcurrentBag<>?如果没有,我如何返回 ConcurrentBag<> 以便我可以遍历它并显示它?

最佳答案

ConcurrentBag<T>是一个 IEnumerable<T> .您可以像往常一样遍历它。但是,由于这是一个线程安全的集合,因此使用它存在性能问题。

如果您想在循环时摆脱性能影响,请调用 ToArray在它上面并返回新数组。

    public IEnumerable<Employee> ReturnMessageFor
{
get
{

return messageFor.ToArray();
}

}

关于C# 返回 ConcurrentBag 的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com