Reputation: 155
I have a program that I am trying to extend the object class to allow for a method that refers to all members of an Array.
Code of ExtendedArray class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleMaze
{
class ExtendedArray : Object
{
private object[] referredTo;
public ExtendedArray()
{
referredTo = null;
}//empty constructor
public ExtendedArray(Object[] obj)
{
referredTo = obj;
}//full constructor
public void referToAll(object changeTo)
{
for (int x = 0; x < referredTo.Length; x++)
{
referredTo[x] = changeTo;
}
}//referToAll(bool)
}//ExtendedArray <== class
}
I intend on using this class and adding things to it in the future so its very generic. Though when I try to use the constructor it errors out with cannot implicitly convert type bool[] to type object[].
Boolean[] dirBoolArray = { curLeft, curUp, curRight, curDown };
ExtendedArray dirBool = new ExtendedArray(dirBoolArray);
Upvotes: 3
Views: 6135
Reputation: 244797
Contrary to what you said in your title, you can assign bool
to an object
variable. The following code compiles fine:
bool b = true;
object o = b;
What the second line actually does is that it boxes the bool
value, which converts it from a value type to reference type.
What this means is that b
is a single byte that directly contains the value true
. On the other hand, o
is a reference (4 or 8 bytes) that points to an object that contains a byte with the true
value, plus some metadata.
What you can't do is to assign a bool[]
to a variable of type object[]
. This is because bool[]
directly contains a single byte for each bool
, while object[]
contains a reference for each item.
Converting bool[]
to object[]
would be quite a big change: each bool
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.
What you can do to make this work is to create object[]
from the start:
object[] dirBoolArray = { curLeft, curUp, curRight, curDown };
ExtendedArray dirBool = new ExtendedArray(dirBoolArray);
This will create an array of boxed bool
s, which is exactly what you need.
Few more notes:
Since you're using Boolean
, maybe you're expecting that there is some difference between bool
and Boolean
(like in Java). In C# there is no difference between the two, bool
is just an alias for Boolean
.
Using object[]
is a code smell, which says you might be doing something wrong. It's certainly not “generic” in the way that word is usually understood in C#.
Upvotes: 5
Reputation: 29244
Why can't you do this:
class ExtendedArray<T>
{
private T[] referredTo;
public ExtendedArray()
{
referredTo=null;
}//empty constructor
public ExtendedArray(T[] obj)
{
referredTo=obj;
}//full constructor
public void referToAll(T changeTo)
{
for(int x=0; x<referredTo.Length; x++)
{
referredTo[x]=changeTo;
}
}//referToAll(bool)
}//ExtendedArray <== class
class Program
{
static void Main(string[] args)
{
ExtendedArray<bool> array=new ExtendedArray<bool>(new bool[10]);
array.referToAll(true);
}
}
Upvotes: 1
Reputation: 108800
Array co-variance only works if the members can be converted without changing their representation (plus some other restrictions).
This means that you can cast an array of a reference type to object[]
, but you can't cast an array that contains value types to object[]
. Converting from bool
to object
does not preserve the representation, and it's thus not possible to cast bool[]
to object[]
.
Upvotes: 2