Note that there are some explanatory texts on larger screens.

plurals
  1. POWorkaround for optional ref parameters in C#
    primarykey
    data
    text
    <p>I'm trying to write a method that takes references to boolean flags and modify them. The booleans are all declared separately (i.e. not in an indexable data structure) and the caller of the method should be able to decide which booleans are being modified.</p> <p>Example code (this works):</p> <pre><code>class Program { private static bool b1, b2, b3, b4, b5; private static void doSomething(ref bool setTrue, ref bool setFalse, ref bool invert) { setTrue = true; setFalse = false; invert = !invert; } static void Main(string[] args) { Console.WriteLine("Pre: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5); doSomething(ref b1, ref b3, ref b5); Console.WriteLine("Post: {0}, {1}, {2}, {3}, {4}", b1, b2, b3, b4, b5); } } </code></pre> <p>Output, as expected:</p> <pre><code>Pre: False, False, False, False, False Post: True, False, False, False, True </code></pre> <p>So far, so good. Now these parameters should be optional on the method. That is, the caller can choose to e.g. use the <code>setTrue</code> and the <code>invert</code> effect, but not the <code>setFalse</code> one.</p> <p>Basically, what I'd like to do is this:</p> <pre><code>doSomething(ref b1, null, ref b5); // error CS1503: Argument 2: cannot convert from '&lt;null&gt;' to 'ref bool' </code></pre> <p>And then declare the <code>doSomething</code> method like this:</p> <pre><code>private static void doSomething(ref bool setTrue, ref bool setFalse, ref bool invert) { if(setTrue != null) setTrue = true; if(setFalse != null) setFalse = false; if(invert != null) invert = !invert; } </code></pre> <p>Note that I do <em>not</em> want to check if the value is null. The values are real bools and can't be null (and declaring them as <code>bool?</code> doesn't really solve my problem). I only want to give the caller the ability to give null <em>as the reference</em>.</p> <p>While the implementation of the method may be more complex, I'd really like to keep the invocation down to one line. (I.e. avoid having to declare temporary variables just for this call.)</p> <p>One possibility would be to declare (eight) overloads for the function with all combinations of bools given or not, but then I need to come up with some scheme to make sure they all have unique signatures. (I'm stuck with C# 3.0, so no named parameters.)</p> <p>Am I missing something? Is there a clean workaround? Currently the only (barely) acceptable alternative I can think of is to pass in strings with the variable names (or null) and then resolve these to the actual field using reflection.</p> <p>PS: As you're probably wondering why I trying to do something this strange, some words of background: the <code>doSomething</code> method is part of a library. The invocations of <code>doSomething</code> are coming from generated C# code. And yes, having all these bools (~200 in the real project) as separate fields <em>does</em> make sense in the big picture, but the reasoning isn't really relevant for this question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload