Are there performance penalties for named/optional parameters for constructors?

c#, constructor, performance

Solution

No performance penalties.

In fact, optional parameters do not exist at runtime. They are a compiler trick, i.e. the compiler puts in the full parameter set.

There even is an explicit warning to not use them (one I totally disagree with) because if you change the default value, then the old compiled code does not use the new values.

Performance differences between overloading or optional parameters?

has exactlyx the same question.

Problem

So would: ``` public Car(string color = "red", topSpeed = 180) { carColor = color; carTopSpeed = topSpeed; } ``` be faster or slower to do than constructor chaining to get the values to carColor and carTopSpeed? I understand on a desktop environment the performance will almost always be negligible, but: I would like to know for mobile game development where all the little things count in performance. Thanks in advance!

Original source

Related problems