Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple Question to using a object instance

Simple Question to using a object instance

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    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:

    M 1 Reply Last reply
    0
    • M MarkPhB

      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:

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • M Martin 0

        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

        M Offline
        M Offline
        MarkPhB
        wrote on last edited by
        #3

        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 );
        
        L 1 Reply Last reply
        0
        • M MarkPhB

          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 );
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            Very Good point Luc! I would use a "using" block for that!

            All the best, Martin

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups