close nav

close nav


Blog Image

ConcurrentBag In C#  

  • by Sriman
  • 17 Jul, 2019
  • in C#,
  • 0 comments
  • 989 views

In this blog, you will learn about C# ConcurrentBag class and how to use ConcurrentBag in C# code.ConcurrentBag is one of the thread-safe collection classes introduced in .NET 4.0. ConcurrentBag allows you to store objects in unordered way.

ConcurrentBag

 
ConcurrentBag is a collection class and is declared in System.Collections.Concurrent namespace. It allows generic data to be stored in unordered from. Bags support duplicates. It is a thread-safe class and allows multiple threads to use it.
 

How to initialize Concurrent Bag



ConcurrentBag<int> bag = new ConcurrentBag<int>();

It will create a empty list of int type.



ConcurrentBag<int> bag = new ConcurrentBag<int>(lst);

The Add() method is used to add items to the bag. The items can be any object. In this case, int. The following code snippet adds three items to the collection.



List<int> lst = new List<int>();  
lst.Add(5);  
lst.Add(6);  
lst.Add(7);
You may add more items to the collection.


lst.Add(10);  
lst.Add(14);

Count

Concurrent bag provides the Count property that returns the number of items stored in the collection. The following code snippet reads the number of items


int cnt = bag.Count;

IsEmpty

The IsEmpty property returns true if the bag is empty. Else it returns false.


bool isEmpty = basg.IsEmpty;

Retrieval of Data from the Concurrent Bag provides two ways to retrieve the data.
  1. TryTake
  2. TryPeek

TryTake

Syntax - bool TryTake (out T result)
 
It will return the first element of the concurrent bag. The return value is returned as an out parameter. The method also removes the item from the bag


int item;  
bool result = bag.TryTake(out item);

TryPeek

Syntax - bool TryPeek(out T result)
 
It will return the first element of the concurrent bag found in the collection as an out parameter. Unlike TryTake, this method does not remove the item.
 
Example


int item;  
bool result = bag.TryPeek(out item);

Array

If you have returned the concurrent bag data in an array, then you have to use the ToArray method.



var result = bag.ToArray();

Clear

The Clear() method removes all items from the bag.


bag.Clear();

Once called, the bag will be empty.
 

CopyTo

The CopyTo method copies the ConcurrentBag<T> elements to an existing one-dimensional Array, starting at the specified array index.
 
Syntax - CopyTo(T[] array, int index)

Example



int[] array = new int[10];  
bag.CopyTo(array, 2);


About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.