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”

Advertisements

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”

How C# 6.0 Simplifies, Clarifies and Condenses Your Code

C# 6.0 isn’t a radical revolution in C# programming. Unlike the introduction of generics in C# 2.0, C# 3.0 and its groundbreaking way to program collections with LlNQ, or the simplification of asynchronous programming patterns in C# 5.0, C# 6.0 isn’t going to transform development. That said, C# 6.0 will change the way you write C# code in specific scenarios, due to features that are so much more efficient you’ll likely forget there was another way to code them. It introduces new syntax shortcuts, reduces the amount of ceremony on occasion, and ultimately makes writing C# code leaner. In this article I’m going to delve into the details of the new C# 6.0 feature set that make all this possible. Specifically, I’ll focus on the items outlined in the Mind Map shown in Figure 1.

dn879355.Michaelis_Figure1_hires(en-us,MSDN.10)

Continue reading

Troubleshootng Deadlocks in .NET Applications

What do you do when your application become unresponsive due to potential deadlocks?

I think the first thing to do is to identify exactly where the application hangs due to a deadlock. To do this you should replace the lock(…) statement with it’s equivalent Monitor.Enter(…)/Monitor.Exit(…). Monitor has an argument which alloys you to specify timeout. So when you change the suspicious code with Monitor.Enter(…)/Monitor.Exit(…) while specifying the timeout, the code will throw the exception when this timeout has exceeded the specified timeout. While this doesn’t solve the dead lock issue, it points you to the location within you code where there is a possible issue.

Additionally, you need to make sure that the locks are taken in the same order i.e,

lock(_a)
{
     lock(_b)
    {
        ....
    } 
}

This is only for troubleshooting purposes. You should revert back to normal locking when the issue is found and fixed.

One easy thing you can do is to pause execution within Visual Studio by pressing on the pause button and then see what each thread is up to.

You also might want to consider more granular locking primitives, such ReadWriterLockerSlim and refactor you code to use less locking.

Whenever you feel like putting a lock always question yourself if this is the right thing to do and if there is a way doing this without locking all together

C# – Undocumented Keywords

While browsing the code of the interlocked class I stumbled upon the following piece of code

public static T CompareExchange<T>(ref T location1, T value, T comparand) where T : class
{
    // _CompareExchange() passes back the value read from location1 via local named 'value'
    _CompareExchange(__makeref(location1), __makeref(value), comparand);
    return value;
}

Further googling revealed that there are actually 4 undocumented keywords in C# __arglist, __refvalue, __makeref, __reftype

More on the subject can be found on codeproject: http://www.codeproject.com/Articles/38695/UnCommon-C-keywords-A-Look

DateTime – It’s all about the rules!

The Rules

  1. Calculations and comparisons of DateTime instances are only meaningful when the instances being compared or used are representations of points in time from the same time-zone perspective.
  2. A developer is responsible for keeping track of time-zone information associated with a DateTime value via some external mechanism. Typically this is accomplished by defining another field or variable that you use to record time-zone information when you store a DateTime value type. This approach (storing the time-zone sense alongside the DateTime value) is the most accurate and allows different developers at different points in a program’s lifecycle to always have a clear understanding of the meaning of a DateTime value. Another common approach is to make it a “rule” in your design that all time values are stored in a specific time-zone context. This approach does not require additional storage to save a user’s view of the time-zone context, but introduces the risk that a time value will be misinterpreted or stored incorrectly down the road by a developer that isn’t aware of the rule.

Continue reading “DateTime – It’s all about the rules!”

Blog at WordPress.com.

Up ↑