new

  • Thread starter Thread starter bill tie
  • Start date Start date
B

bill tie

byte[] myByteArray = new byte[1024];
new Random().NextBytes(myByteArray); <--- How's new used here?

Why can I do this?

Thank you.
 
byte[] myByteArray = new byte[1024];
new Random().NextBytes(myByteArray); <--- How's new used here?

It's used the same as it would be used with any reference type: to create
a new instance of the type.

Fine, new is used as an operator creating an unspecified/anonymous object.
Is this correct?
 
byte[] myByteArray = new byte[1024];
new Random().NextBytes(myByteArray); <--- How's new used here?
It's used the same as it would be used with any reference type: to create  
a new instance of the type.

Fine, new is used as an operator creating an unspecified/anonymous object..
Is this correct?

In addition to what Peter said, the line is creating a new object that
just happens to be missing the typical variable assignment. That
means you cannot reference it by name from another statement or
expression. That may have been what you meant by anonymous, but I
would definitely avoid using that terminology since it is already in
use for other C# constructs.
 
rossum said:
byte[] myByteArray = new byte[1024];
new Random().NextBytes(myByteArray); <--- How's new used here?
Rewrite the code in three lines with an explicit variable for Random:

byte[] myByteArray = new byte[1024];
Random tempRandom = new Random();
tempRandom.NextBytes(myByteArray);

Now look at the assignment on the second line. The left hand side is
a variable of type Random. Hence the right hand side is also a
variable of type Random (otherwise the compiler would complain).
Hence "new Random()" results in a variable of type Random.

To clarify:

The type of the variable on the left hand side is a reference to a
Random object, i.e. it's a variable that can hold a reference to a
Random object.

The expression "new Random()" creates a Random object on the heap and
returns the reference to it, so the value of the expression is a reference.

The value of the expression on the right hand side, which is a
reference, is assigned to the variable on the left hand side, which can
hold a reference.
 
Peter said:
[...] That
means you cannot reference it by name from another statement or
expression. That may have been what you meant by anonymous, but I
would definitely avoid using that terminology since it is already in
use for other C# constructs.

Ah, good point. I assumed that by "anonymous" he actually meant an
anonymous type, which of course that syntax wasn't. But if he's just
using the word "anonymous" in a non-technical sense, I suppose the
instance could be considered "anonymous" (I don't think I'd ever use
that word to describe an instance, but I can see why someone else
might).

That's true. The correct term for an object not stored in a variable for
use outside the expression is "temporary". For a value we'd probably say
"intermediate".
 
That's true.  The correct term for an object not stored in a variable for
use outside the expression is "temporary".  For a value we'd probably say
"intermediate".

Thanks. I was wondering if there was a commonly accepted term for
that. "Temporary" makes sense to me.
 
Brian said:
Thanks. I was wondering if there was a commonly accepted term for
that. "Temporary" makes sense to me.

There isn't really any specific term for ut, it's just an object. The
object is the same regardless if you have one variable containing a
reference to it, ten variables containing a reference to it, or zero
variables containing a reference to it.

It's quite common to have short-lived objects that aren't referenced by
a variable. For an example look at the statement:

string x = "There are " + items + " items in " + pages + " pages.";

There is a lot of "anonymous" objects here:

The literal strings "There are ", " items in " and " pages." are
constants in the assembly, but the don't have any names.

The values from the variables items and pages (asuming that they are
integers) are copied and boxed inside objects, which doesn't have any names.

There is an object array that is created to hold the references of the
literal strings and the boxed values, which doesn't have a name.

The object array is sent to string.Concat, and the only surviving object
is the final string. It's reference is stored in the variable.
 
Göran Andersson said:
There isn't really any specific term for ut, it's just an object. The

In most languages, the lifetime of such objects is special, and the term
"temporary" becomes very important. In .NET they're still garbage collected
like any other, but "temporary" is still an accurate description.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top