简体   繁体   English

C#返回ConcurrentBag的副本

[英]C# Return a Copy of ConcurrentBag

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();
            }


        }

    }
}

I have a Message class where a user could leave a message for more than one person. 我有一个Message类,用户可以为多个人留言。 I have a getter for it, however, is returning a ConcurrentBag<> the way I've done proper practice? 我有一个吸气剂,但是,我正在以正确的做法回归ConcurrentBag <> If not, how do i return the ConcurrentBag<> so I can loop through it and display it? 如果没有,我如何返回ConcurrentBag <>所以我可以循环并显示它?

ConcurrentBag<T> is an IEnumerable<T> . ConcurrentBag<T>是一个IEnumerable<T> You can loop through it as usual. 你可以照常循环播放它。 However, as this is a thread safe collection, there are performance concerns to using it. 但是,由于这是一个线程安全的集合,因此使用它会产生性能问题。

If you want to get rid of the performance impact while looping, call ToArray on it and return the new array instead. 如果您想在循环时摆脱性能影响,请在其上调用ToArray并返回新数组。

    public IEnumerable<Employee> ReturnMessageFor
    {
        get
        {

            return messageFor.ToArray();
        }

    }

It's not clear to me what you are trying to accomplish. 我不清楚你想要完成什么。

Are you trying to externalize the Bag for all operations? 您是否尝试将Bag外部化为所有操作? Because that's what you did... 因为那就是你做的......

If you want to externalize something you can iterate over you should either return the Bag as IEnumerable or return an array or a list copied from the Bag. 如果你想要外化你可以迭代的东西,你应该将Bag作为IEnumerable返回,或者返回一个数组或从Bag复制的列表。

Either way it's safe to iterate over. 无论哪种方式,迭代都是安全的。 Might not be the best in terms of performance, but that's another question. 在性能方面可能不是最好的,但这是另一个问题。

    // Option 1
    public IEnumerable<Employee> ReturnMessageFor
    {
        get
        {
            return messageFor;
        }
    }

    // Option 2
    public Employee[] ReturnMessageFor
    {
        get
        {
            return messageFor.ToArray();
        }
    }

Notes: 笔记:

  • You might want to make messageFor readonly (in the code you posted it is readonly). 您可能想要readonly发送message(在您发布的代码中只读取)。
  • Remember that a ConcurrentBag allows you to safely iterate over a snapshot of the collection in a thread safe manner, but it does not lock the items in the collection. 请记住,ConcurrentBag允许您以线程安全的方式安全地迭代集合的快照,但它不会锁定集合中的项目。

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

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