What is the difference between CLR and DLR in C#? are these two concept comparable?
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.
dynamic
also uses the DLR. –
Wallywalnut 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 │
└────────────────────────────────────┴───────────────────────┘
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:
- Hardware independence 2. Common type system 3. Language interoperability
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.
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++;
The DLR runs on top of the CLR. The DLR allows C# dynamic
variables and other dynamic language features.
© 2022 - 2024 — McMap. All rights reserved.