0

Code snippet:

                var pMessageId = p.Get<byte[]>("p_message_id");
            var pContentId = p.Get<byte[]>("p_content_id");
            if (pMessageId != null && pContentId != null)
            {
                var result = new Guid(pMessageId);
                var secondResult = new Guid(pContentId);
                await transaction.CommitAsync(cancellationToken);
                return 
            }

I want to return two guid values at once (result, secondResult). How do I do that?

Matheo
  • 35
  • 6

2 Answers2

2

Use a value tuple:

public (Guid Result, Guid SecondResult) MyMethod(){
   ...
   return (result, secondResult);
}
JonasH
  • 28,608
  • 2
  • 10
  • 23
0

The issue with "Tuple" is that your values are accessible by .Item1, .Item2.

IMHO, you mortgage future maintainability for a quick fix.

(That part is opinion based).

But technically, one way to solve it is to create a simple Poco. (Aka, start no opinion area)

public sealed class GuidHolder

  public Guid GuidOne { set; get; }

  public Guid GuidTwo { set; get; }

}

or a more "convenient" version

public sealed class GuidHolder

  public GuidHolder() : this(Guid.NewGuid() , Guid.NewGuid() )
  {}


    public GuidHolder(Guid g1, Guid g2)
    { 
       this.GuidOne = g1;
       this.GuidTwo = g2;
    }

  public Guid GuidOne { private set; get; }

  public Guid GuidTwo { private set; get; }

}

So the above is a no opinion technical answer.

Back to opinion:

IMHO, (the above poco's are) 3 minutes of work. Now your code is more readable for the future.

When your code base is riddled with ".Item1" .. "ItemN" code.....is becomes difficult to maintain. Aka, mortgage the future maintainability to save 3 minutes of work.

granadaCoder
  • 26,328
  • 10
  • 113
  • 146