Collections Best Practices
In the .NET Framework, a lot of effort went into creating collection classes that are powerful and that address a variety of needs and styles. These collections are simple to use, intuitive, and have adequate performance—all very important characteristics. In this month’s installment, I’ll look at collections in .NET, how they work, when to use them, and some best practices.
IEnumerable and Yield in .NET 2.0
IEnumerable is an interface that has allowed for the creation of collections which can be iterated with a foreach loop since .NET 1.0. Well, with the introduction of generics and the yield keyword in .NET 2.0 things have gotten a whole lot simpler. This article will demonstrate how to use the generic IEnumerable<> interface along with the new yield keyword to create collections on the fly, without the need to implement all of the boiler plate code that was previously required.
Digging into IDisposable
One of the major productivity benefits that the common language runtime (CLR) offers developers of managed code is that the garbage collector (GC) makes sure any memory allocated on the managed heap is cleaned up after it is no longer needed. Developers are thus saved countless hours of debugging difficult problems arising from memory leaks, from trying to use released memory, and from double-freeing memory. However, while the garbage collector is great at making sure that memory does not leak, it doesn’t have any knowledge about other resources that need to be freed. For instance, the garbage collector doesn’t know how to close a file handle or how to release memory allocated outside of the managed heap with an API such as CoAllocTaskMem.
One problem with this system is that the garbage collector does not run deterministically and, as a result, your object may not be finalized for a long time after the last reference to it has gone away.
To avoid having to wait an indeterminate amount of time for the garbage collector to act, types that own resources should implement the IDisposable interface; then consumers of that type can release those resources in a timely manner.
Multi Index Container for C# 2.0
Some of us have had a need for associative containers having more than one index, sometimes we also need associative containers with non-unique keys. Boost.MultiIndex provides a sophisticated library of container having more than one indexing mechanism. Need for similar data structure in C# (.Net) and introduction of generics in .Net pushed me to design multi-index container similar concept.
Interfaces in C# for beginners
Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference. Before things start getting difficult let me start using simple and short examples to explain the concept of interfaces..
Comparing Values for Equality in .NET: Identity and Equivalence
An article clarifying the various ways of comparing two values for equality in .NET
C# Language Infrequent Methods,Operators and Techniques
Here I am providing a basic overview of some things which are included in .NET 2.0 but only few developers are using them. I am not saying that nobody knows about these functions, operators or facilities but only few developers are using these based on their requirements.
Asynchronous design patterns.
This article describes the asynchronous design pattern, it's implementation, it's limitations and ways to improve the situation in certain circumstances.
HyperDescriptor: Accelerated dynamic property access
.NET provides flexible data-binding and runtime property access, but by default this is via reflection an is known to be relatively slow. This article uses the power of Reflection.Emit to provide a pre-compiled (and much accelerated) implementation for reflection properties, and demonstrates the use of TypeDescriptionProvider to dynamically apply this implementation to types.
A lot of technical details are included, but this code is all provided in the source; as a consumer you have to do almost nothing. Really. You may wish to jump ahead to the usage scenarios, then dip back if you want to know what makes it all work.
Generics Explained (.NET Framework version 2.0)
This article discusses generics. Generics is a new feature included in .NET version 2.0.