Patterns for simulating optional "out" parameters in C#?

Posted by Jesse McGrew on Stack Overflow See other posts from Stack Overflow or by Jesse McGrew
Published on 2010-06-13T07:41:41Z Indexed on 2010/06/13 7:52 UTC
Read the original article Hit count: 328

I'm translating an API from C to C#, and one of the functions allocates a number of related objects, some of which are optional. The C version accepts several pointer parameters which are used to return integer handles to the objects, and the caller can pass NULL for some of the pointers to avoid allocating those objects:

void initialize(int *mainObjPtr, int *subObjPtr, int *anotherSubObjPtr);

initialize(&mainObj, &subObj, NULL);

For the C# version, the obvious translation would use out parameters instead of pointers:

public static void Initialize(out int mainObj, out int subObj,
    out int anotherSubObj);

... but this leaves no way to indicate which objects are unwanted. Are there any well-known examples of C# APIs doing something similar that I could imitate? If not, any suggestions?

© Stack Overflow or respective owner

Related posts about c#

Related posts about optional-parameters