473,320 Members | 2,145 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,320 software developers and data experts.

new

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

Why can I do this?

Thank you.

Aug 7 '08 #1
11 1247
On Wed, 06 Aug 2008 17:02:00 -0700, bill tie
<bi*****@discussions.microsoft.comwrote:
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.

In this particular case, it appears that the instance is used just once,
to fill the byte[] instance with random numbers. And indeed, there
_could_ be a bug here depending on what other random numbers the
application is trying to generate at the same time. But there's nothing
fundamentally wrong with the code.
Why can I do this?
Why wouldn't you be able to? "new Random()" is an expression of type
Random, so you can do anything with it that you could with any other
expression of the same type.

Pete
Aug 7 '08 #2
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?

Aug 7 '08 #3
On Wed, 6 Aug 2008 17:02:00 -0700, bill tie
<bi*****@discussions.microsoft.comwrote:
>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.

Since new Random generates a temporary variable of type Random, then
there is no need to explicitly declare it if we do not need to use it
again. The compiler can deal with setting up the variable itself
without us needing to actually declare it.

Think of "new" as a way to invoke the constructor and to return the
constructed object. The returned object can either be assigned to an
explicit variable (as in the 3-line version) or be used once and
thrown away (as in the 2-line version).
>
Why can I do this?
Because "new" returns a fully constructed object of the indicated
type.

HTH

rossum
>
Thank you.
Aug 7 '08 #4
On Wed, 06 Aug 2008 17:43:01 -0700, bill tie
<bi*****@discussions.microsoft.comwrote:
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?
No, not correct at all. In the example you posted, the type certainly
_is_ specified, and is not anonymous. The type is "Random" and it's being
instantiated the same way it'd ever be instantiated: with the expression
"new Random()".
Aug 7 '08 #5
On Aug 6, 7:43*pm, bill tie <bill...@discussions.microsoft.comwrote:
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.
Aug 7 '08 #6
On Wed, 06 Aug 2008 19:04:17 -0700, Brian Gideon <br*********@yahoo.com>
wrote:
[...] 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).

Pete
Aug 7 '08 #7
rossum wrote:
On Wed, 6 Aug 2008 17:02:00 -0700, bill tie
<bi*****@discussions.microsoft.comwrote:
>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.
Since new Random generates a temporary variable of type Random, then
there is no need to explicitly declare it if we do not need to use it
again. The compiler can deal with setting up the variable itself
without us needing to actually declare it.

Think of "new" as a way to invoke the constructor and to return the
constructed object. The returned object can either be assigned to an
explicit variable (as in the 3-line version) or be used once and
thrown away (as in the 2-line version).
>Why can I do this?
Because "new" returns a fully constructed object of the indicated
type.

HTH

rossum
>Thank you.

--
Göran Andersson
_____
http://www.guffa.com
Aug 7 '08 #8
Peter Duniho wrote:
On Wed, 06 Aug 2008 19:04:17 -0700, Brian Gideon
<br*********@yahoo.comwrote:
>[...] 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".
>
Pete

Aug 7 '08 #9
On Aug 7, 9:29*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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.
Aug 7 '08 #10
Brian Gideon wrote:
On Aug 7, 9:29 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>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.
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
_____
http://www.guffa.com
Aug 7 '08 #11
Göran Andersson wrote:
Brian Gideon wrote:
>On Aug 7, 9:29 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>>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.

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

Aug 7 '08 #12

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.