C# Multithreaded Resource access – Concurrent Collections

Concurrent collections are a feature added in .Net 4.0 and allow developers to create thread safe collections without worrying as much as they had to worry with Generic Collections.
They do not remove any possible concern the developer should have while working with resources that are accessed from multiple threads, but removes many of the common ones.

Continue reading C# Multithreaded Resource access – Concurrent Collections

C# Typecasting: Use ‘as’ instead of the normal Cast

csharpWe are all used to casting from one type to another in different languages that we use. In all C-like languages (C,C++,C#,Java etc) we use this simple format.

CustomObject a = new CustomObject();
object b = (object)a;

Okay I am using an overly trivial example here but I think you get the point.

Now imagine the following example. We are given an enumerable formĀ of data through the network, in JSON format let’s say, We are aware that this enumerable has different forms of data, some are objects, some are strings, some are ints etc. Bear with me here, I know this is a crazy scenario which has no meaning but let’s suspend our disbelief for a moment and go through with it. So let’s say we have the following Enumerable

Continue reading C# Typecasting: Use ‘as’ instead of the normal Cast

Out vs Ref in C#

csharpAll languages, or at least most, have some way of passing a value by reference. When we wish to return more than one values from a method or simply if we want to use the classic value swap paradigm then we need to pass values by reference to our method. C has pointers (which in fact you are still passing the pointer to your method by value, but in fact that pointer has a reference to the variable you want to change) Visual basic has the ByRef argument and many other languages have their own way of doing this.
Continue reading Out vs Ref in C#