Simple Question to using a object instance
-
-
Hi, is there a problem when i use an object instance like this ?
( new AnyClass() ).SomeMethode();
instead of
AnyClass ac = new AnyClass(); ac.SomeMethode();
The compile dont have a problem with it but i asking me whether it is good or bad :confused:
Hello, No problem, but: I wouldn't want to see code like this in one of my projects. If it makes sence to do this:
MarkPhB wrote:
( new AnyClass() ).SomeMethode();
,there is not need to use a class at all, I guess. You could simply use a static method.
All the best, Martin
-
Hello, No problem, but: I wouldn't want to see code like this in one of my projects. If it makes sence to do this:
MarkPhB wrote:
( new AnyClass() ).SomeMethode();
,there is not need to use a class at all, I guess. You could simply use a static method.
All the best, Martin
-
Thanks for the answer =) i use this sometimes when i have a class with have a methode that i need only one time. Like
( new Form1() ).ShowDialog()
or
( new RNGCryptoServiceProvider() ).GetBytes( randomBytes );
Hi, I don't like your
( new Form1() ).ShowDialog();
since Form1 (assuming inheriting from Form) offers a Dispose() method which you should call once you don't need the Form1 instance any longer. :)Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi, I don't like your
( new Form1() ).ShowDialog();
since Form1 (assuming inheriting from Form) offers a Dispose() method which you should call once you don't need the Form1 instance any longer. :)Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google