C # instance object

What's the difference between:

Object o = new Object();
o.foo();

and

new Object().foo();

(if I don't need a link after that)?

Are there reasons to use one instead of the other (e.g. memory usage)?

+3
source share
5 answers

There is no difference in deadlines.

There may be a difference in debugging:

  • This can be conveniently broken after the object is created, but before it is foo()called
  • It may be useful to subsequently check the value of a variable.
  • , . ( , , NullReferenceException, , ).

, - , .

+8

, .

+6

o, .
(JIT), , .

. , .

+2

.

, ( ), ildasm, , - witll .

EDIT: actually it is sometimes easier for me to debug when declaring a variable. Easier to check, therefore easier to debug.

0
source

EDIT: Invalid code removed

The difference is that in the first case, you can see the object created in the debugger.

-1
source

Source: https://habr.com/ru/post/1743160/


All Articles