Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does casting a generic type using the dynamic keyword cause boxing?

Will the following code cause boxing of the integer passed into the call to GenericMethod?

void GenericMethod<T>(T value)
{
  int test = (dynamic)value;
}

void Main()
{
  GenericMethod(100);
}
like image 506
Slight Avatar asked Apr 08 '26 08:04

Slight


1 Answers

According to C# specification §4.7 The dynamic type:

dynamic is considered identical to object except in the following respects:

  • Operations on expressions of type dynamic can be dynamically bound (§7.2.2).

  • Type inference (§7.5.2) will prefer dynamic over object if both are candidates.

So, casting to dynamic cause boxing in the same way as casting to object.

like image 170
user4003407 Avatar answered Apr 10 '26 23:04

user4003407



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!