Jump to content

Two return methods

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
I was reading on another forum about two different ways to return objects in C#. I've never seen the second way and have always used the first method.

Here is the first (the old way):


public int GetData(int x, int y)


and here is the second


public void GetData (int x, int y, int z, out int myInt)


It seems they both do the same thing and there would be no difference using them. Which method should I use? Which method is correct?

#2
smith

smith

    Programmer

  • Members
  • PipPipPipPip
  • 153 posts
Which ever method you prefer. There is no right or wrong.

for (int i;;) {

   cout << "Smith";

}


#3
Guest_NeedHelp_*

Guest_NeedHelp_*
  • Guests
I guess I'll use the one I've always used.

private int myfunc();

#4
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
It is slightly odd, me thinks, to use an out parameter with a void return. Usually, you'd use an out to give back another piece of info in addition to the return. The .NET TryParse methods follow this convention, returning true/false to let you know if the Parse succeeded, and giving you the parsed value in the out parameter.

There could be a reason behind having a void return and an out...but I can't think of a decent one right now.