Search code examples
c#guidout

c# out causing a no overload for method error?


I am new to c# and a little confused about how the out parameter modifier works.

I have this function

public static void GuidUnsmash(string smashed, out Guid first, out Guid second)
{
    if (smashed.Length != 64)
    {
        throw new ArgumentOutOfRangeException();
    }

    string firstGuid = smashed.Substring(0, 32);
    string secondGuid = smashed.Substring(32, 32);

    first = new System.Guid(firstGuid);
    second = new System.Guid(secondGuid);
}

which im trying to call using this

[HttpGet]
public async Task<ActionResult> Pending(string pendingticket)
{
    // CAR-AAR built with PersonId and MonerisID in ticket, this was overkill, just needed the MonerisID
    GuidHelpers.GuidUnsmash(pendingticket, out Guid personId, out Guid monerisId); //this is the line that has the error
    var preload = await MonerisApplicationRepository.PrepareTransactionForMonerisPreload(monerisId);
    var preloadResponse = await MonerisRepository.PostPreloadTransactionRequest(preload);
    var persistPreload = await MonerisApplicationRepository.PersistMonerisPreloadTransactionResponse(monerisId, preloadResponse);

    var transactionRequest = preloadResponse.ToMonerisPreAuthorization();

    //var viewPendingRequest = new MonerisPendingPreloadTransaction(transactionRequest, preload);

    // View redirects to Moneris with autosubmit to begin preauthorization process.
    return View(transactionRequest);
}

However i am getting an error that says that there is no overload method for GuidUnsmash. Which is confusing me because they both have the same amount of parameters.


Solution

  • Normally, parameters used in function calls are "passed-by-value". The other option is for parameters to be passed-by-reference.

    Keyword ref forces a parameter to be passed-by-reference. But that does not mean the function will actually do anything with the variable. It may or it may not.

    Keyword out forces a parameter to be passed-by-reference and also that the value has to be assigned in the function. This can have ramifications for stuff like readonly variables which need a assignment. And so the caller is aware the value will not stay the same.

    Keyword in is relatively new syntax, related to 'ref' and 'out'. It forces a parameter to be passed-by-reference but also prevents any reassignment of the value (like a inverted out). However I am somewhat unsure why you would use it.

    In langauges that use naked pointers, usually naked pointer is used instead of ref. And there are no in or out keywords.

    As for overloading:

    The in, ref, and out keywords are not considered part of the method signature for the purpose of overload resolution. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref or in argument and the other takes an out argument.

    Which means the error make no real sense.

    So that leaves one hard-to-debug issue: the code is so broken somewhere before or between those two code piece, the compiler has issues even still telling you where the error is.