473,379 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

New as applied to arrays

Given:

class MyClass
{
private int var;

public MyClass ()
{
// Any old init code
var = 3;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
MyClass [] array = new MyClass [14];
}
}

What actually happens when new is called? I've looked at the IL and it
seems that the newarr command is called. I've not been any detailed
info on newarr and what it actually does.

The constructor in MyClass is not called so no instances of MyClass are
created. What is new actually doing?

Thanks in advance,

Nick

Apr 13 '06 #1
2 1006
nick_nw wrote:

<snip>
The constructor in MyClass is not called so no instances of MyClass are
created. What is new actually doing?


new MyClass[14] creates an array with 14 slots, a bit like:

MyClass x1;
MyClass x2;
MyClass x3;
MyClass x4;
etc

Each of these is initially null, because that's the default value for
reference types. (When you create an array, it is filled with the
default value for that type, whatever that type is.)

Jon

Apr 13 '06 #2
Nick,

When you create an array you create an object that has slots to hold 14, in
your case, references to instnaces of your class. It doesn't actually
instantiates objects of that class of yours - the array is empty in the
beginning..
--
HTH
Stoitcho Goutsev (100)

"nick_nw" <ng****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Given:

class MyClass
{
private int var;

public MyClass ()
{
// Any old init code
var = 3;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
MyClass [] array = new MyClass [14];
}
}

What actually happens when new is called? I've looked at the IL and it
seems that the newarr command is called. I've not been any detailed
info on newarr and what it actually does.

The constructor in MyClass is not called so no instances of MyClass are
created. What is new actually doing?

Thanks in advance,

Nick

Apr 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
0
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ?...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: natty2006 | last post by:
Submission Deadline extended: 13 November 2006 ************************************************************* IADIS INTERNATIONAL CONFERENCE APPLIED COMPUTING 2007 February 17-20, 2007 -...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
0
by: tutorialwebs | last post by:
Applied cryptography text books http://www.365x24live.com/Applied%20Cryptography/ewtoc.html http://www.365x24live.com/Applied%20Cryptography/ewtoc.html...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.