In C#, is there any significant reduction in memory allocation when passing a DateTime reference as a parameter to a function as opposed to passing it by value?
int GetDayNumber(ref DateTime date)
vs
int GetDayNumber(DateTime date)
The code inside the function is not modifying the date in any case.
A
DateTimeis a 8 byte struct. Arefhas 4 or 8 bytes depending on your target architecture. So at best you’d save 4 bytes of stack memory, which is completely irrelevant.It’s even likely that
refprevents some optimizations, such as placing theDateTimein a register, thus actually increasing memory use.This is a clear case of premature optimization. Don’t do this.