468,167 Members | 1,921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,167 developers. It's quick & easy.

Can someone explain this use of "new"?

I have another newbie question I could use some help with. I ran across this
code in one of the MS training guides:

public class NullTokenVisitor
{...}

public class Application
{
public static void Main()
{
new NullTokenVisitor();
}

}
I was surprised that this code compiles and runs. It appears that I am
creating a new instance of the NullTokenVisitor class without assigning the
object reference to a variable. Am I understanding this code correctly? If
so, what would be the purpose of using this type of expression outside of a
purely academic exercises?
Jan 23 '06 #1
7 1107
the only thing matters to the compiler is syntax (c#).
theres *nothing* wrong to that code.
Jan 23 '06 #2
It would mean that any code in the NullTokenVisitor's constructor will
be called. Perhaps the code in that constructor has some affect on
objects outside of its scope. Maybe it "adds 1" to the "Visitor" table
in a database or something. God knows really... whats in the
constructor code?

Its kind of along the same lines as something like this:
Dim d as double = 45.7214
Math.Floor(d)

I can call Math.Floor(d) and not assign its result to anything. It
would be pointless, but the compiler accepts it, and still performs the
flooring function....

This help?

I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #3
Does that mean you're overpaid by a factor of 4? ;o)
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Steven Nagy" <le*********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
[ ]
I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #4
At my pay rate, I'm practically a volunteer...

Jan 23 '06 #5
Steven,

Thanks for your input. In the example you provide of Math.Floor() I sort of
assumed that Floor is a static method so it makes sense that you would call
it without instantiating an object. What good is it to fire the constructor
in a class if you don't assigne the object reference to a variable? The
object is lost and now just wasted memory. It bakes my noodle is that the C#
compiler would even allow me to instantiate an object without assigning it to
a variable. The sample code I provided uses it as an artifical test just to
make sure that the class would instantiate without throwing an error. I
wouldn't expect to ever leave that sort of code in a release application and
I can't figure out what sort of practical use such a statement could have in
a live application.

"Steven Nagy" wrote:
It would mean that any code in the NullTokenVisitor's constructor will
be called. Perhaps the code in that constructor has some affect on
objects outside of its scope. Maybe it "adds 1" to the "Visitor" table
in a database or something. God knows really... whats in the
constructor code?

Its kind of along the same lines as something like this:
Dim d as double = 45.7214
Math.Floor(d)

I can call Math.Floor(d) and not assign its result to anything. It
would be pointless, but the compiler accepts it, and still performs the
flooring function....

This help?

I could be wrong tho... (I usually rate my chances at around 25%
correctness)

Jan 23 '06 #6
Why?

Firstly, the object isn't lost: the GC knows exactly where to find it, and
will come a-knockin' very soon.

Secondly, the compiler has no way of knowing what your ctor does; it could
increment static variables, it could chhose to throw an exception (perhaps
arguably unwisely, but that's another issue), it could choose to tweak any
of the variables in the arguments to the ctor.

Thridly - in the "lost" scenario, your instance ctor could also perhaps (not
100% sure - haven't tried it) add itself to a static collection, so even
less "lost" - in fact, if it was a form, the ctor could also perhaps show
itself? (again not tried, and again, not perhaps adviseable).

OK, fine, all of the above scenarios would be better done in better ways,
but I see no sensible reason that the compiler should know this. I'd rather
it simply collected an object occasionally than have me wondering why
something that looks logical doesn't work...

Marc
Jan 23 '06 #7
Exactly.

The point is you can do it, just like you code like this:

MyObject.x = 1;
MyObject.x = 2;
MyObject.x = 1;

Seems completely pointless! But the compiler lets you do it...
And it could be that the X property is doing more than what the code
suggests... it could be launching missiles at the soviet union in the
related property getter.

Jan 24 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by b83503104 | last post: by
1 post views Thread by sven_c_t | last post: by
12 posts views Thread by Ola Johansson | last post: by
1 post views Thread by mmcc128 | last post: by
51 posts views Thread by Tony Sinclair | last post: by
7 posts views Thread by Jim Land | last post: by
350 posts views Thread by Lloyd Bonafide | last post: by
reply views Thread by Manousos | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.