Archives for C# – the language - Page 2
C#, .NET Exception Handling Best Practices
The Problem Statement You have just started working on an existing codebase – an n-Tier project - and noticed that the Exception Handling framework wasn’t as well defined as you…
New interfaces for implementing Publish-Subscribe in .NET
You know you are a techie at heart if you find yourself getting excited at the introduction of a couple of new interfaces. .NET introduces IObservable (Publisher) and IObserver (Subscriber).…
Generating Random Numbers C#
When tasked with generating Random numbers, some folks use a static method such as that shown below: Code Snippet static Random rn = new Random(); static int ReturnNextRandom(int maxIndex) {…
Swapping without a temp variable
Everyone is familiar with the basic swap – using a temporary variable static void RegularSwapUsingTempVariable(ref int a, ref int b) { int temp = a; a = b; b =…
Floats and Doubles can lose you money…use Decimals or a custom Money class instead
Ariane 5 Rocket Launcher Crash In 1996, the European Space Agency lost a valuable shuttle just 37 seconds into the launch sequence. After a deep dive into the root cause…
LINQ versus looping–Performance
Looping and Iterating is bad for performance right? So – a loop like the one below should be bad. for (int i = 0; i <= iterations; i++) { foreach…
Use Funcs only if you want results back
If you are trying to encapsulate a method that returns a value, Funcs are ideal. However, what if your method returns void? Func will not work (Funcs were designed to…
Delegates in C# – pre-lambda and post-lambda expressions
Delegates are amongst the most powerful constructs in the C# language (Full Solution Download at the end of this article). Some uses of delegates in c# include : Delaying invocation(calling)…
The Under-Appreciated Hash Function–Illustrated using a Birthday Lookup
Overview Arrays are superfast. Simply becasue the exact location (index) of the element is provided. What if we could tie that index to the actual value of the data being…
The Under-Appreciated Hash Function–Illustrated using a Birthday Lookup
Considering the amount of time they have saved mankind, Hash-functions are among the most under-appreciated gems of #160; A hashing function will typically, using just a SINGLE lookup – retrieve…