C# Reflection - MemberInfo Class

MemberInfo class provides a quick look of all the members in a Type. Below is the same code to get all members of a Type. static void Main(string[] args) { Type type = typeof(Program); foreach(MemberInfo member in type.GetMembers()) { Console.WriteLine(member.Name + ":" + member.MemberType); } } MemberInfo MemberType property MemberInfo prov...

Continue Reading

C# Reflection - Type class

A Type class is an important class in C# reflection. Type class represents class types, interface types, array types, value types, enum types, type parameters, generic type definitions, and open/closed generic types. Type class helps you to find properties, methods, events, fields, and constructors declared in a type. It also provides us the Assembly in which the type is dec...

Continue Reading

BlockingCollection in C# - Introduction and Examples

BlockingCollection is a collection class which ensures thread-safety. Multiple threads can add and remove objects in BlockingCollection concurrently. It implements the producer-consumer pattern. In this pattern, there are two threads one is called producer and other is called consumer. Both threads share a common collection class to exchange data between them. BlockingC...

Continue Reading

ConcurrentBag in C# - Introduction and Examples

ConcurrentBag is one of the thread-safe collection classes introduced in .NET 4.0. ConcurrentBag allows you to store objects in unordered way. Contrary to ConcurrentDictionary class, it allows you to store duplicate objects. ConcurrentBag allows multiple threads to store the objects. It is optimized for scenarios where same thread act as producer and consumer. That means sam...

Continue Reading

ConcurrentDictionary in C# - Introduction, Examples

ConcurrentDictionary is one of five collection classes introduced in .NET 4.0. It exists in System.Collections.Concurrent namespace. ConcurrentDictionary is thread-safe collection class to store key/value pairs. ConcurrentDictionary can be used with multiple threads concurrently.  Without ConcurrentDictionary class, if we have to use Dictionary class with mult...

Continue Reading

ConcurrentQueue in C# - Introduction and Examples

C# ConcurrentQueue is a thread-safe collection class. It is introduced in .NET 4.0 with other concurrent collection classes. It provides a thread-safe First-In-First-Out (FIFO) data structure. You can read more about Queue here. ConcurrentQueue exists in System.Collections.Concurrent namespace ConcurrentQueue is a wrapper around generic Queue class. Queue...

Continue Reading

ConcurrentStack in C# - Introduction and Examples

ConcurrentStack is a thread-safe generic collection class introduced in .NET 4.0 framework. It provides a Last-In-First-Out (LIFO) data structure. You can read more about Stack here. ConcurrentStack is like wrapper around Stack class. Stack class is not thread-safe. ConcurrentStack provides thread-safety. It internally uses locking to synchronize different threads. Cr...

Continue Reading

C# Convert string to int

In C#, there are there ways for converting string to int. Each way has its own advantages and disadvantages. These ways are: int.Parse method int.TryParse method (Best way) Convert.ToInt32() method int.Parse method int.Parse method converts string input into integer. It takes the string input and returns int as output. static void Main(...

Continue Reading

C# Func

In Microsoft Framework class library, there are some built-in delegates. Action, Predicate and Func are some of them. I have explained Action and Predicate in my previous posts. Action and Predicate delegates has restrictions. Action delegate can take only parameters and does not return anything. Predicate delegate can take parameters but returns only boolean value. To ret...

Continue Reading

C# Generic Methods

In my previous post, I explained about Generics. C# also provides Generic Methods. We can create a method which defer the parameter data type until the method is called. These parameters are called Type parameters that means we can pass the actual data type later. Below is the example of Generic Method. static void Swap<T>(ref T input1, ref T input2) { T temp =...

Continue Reading

Join our newsletter and get an occasional email with a technology and dotnetpattern.com news update.