Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Contrary to what you said in your title, you can assign <code>bool</code> to an <code>object</code> variable. The following code compiles fine:</p> <pre><code>bool b = true; object o = b; </code></pre> <p>What the second line actually does is that it <a href="http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx" rel="nofollow">boxes</a> the <code>bool</code> value, which converts it from a value type to reference type.</p> <p>What this means is that <code>b</code> is a single byte that directly contains the <em>value</em> <code>true</code>. On the other hand, <code>o</code> is a <em>reference</em> (4 or 8 bytes) that points to an object that contains a byte with the <code>true</code> value, plus some metadata.</p> <p>What you can't do is to assign a <code>bool[]</code> to a variable of type <code>object[]</code>. This is because <code>bool[]</code> directly contains a single byte for each <code>bool</code>, while <code>object[]</code> contains a <em>reference</em> for each item.</p> <p>Converting <code>bool[]</code> to <code>object[]</code> would be quite a big change: each <code>bool</code> would have to be boxed separately and a new array would have to be allocated for the references to these boxes. Because of that, the compiler doesn't allow you to do that directly.</p> <p>What you can do to make this work is to create <code>object[]</code> from the start:</p> <pre><code>object[] dirBoolArray = { curLeft, curUp, curRight, curDown }; ExtendedArray dirBool = new ExtendedArray(dirBoolArray); </code></pre> <p>This will create an array of boxed <code>bool</code>s, which is exactly what you need.</p> <p>Few more notes:</p> <ul> <li><p>Since you're using <code>Boolean</code>, maybe you're expecting that there is some difference between <code>bool</code> and <code>Boolean</code> (like in Java). In C# there is no difference between the two, <code>bool</code> is just an alias for <code>Boolean</code>.</p></li> <li><p>Using <code>object[]</code> is a code smell, which says you <em>might</em> be doing something wrong. It's certainly not “generic” in the way that word is usually understood in C#.</p></li> </ul>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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