C#: Thread Safe Event calls

Sudhanshu's Ode to Code

Hello World,

While working on a multi-threaded Windows service that used events heavily, I did some research on how to trigger events in a thread safe manner. Inevitably, one looks for reference material or books that have the respect of the community and Jeffery Richter’s CLR via C# is one such resource.

The Context

In the chapter on Events (Chapter 11, pg 265 in the eBook), Jeffery summarizes the first 3 steps of how to raise events in a thread safe manner by providing a snippet for an extension method that can do this as such:

The above code is supposed to be an improvement over the following way where one uses a temp variable to store the passed in delegate instance and then compares the temp to null:

The reason why he proposes the code in the extension method over the simpler approach above is that though in theory…

View original post 262 more words

Benchmarking hash functions

lonewolfer

It turns out that for integer hash tables implementations the identity function (f(x)=x) is preferred in most cases. This is a trade-off between speed and desirable properties such as uniform distribution that works well for integers. In essence the speed of attempted lookups is so fast that we can afford the increased number of collisions a naive hash function will give us.

For byte arrays such as strings this is not the case, and instead the opposite is true. Optimising the lookup algorithm is less important, and the properties of the hash function much more so.

To choose a good hash function we need to look at both speed, how quickly the hash is calculated, and how likely collisions are to occur in a given use case. We will start with looking at speed, or data throughput, of some of the most interesting potential candidates.

The benchmark

In this micro-benchmark…

View original post 220 more words

Using .NET Reference Source for debugging

Improvements when debugging .NET Reference Source

Historically since the inception of this effort, Microsoft have published sources and PDBs for every major .NET framework update namely .NET framework 4.0 and 4.5. However these builds would be rendered effectively useless the moment any update to the framework was released, since the binaries on the updated box no longer matched the PDBs that were indexed on the reference source server. Unfortunately the design of the system that they had in place was geared towards doing single and infrequent pushes of sources and symbols out and did not account for the sheer volume of builds and patches that come are produced out of the .NET framework build system.

Continue reading “Using .NET Reference Source for debugging”

Whats new in C# 6.0

mytechnetknowhows

  • Exception Filters : Exception filters are a CLR capability that is exposed in Visual Basic and F#, but hasn’t been in C# – until now. If the parenthesized expression after ‘if’ evaluates to true, the catch block is run, otherwise the exception keeps going. Basically Exception Filter is one of the new features of C# 6.0 that allows us to specify a conditional clause for each catch block. In other words now we can write a catch block that will handle the exception of a specific type only when a certain condition is true that is written in an exception filter clause. First we see a code snippet of an Exception Filter then we will learn more about it.
        static void Main(string[] args)
        {
            try
            {
                throw new ArgumentNullException("Arg1");
                //throw new ArgumentNullException("Arg2");
            }
            catch (ArgumentNullException ex) if (ex.Message.Contains("Arg1"

View original post 1,359 more words

Value Types – When (not) to Use

Just to reiterate – What are the value types (structs, enums)

Value types are cut-down classes.  Imagine classes that don’t support inheritance or finalizers, and you have the cut-down version: the struct.  Structs are defined in the same way as classes (except with the struct keyword), and apart from the some limitations (described below) , structs can have the same rich members, including fields, methods, properties and operators.  

In the previous post I have also outlined several benefits of using value types:

1) They don’t require garbage collection
2) They use less space due to alignment and having no object header
3) Even though they are copied you can still get around the issue by using the ref keyword
4) By default value types are not nulluble, but you can wrap them into Nulluble,  essentially achieving the same semantics as a reference type.

However there are certain major drawbacks using value types:

Continue reading “Value Types – When (not) to Use”

Linq and Missing UnionAll

While it’s a bit weird that Linq doesn’t come with UnionAll there are number of ways of implementing it. The first and the most obvious one is to write something like:

            public static IEnumerable<T> UnionAll<T>(IEnumerable<T> set1, IEnumerable<T> set2)
            {
                foreach (var s1 in set1)
                    yield return s1;

                foreach (var s2 in set2)
                    yield return s2;
            }

However according to Google this doesn’t seem to play nice with EF and Linq to SQL. The second way of achieving the same goal would be writing an extension method that uses the .Concat function of Linq

            public static IEnumerable<T> UnionAll<T>(IEnumerable<T> set1, IEnumerable<T> set2)
            {
                return set1.Concat(set2);
            }

This appears to achieve the desired effect. It’s worth noting that I haven’t tested the performance of the 2 implementation, but my guess is that in Linq to Object it would be almost identical

Reference types vs value types

All C# types fall into the following categories:

  • Value types
  • Reference types
  • Generic type parameters
  • Pointer types

Value types comprise most built-in types (specifically, all numeric types, the char type, and the bool type) as well as custom struct and enum types.

Reference types comprise all class, array, delegate, and interface types. (This includes the predefined string type.) The fundamental difference between value types and reference types is how they are handled in memory.

Continue reading “Reference types vs value types”

SQL Interview Q & A

SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Introduction – Day 1 of 31

In this very first blog post – various aspect of the interview questions and answers are discussed. Some people like the subject for their helpful hints and thought provoking subject, and others dislike these posts because they feel it is nothing more than cheating.  I’d like to discuss the pros and cons of a Question and Answer format here.

SQL SERVER – Interview Questions and Answers – Frequently Asked Questions – Day 2 of 31

  • What is RDBMS?
  • What are the Properties of the Relational Tables?
  • What is Normalization?
  • What is De-normalization?
  • How is ACID property related to Database?
  • What are the Different Normalization Forms?

Continue reading

Getting started with VBA

Chanmingman's Blog

This article is get you started with VBA.

After almost 20 years I have learned VBA, Microsoft never changed Macro to be able to use C# without using Visual Studio. Hence, many people stuck with VBA.

This article will show you how to start VBA in Excel 2013.

1. Start Excel 2013.

2. Go to File Tab -> Options -> Customize Ribbon then enable the Developer tab.

 vba

3. Go to Developer tab choose Insert, under ActiveX choose Command Button.

 vba2

4. Double click the Command Button Excel will bring you to Visual Basic. Type the following code inside the CommandButton1_Click().

Private Sub CommandButton1_Click(

  Worksheets(“Sheet1”).Activate

  Range(“A1”).Value = “1”

End Sub

5. Back to the Excel sheet click the Design Mode to enable to run mode.

Now you can try to click the button.

View original post

Blog at WordPress.com.

Up ↑