Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pass Reference or Return Value

Pass Reference or Return Value

Scheduled Pinned Locked Moved C#
algorithmsquestiondiscussion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Warburton
    wrote on last edited by
    #1

    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

    L L 2 Replies Last reply
    0
    • J Jim Warburton

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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|


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        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|


        J Offline
        J Offline
        Jim Warburton
        wrote on last edited by
        #3

        Thanks, answered my question succinctly.

        this thing looks like it was written by an epileptic ferret Dave Kreskowiak

        1 Reply Last reply
        0
        • J Jim Warburton

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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, the DateTime class also uses return values (AddHours, AddMonths, etc.), which means that DateTime 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

          R 1 Reply Last reply
          0
          • L Lost User

            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, the DateTime class also uses return values (AddHours, AddMonths, etc.), which means that DateTime 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

            R Offline
            R Offline
            Roger Alsing 0
            wrote on last edited by
            #5

            >>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)

            My Blog

            L 1 Reply Last reply
            0
            • R Roger Alsing 0

              >>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)

              My Blog

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 and DateTime 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 original DateTime methods do, by returning a new instance.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups