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. Why static classes?

Why static classes?

Scheduled Pinned Locked Moved C#
questiondatabaseperformancehelp
9 Posts 4 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
    mjackson11
    wrote on last edited by
    #1

    I am creating an app to interrogate a web-site. In combing through the examples on MSDN and elsewhere to learn about socket programming, I noticed that almost all the examples have static classes and methods. What is the reason for this? In my case, I am indifferent. For a variety of reason out of my control, this will be a single threaded query/reply app. Is there some performance issue involved? It would be easier to not have to declare/manage variable representing the classes. Is that why it is done? :confused:

    P C L 3 Replies Last reply
    0
    • M mjackson11

      I am creating an app to interrogate a web-site. In combing through the examples on MSDN and elsewhere to learn about socket programming, I noticed that almost all the examples have static classes and methods. What is the reason for this? In my case, I am indifferent. For a variety of reason out of my control, this will be a single threaded query/reply app. Is there some performance issue involved? It would be easier to not have to declare/manage variable representing the classes. Is that why it is done? :confused:

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Most handy for classes that contain only library routines. Otherwis, a regular class is probably more appropriate even if you intend to never have more than one instance.

      1 Reply Last reply
      0
      • M mjackson11

        I am creating an app to interrogate a web-site. In combing through the examples on MSDN and elsewhere to learn about socket programming, I noticed that almost all the examples have static classes and methods. What is the reason for this? In my case, I am indifferent. For a variety of reason out of my control, this will be a single threaded query/reply app. Is there some performance issue involved? It would be easier to not have to declare/manage variable representing the classes. Is that why it is done? :confused:

        C Offline
        C Offline
        Clifford Nelson
        wrote on last edited by
        #3

        Generally you only use static classes for stateless methods. There are also singletons, and it is generally considered good practice to access singletons through and static instance property which will instantiate the singlton class if it has not already in instantiated when the instance property is first called.

        1 Reply Last reply
        0
        • M mjackson11

          I am creating an app to interrogate a web-site. In combing through the examples on MSDN and elsewhere to learn about socket programming, I noticed that almost all the examples have static classes and methods. What is the reason for this? In my case, I am indifferent. For a variety of reason out of my control, this will be a single threaded query/reply app. Is there some performance issue involved? It would be easier to not have to declare/manage variable representing the classes. Is that why it is done? :confused:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          mjackson11 wrote:

          Is there some performance issue involved?

          Yes. Removes the need to lookup the object-instance.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

          P 1 Reply Last reply
          0
          • L Lost User

            mjackson11 wrote:

            Is there some performance issue involved?

            Yes. Removes the need to lookup the object-instance.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Sure, but that's no reason to use a static class when a regular class is more apropriate.

            L 1 Reply Last reply
            0
            • P PIEBALDconsult

              Sure, but that's no reason to use a static class when a regular class is more apropriate.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              And vice versa; a static class would be appropriate. What's the use of a singleton-pattern if all it does is replace a static class, without adding any additional value?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

              P 1 Reply Last reply
              0
              • L Lost User

                And vice versa; a static class would be appropriate. What's the use of a singleton-pattern if all it does is replace a static class, without adding any additional value?

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I don't recommend the Singleton-pattern either. One of the things I had to work around in the last year is that the System.Console class is static -- I needed a non-static class I could pass around so I had to wrap System.Console in another class, it was dreadful. The problem is worse because .net doesn't have Interfaces for TextReader and TextWriter. :mad: I had to create those as well. Making a class static or Singleton simply because you don't expect to ever need more than one is not a good idea; don't limit your options unnecessarily.

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I don't recommend the Singleton-pattern either. One of the things I had to work around in the last year is that the System.Console class is static -- I needed a non-static class I could pass around so I had to wrap System.Console in another class, it was dreadful. The problem is worse because .net doesn't have Interfaces for TextReader and TextWriter. :mad: I had to create those as well. Making a class static or Singleton simply because you don't expect to ever need more than one is not a good idea; don't limit your options unnecessarily.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Why pass a static class around in the first place? There's no need to pass a reference if there's only one class.

                  PIEBALDconsult wrote:

                  Making a class static or Singleton simply because you don't expect to ever need more than one is not a good idea; don't limit your options unnecessarily.

                  I'd expect an app to have a single stdout. Did you have multiple?

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    Why pass a static class around in the first place? There's no need to pass a reference if there's only one class.

                    PIEBALDconsult wrote:

                    Making a class static or Singleton simply because you don't expect to ever need more than one is not a good idea; don't limit your options unnecessarily.

                    I'd expect an app to have a single stdout. Did you have multiple?

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Eddy Vluggen wrote:

                    if there's only one class

                    But there might not be!

                    Eddy Vluggen wrote:

                    a single stdout

                    That's not the point. I want to pass a reference to something that implements ITextWriter, for example, I don't care that it just happens to be System.Console in this particular case. But .net doesn't allow me to do that easily for multiple reasons.

                    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