What is the difference between CLR and DLR in C#?

19,897

Solution 1

The Common Language Runtime (CLR) is the core set of services offered by .NET – a type system, JIT, a garbage collector, &c. Those are available to all .NET languages, hence the "Common" part.

The Dynamic Language Runtime (DLR) builds atop of this and offers services for dynamic languages: dynamic types, dynamic method dispatch, code generation, &c. The idea is to make those things uniform and share them among dynamic languages so that they work predictably and similar, just like things on the CLR are across all languages too.

In a way those are comparable, a "normal" language on .NET uses the CLR, a dynamic language should use the DLR, but will use the CLR as well. They are basic sets of functionality that the designers considered to be good when they are common across languages. IronPython and IronRuby were implemented on top of the DLR, as is C# 4's dynamic feature.

Solution 2

I'll just add a simple diagram to demonstrate the point:

    "*** Runtime & Libraries ***"    │ "*** Languages ***"
┌────────────────────────────────────┤
│ .NET Libraries                     │
│   ┌────────────────────────────────┼──────────────────┐
│   │ Dynamic Language Runtime (DLR) │ C# 4.0 'dynamic' ├────┐
├───┴────────────────────────────────┼──────────────────┘    │
│ Common Language Runtime (CLR)      │   C# 1.0, 2.0, 3.0    │
└────────────────────────────────────┴───────────────────────┘

Solution 3

Just adding a bit of clarity to the already given excellent answers.

First the CLR, this is a abstraction layer that sits between user code and the physical hardware. There are several advantages:

  1. Hardware independence 2. Common type system 3. Language interoperability

CLR

The .Net DLR was created by Jim Hugunin and was

The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages.
Dynamic languages can identify the type of an object at run time, whereas in statically typed languages such as C# and Visual Basic (when you use Option Explicit On) you must specify object types at design time. Examples of dynamic languages are Lisp, Smalltalk, JavaScript, PHP, Ruby, Python, ColdFusion, Lua, Cobra, and Groovy.

DLR Architecture
DLR

The DLR adds a set of services to the CLR for better supporting dynamic languages. These services include the following: Expression trees. The DLR uses expression trees to represent language semantics. For this purpose, the DLR has extended LINQ expression trees to include control flow, assignment, and other language-modeling nodes. For more information, see Expression Trees (C# and Visual Basic). Call site caching. A dynamic call site is a place in the code where you perform an operation like a + b or a.b() on dynamic objects. The DLR caches the characteristics of a and b (usually the types of these objects) and information about the operation. If such an operation has been performed previously, the DLR retrieves all the necessary information from the cache for fast dispatch. Dynamic object interoperability. The DLR provides a set of classes and interfaces that represent dynamic objects and operations and can be used by language implementers and authors of dynamic libraries. These classes and interfaces include IDynamicMetaObjectProvider, DynamicMetaObject, DynamicObject, and ExpandoObject. The DLR uses binders in call sites to communicate not only with the .NET Framework, but with other infrastructures and services, including Silverlight and COM. Binders encapsulate a language's semantics and specify how to perform operations in a call site by using expression trees. This enables dynamic and statically typed languages that use the DLR to share libraries and gain access to all the technologies that the DLR supports.

Examples

dynamic d = "test";  
Console.WriteLine(d.GetType());  
// Prints "System.String".  

d = 100;  
Console.WriteLine(d.GetType());  
// Prints "System.Int32".    

dynamic d = "test";  

// The following line throws an exception at run time.  
d++;  

Solution 4

The DLR runs on top of the CLR. The DLR allows C# dynamic variables and other dynamic language features.

http://en.wikipedia.org/wiki/Dynamic_Language_Runtime

Share:
19,897

Related videos on Youtube

masoud ramezani
Author by

masoud ramezani

I'm a Software Developer. :)

Updated on July 22, 2020

Comments

  • masoud ramezani
    masoud ramezani almost 4 years

    What is the difference between CLR and DLR in C#? are these two concept comparable?

  • Cœur
    Cœur over 5 years
    Please use a picture or dashed-borders, because from the current ascii diagram alone it's quite unclear if CLR is included in .NET Libs or not.
  • Pacerier
    Pacerier almost 5 years
    Basically one extra layer? Can IronPython use CLR directly?
  • Rich
    Rich almost 5 years
    @Pacerier: Yes. Yes. The DLR just makes it convenient and easy to have operations at runtime that are usually done at compile-time. And in a manner that's consistent across all languages that use the DLR. As mentioned, C#'s dynamic also uses the DLR.
  • MarredCheese
    MarredCheese almost 4 years
    What kind of diagram is this and what does it mean?