0

I have written a utility function:

public static void SerializeErrorMessage(int ErrorCode, string ErrorMessage, out byte[] Buffer)
{
    object ErrorJson = new { ErrorCode, ErrorMessage };
    string Serialized = JsonConvert.SerializeObject(ErrorJson);     
    Buffer = Encoding.UTF8.GetBytes(Serialized);
}

I'm wondering why I should/shouldn't have written it like this instead:

public static byte[] SerializeErrorMessage(int ErrorCode, string ErrorMessage)
{
    object ErrorJson = new { ErrorCode, ErrorMessage };
    string Serialized = JsonConvert.SerializeObject(ErrorJson);     
    byte[] Buffer = Encoding.UTF8.GetBytes(Serialized);
    return Buffer;
}

Is it just a matter of personal preference? Is the first function more performant than the second function?

micah
  • 7,596
  • 10
  • 49
  • 90
  • in that instance I would say it's preference. It's often used as a way to 'return' multiple values – musefan Sep 27 '17 at 15:15
  • 3
    Think about `public static bool TrySerializeErrorMessage()` method instead to see a usage of `out` keyword ;). – shA.t Sep 27 '17 at 15:15
  • 1
    See all the try parse methods [TryParse](https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx) – Lithium Sep 27 '17 at 15:15

1 Answers1

0

The out keyword is used when more than one value from a function needs to be returned, without wrapping the values in an object.

e.g. Integer.TryParse, has both a return value (bool if parse was successful), and an out parameter that returns the value if the parsing was successful.

In your case, there is no difference

Allanckw
  • 641
  • 1
  • 6
  • 17