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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. using static

using static

Scheduled Pinned Locked Moved C#
performancedatabasehelpquestion
3 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.
  • P Offline
    P Offline
    pgraeve
    wrote on last edited by
    #1

    I find it much easier if a class is going to have an empty constructor to make the class's properties and methods static. But I haven't read or seen anything on the "ramifications" of using static a bunch. One one hand it seems as though it would help the system's performance since static methods and properties are loaded on system startup - but does this dramatically affect the system's "startup" time; and memory usage - or enough to worry about it anyway? And is calling static methods etc. offer any significant performance increase over intializing a class with an empty constructor and then utilizing its methods etc.? Has anyone discovered some good "rules of thumb" for determining when to make a method or property static? I typically create static sql connection strings, color properties, etc... based on the above questions, is this a good idea or not?

    C H 2 Replies Last reply
    0
    • P pgraeve

      I find it much easier if a class is going to have an empty constructor to make the class's properties and methods static. But I haven't read or seen anything on the "ramifications" of using static a bunch. One one hand it seems as though it would help the system's performance since static methods and properties are loaded on system startup - but does this dramatically affect the system's "startup" time; and memory usage - or enough to worry about it anyway? And is calling static methods etc. offer any significant performance increase over intializing a class with an empty constructor and then utilizing its methods etc.? Has anyone discovered some good "rules of thumb" for determining when to make a method or property static? I typically create static sql connection strings, color properties, etc... based on the above questions, is this a good idea or not?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If a method does not rely on state within the class ( i.e. member variables of a specific instance of a class ), then it should be static, or possibly part of a fascade class. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      1 Reply Last reply
      0
      • P pgraeve

        I find it much easier if a class is going to have an empty constructor to make the class's properties and methods static. But I haven't read or seen anything on the "ramifications" of using static a bunch. One one hand it seems as though it would help the system's performance since static methods and properties are loaded on system startup - but does this dramatically affect the system's "startup" time; and memory usage - or enough to worry about it anyway? And is calling static methods etc. offer any significant performance increase over intializing a class with an empty constructor and then utilizing its methods etc.? Has anyone discovered some good "rules of thumb" for determining when to make a method or property static? I typically create static sql connection strings, color properties, etc... based on the above questions, is this a good idea or not?

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        As Christian implied, you should use instance members when the class requires state for each instantiation. Statics are also executed synchronously within a single AppDomain so forget multi-threaded applications. Finally, this makes for a terrible object-oriented design and throws polymorphism out the window since you can neither mark a static member as abstract nor override an inheritted static member. You're basically turning the .NET Framework into a procedural construct. If that's what you're after, you don't want object-oriented runtimes like the .NET CLR or Java Runtime Environment. Static members are best reserved for class factories, non-instance operations (like binary operators, non-class specific methods like Control.MousePosition, and the like). Everything else should be an instance member. In a good OO design, you'll end up using mostly instance members.

        Microsoft MVP, Visual C# My Articles

        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