Pass Reference or Return Value
-
Which is preferred, to pass a reference or use a return value
private DateTime SetToHour(DateTime timeCheck, int hours) { //code which changes timeCheck return timeCheck; }
or
private void SetToHour(ref DateTime timeCheck, int hours) { //code which changes timeCheck }
I have done some searching online and asked around work, no definitive answer has come. Use ref for this and return for that. Mostly seems to personal choice. Thoughts???
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
-
Which is preferred, to pass a reference or use a return value
private DateTime SetToHour(DateTime timeCheck, int hours) { //code which changes timeCheck return timeCheck; }
or
private void SetToHour(ref DateTime timeCheck, int hours) { //code which changes timeCheck }
I have done some searching online and asked around work, no definitive answer has come. Use ref for this and return for that. Mostly seems to personal choice. Thoughts???
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
Hi, IMO a return value is the normal approach. It has some drawbacks: - you can return only one thing; - if the return thingy is a reference type, then the API choice implies you must create an object of that type, which you may or may not want to impose on the API users. Hence, a ref parameter is acceptable and useful when: - the function has to produce more than one thing; - or the function is supposed to stuff some data in a pre-existing struct. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Hi, IMO a return value is the normal approach. It has some drawbacks: - you can return only one thing; - if the return thingy is a reference type, then the API choice implies you must create an object of that type, which you may or may not want to impose on the API users. Hence, a ref parameter is acceptable and useful when: - the function has to produce more than one thing; - or the function is supposed to stuff some data in a pre-existing struct. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Thanks, answered my question succinctly.
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
-
Which is preferred, to pass a reference or use a return value
private DateTime SetToHour(DateTime timeCheck, int hours) { //code which changes timeCheck return timeCheck; }
or
private void SetToHour(ref DateTime timeCheck, int hours) { //code which changes timeCheck }
I have done some searching online and asked around work, no definitive answer has come. Use ref for this and return for that. Mostly seems to personal choice. Thoughts???
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
In my opinion I'd use the non-ref method. The reason is, that many classes in the .NET framework are immutable, like
string
. Even more important, theDateTime
class also uses return values (AddHours
,AddMonths
, etc.), which means thatDateTime
objects are immutable, too. In C# 3.0 you could also use extension methods, for example like this:public static class DateTimeExtensions { public static DateTime SetToHour(this DateTime source, int hours) { if ((hours < 0) || (hours > 23)) throw new ArgumentOutOfRangeException("Hours must be between 0 and 23"); return source.AddHours(hours - source.Hour); } }
and call it like that:
DateTime t = DateTime.Now;
Console.WriteLine(t);
t = t.SetToHour(24); // cool, we got a new extension method in the .NET DateTime class
Console.WriteLine(t);regards
-
In my opinion I'd use the non-ref method. The reason is, that many classes in the .NET framework are immutable, like
string
. Even more important, theDateTime
class also uses return values (AddHours
,AddMonths
, etc.), which means thatDateTime
objects are immutable, too. In C# 3.0 you could also use extension methods, for example like this:public static class DateTimeExtensions { public static DateTime SetToHour(this DateTime source, int hours) { if ((hours < 0) || (hours > 23)) throw new ArgumentOutOfRangeException("Hours must be between 0 and 23"); return source.AddHours(hours - source.Hour); } }
and call it like that:
DateTime t = DateTime.Now;
Console.WriteLine(t);
t = t.SetToHour(24); // cool, we got a new extension method in the .NET DateTime class
Console.WriteLine(t);regards
>>The reason is, that many classes in the .NET framework are immutable, like string. Im not quite getting in what way you mean that would affect anything? Its perfectly possible to pass a "ref string" Even if the instance of a string is immutable doesnt mean that the variable reference to that instance is immutable... Or do you just mean that the api would be more standard because other methods on immutable types work that way? (In that case I agree ;-) , rule of least surprise)
-
>>The reason is, that many classes in the .NET framework are immutable, like string. Im not quite getting in what way you mean that would affect anything? Its perfectly possible to pass a "ref string" Even if the instance of a string is immutable doesnt mean that the variable reference to that instance is immutable... Or do you just mean that the api would be more standard because other methods on immutable types work that way? (In that case I agree ;-) , rule of least surprise)
Roger Alsing wrote:
Its perfectly possible to pass a "ref string" Even if the instance of a string is immutable doesnt mean that the variable reference to that instance is immutable...
Absolutely correct - what I meant is that the .NET library methods for the
string
andDateTime
objects (among others) do not modify the variable but rather return a new instance. So if you want to add another method to the DateTime class (by using extension methods or simply a utility class) I might be a good idea to do it in the same way like the originalDateTime
methods do, by returning a new instance.