Miles
Miles

Reputation: 21

Does a primitive type get boxed when declared as a dynamic

I'm working on a way to handle parsing strings to types without knowing what types they are being parsed to. Specifically bools/ints/dates etc.

I am considering using a Dictionary<Type, Func <string,dynamic>> to do this but want to know if there is going to be a boxing/unboxing of the value.

Upvotes: 1

Views: 44

Answers (1)

user1726343
user1726343

Reputation:

Yes, the value type instances you return from your funcs will be boxed (you effectively have a dictionary that stores Func<string, object>s).

When you invoke the func and try to assign it to a variable or pass it into a method, the value will be unboxed and stored in a reference of the appropriate type.

Upvotes: 2

Related Questions