What does this do? Object[0]

  • Thread starter Thread starter Chad Z. Hower aka Kudzu
  • Start date Start date
Chad Z. Hower aka Kudzu said:
Object MyOnject = new Object[0];

What does the [0] signify?

Hi Chad,

It means you instantiated an array of type object with a Length of zero.

Joe
 
Joe Mayo said:
Chad Z. Hower aka Kudzu said:
Object MyOnject = new Object[0];

What does the [0] signify?

Hi Chad,

It means you instantiated an array of type object with a Length of zero.
Which, because it isn't immediately obvious, works because an array of
object(object[]) is still derived from object and will assign without
complaint.
 
Daniel O'Connell said:
Which, because it isn't immediately obvious, works because an array of
object(object[]) is still derived from object and will assign without
complaint.

Indeed. If I saw a line of code like that in production source, I'd
definitely be suspicious - there'd have to be a good reason for the
variable not to be declared as object[] instead of object.
 
Jon Skeet said:
Indeed. If I saw a line of code like that in production source, I'd
definitely be suspicious - there'd have to be a good reason for the
variable not to be declared as object[] instead of object.

Well I found it in MSDN in, in sample code. :)

I changed it to Object() anyways. It was only being used for a Monitor and
nothing else.

I suspet they were trying to save a few bytes, but it just doesnt seem a
clean approach. Some prior C developer probably.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Jon Skeet said:
Daniel O'Connell said:
Which, because it isn't immediately obvious, works because an array of
object(object[]) is still derived from object and will assign without
complaint.

Indeed. If I saw a line of code like that in production source, I'd
definitely be suspicious - there'd have to be a good reason for the
variable not to be declared as object[] instead of object.

As would I, the only use I could think of would be future proofing against
arrays == overload, and that is still a horrid construct.
object[] o = new object[0];

if (((object)o) == ((object)<whatever>))

seems far better to me.
 
Chad Z. Hower aka Kudzu said:
Jon Skeet said:
Indeed. If I saw a line of code like that in production source, I'd
definitely be suspicious - there'd have to be a good reason for the
variable not to be declared as object[] instead of object.

Well I found it in MSDN in, in sample code. :)

Which sample was it? Something is very odd about that...
 
Daniel O'Connell said:
Which sample was it? Something is very odd about that...

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpconASynchronousFileIO.asp

public static Object NumImagesMutex = new Object[0];

This line also looks suspicious:

fs.BeginRead(state.pixels, 0, numPixels, readImageCallback,
state);

Is it my imagination or is the call back not being referenced correctly? Such
has never compiled in my code...


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad Z. Hower aka Kudzu said:
Daniel O'Connell said:
Which sample was it? Something is very odd about that...

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpconASynchronousFileIO.asp

public static Object NumImagesMutex = new Object[0];

This is really strange, unless there is(was) a bug in object itself that I'm
not aware of this is a strange construct. An array of type object, even with
no members probably takes up more memory space than an empty object. I
really wonder what the point was...
This line also looks suspicious:

fs.BeginRead(state.pixels, 0, numPixels, readImageCallback,
state);

Is it my imagination or is the call back not being referenced correctly? Such
has never compiled in my code...

The callback is ok, although the ease of reading of this code sucks(for
non-colored code I like a little space inbetween comments and code), there
is a variable near the top of the method defined as:
AsyncCallback readImageCallback = new
AsyncCallback(ReadInImageCallback);

which is perfectly ok, a delegate is just an object afterall.
 
Daniel O'Connell said:
This is really strange, unless there is(was) a bug in object itself that
I'm not aware of this is a strange construct. An array of type object,
even with no members probably takes up more memory space than an empty
object. I really wonder what the point was...

You can change it to Object() and it works just fine. Its only used for a
Monitor lock. I suspect it was some old C coder trying to save some bytes,
assuming a 0 length array takes less space than an object.
The callback is ok, although the ease of reading of this code sucks(for
non-colored code I like a little space inbetween comments and code),
there is a variable near the top of the method defined as:
AsyncCallback readImageCallback = new
AsyncCallback(ReadInImageCallback);

Aah - That I did not see. Thats poor code if you ask me - why would they
create a variable like that to be used once?

Also naming them nearly identical, with only really a case differnce and two
letters is a very poor choice too. I really hate the actualy use of case
senstivity in C#.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Chad Z. Hower aka Kudzu said:
Aah - That I did not see. Thats poor code if you ask me - why would they
create a variable like that to be used once?

I often do that - just to break an otherwise complicated expression
into several smaller steps.
Also naming them nearly identical, with only really a case differnce and two
letters is a very poor choice too. I really hate the actualy use of case
senstivity in C#.

Personally I love it - because it gives extra readability via
conventions. Admittedly I don't think this is a particularly good
choice of names, but I usually name properties and fields with only a
case difference:

string name;
public string Name
{
get { return name; }
}

etc.
 
Chad Z. Hower aka Kudzu said:
You can change it to Object() and it works just fine. Its only used for a
Monitor lock. I suspect it was some old C coder trying to save some bytes,
assuming a 0 length array takes less space than an object.


Aah - That I did not see. Thats poor code if you ask me - why would they
create a variable like that to be used once?

Also naming them nearly identical, with only really a case differnce and two
letters is a very poor choice too. I really hate the actualy use of case
senstivity in C#.

The naming doesn't bug me so much as the nasty commenting. It'd be far
easier to see if they had used some whitespace.
I use case sensitive naming rather often actually. It does take some getting
used to however(in VB it wasn't possible, and in C\C++ I used a camelCasing
style naming standard for globals, so it wasn't feasible there most of hte
time), but it has its upsides when you do get used to it.
 
Back
Top