3

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);
7
  • You can convert a nullable bool? to an object. Apr 8, 2013 at 17:26
  • 2
    It is not clear what you're trying to accomplish here. What's the intended usage of your extended array? How is it different from List<T>? Apr 8, 2013 at 17:27
  • google Boxing boolean c# Apr 8, 2013 at 17:28
  • Explanations aside, you almost certainly need to use generics here to solve the problem so that you can accept an array of T, rather than an array of objects.
    – Servy
    Apr 8, 2013 at 17:32
  • 2
    BTW there is not need to explicitly inherit from Object. All classes inherit from it anyway. Apr 8, 2013 at 17:35

3 Answers 3

5

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 bools, 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#.

1
  • 1
    Your input was very useful svick. Thank you. Apr 9, 2013 at 16:06
2

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[].

1
  • 1
    I doubt the OP knows what covariance is.
    – svick
    Apr 8, 2013 at 17:30
1

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);
    }
}
4
  • The whole point of his type seems to be intentionally removing type safety, unless that was just an unintended side effect. This here wouldn't have any advantage to just using a regular array.
    – Servy
    Apr 8, 2013 at 17:50
  • 1
    I think you are just speculating. Let us see what the OP says. Apr 8, 2013 at 17:52
  • @Servy I am trying to make it accept any kind of array thrown at it. Note: I am a relatively new C# programmer though I have experience with Basic and Java. I also don't know what T[] means nor do I know what the <bool> syntax means. Apr 9, 2013 at 15:12
  • 1
    Nevermind I went and looked up <T>. It all makes sense now, thank you. Apr 9, 2013 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.