Object header get complicated

Object header, sync block, thin lock vs fat lock, hashcode – all of this and more will be explained in the following post.

First, let’s make a brief overview on the object header in the CLR.

What is the object header?

An header is – like it sound – header “above” the object.
If I try to illustrate it, it will be like this —->

Every object in CLR has an header that contains
info related to the object. The run -time use this info as we’ll see later in this post.


Continue reading

Posted in .NET, c# | Tagged , , , , , , , , , , , , | 2 Comments

C# Behind the scenes – Local Functions

The second “Behind the scenes” article, will be about local function.

This is much simple from previous post about yield keyword.
In short, local function translated to a method of the class.

Example 1

void M(int o)
    int Multiply(int n) => n * 2;

    foreach (var i in new List<int> { 1, 2, 3, 4, 5 })

Continue reading

Posted in .NET, c# | Tagged , , , | 1 Comment

C# Behind the scenes – `yield`

The first “Behind the scenes” article will be about yield keyword.

Lets take for example a very simple code that return numbers 1,2,3:

public IEnumerable<int> ExampleEnumerable()
    var arr = new int[] { 1, 2, 3 };
    foreach (var i in arr)
        yield return i;

The compiler translates this 4 lines code into an entire class.
Continue reading

Posted in .NET, c# | Tagged , , , , | 1 Comment

C# Behind the scenes

I see a lot of beginners programmers, but certainly not just beginners, who are not sure how things work in C#.

I’m not talking about the CLR but about the language itself. And mostly specific on all sorts of keywords which are compiler sugar.

There are simple things that probably everyone understands what it means, such as this or const. But what about yield or async and what happens when we use lambda expression or write Linq in the form of query comprehension?
Continue reading

Posted in .NET, c# | Tagged , , | 2 Comments

DumpMiner – UI tool for playing with ClrMD

ClrMD is a library built by Lee Culver. This is a live process and crash dump introspection library.  It allows you to write tools and debugger plugins which can do thing similar to SOS and PSSCOR.

I’m not going to write here about ClrMD, there are plenty of explanations on the net, but I want to talk about tool called DumpMiner I built using ClrMD.

When ClrMD came out, maybe three years ago, I started playing with it and I was very impressed. It gives you fairly easily, with managed code, to build tools which analyze dumps or live process (like WinDbg with SOS).

The main drawback of WinDbg  for .NET developers is the is the need to remember a plenty of commands and when to use each of them is definitely not an easy task.

So after I wrote a few examples with ClrMD, I decided to build a simple UI tool to demonstrate the operations you can do with ClrMD with easy to use UI, just click the operation and you have what you search for.

Continue reading

Posted in .NET | Tagged , , , , , , , , , , , , , | Leave a comment

Mono.Cecil – Simplify and Optimize macros

You may have seen it, but what does it actually do? Do I need to add them?

When we write IL macros, we emit instruction after instruction to the method body. Instruction, is operation to do (e.g. math operations, load\store, invocations etc.) and it may have operand. The instruction is 1-byte or 2-bytes length.

Lets look on an example of loading method argument: Continue reading

Posted in .NET, c# | Tagged , , , , , | Leave a comment

Mono.Cecil – IAssemblyResolver

One of the important thing you need to know when you start to do real work with Cecil is the IAssemblyResolver

public interface IAssemblyResolver
    AssemblyDefinition Resolve(AssemblyNameReference name);

    AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters);

    AssemblyDefinition Resolve(string fullName);

    AssemblyDefinition Resolve(string fullName, ReaderParameters parameters);

The role of this interface, is to handle module resolving. Continue reading

Posted in .NET, c# | Tagged , , , | Leave a comment