Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boxing on dynamic data type in c#?

Tags:

c#

.net

Type checking for an Object takes place at compile time where as type checking for dynamic data type takes place at run time then how can we box a dynamic value into Object?

dynamic dynamic = "This is dynamic data type";
Object obj = dynamic;
Console.WriteLine(obj);
like image 996
monofal Avatar asked May 15 '17 14:05

monofal


People also ask

What is dynamic data type in C?

Dynamic data types are dynamic in nature and don't require initialization at the time of declaration. It also means that a dynamic type does not have a predefined type and can be used to store any type of data. We can define this data type using the keyword “dynamic" in our code.

What is boxing and unboxing in reference type variable?

Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object. In the following example, the integer variable i is boxed and assigned to object o .

How can we avoid boxing and unboxing?

How to prevent boxing & unboxing: Use ToString method of numeric data types such as int, double, float etc. Use for loop to enumerate on value type arrays or lists (do not use foreach loop or LINQ queries) Use for loop to enumerate on characters of string (do not use foreach loop or LINQ queries)

Which of the following codes is boxing?

Boxing In C#The process of converting a Value Type variable (char, int etc.) to a Reference Type variable (object) is called Boxing.


1 Answers

dynamic is already always an object - it is essentially implemented as object, with different rules on how invocation happens. So there's nothing to box between dynamic and object. Additionally, a string literal is an object, so: there's nothing to box there.

No boxing required here. You already have an object. The implicit type conversion from dynamic to object is a no-op.

like image 192
Marc Gravell Avatar answered Oct 12 '22 22:10

Marc Gravell