7 Replies - 620 Views - Last Post: 26 September 2013 - 07:09 AM Rate Topic: ****- 1 Votes

#1 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 229
  • Joined: 28-August 13

Static Classes

Posted 23 September 2013 - 03:52 PM

This is something I've never been able to fully understand. I know that static classes can't be instantiated, and that they are pre-allocated to memory during start up?, but what are the correct situations to use the static modifier?

Everything I've came across so far says that "helper" classes that don't have state are an ideal situation to use the static modifier. But what if the class manages some kind of "state", but you only ever need 1 instance of the class, and it needs to live for the lifetime of the application? I understand the "singleton pattern" can be applied in this situation, but would just marking the class and all its members as static be appropriate design as well? Would making it a normal class and keeping it as a global structure be better design?

Basically, I want to learn everything there is to 'static'. Memory implications, when it's appropriate to use it, when it's not appropriate to use it, performance, etc. Obviously this might be too much to explain in this topic, but if you know of any good c# books that cover the static modifier in depth, I would appreciate a link to the book. Any good online references to some of my questions would also be appreciated, but everything I've came across so far doesn't answer everything I'm wondering about 'static'.

This post has been edited by Rhino1111: 23 September 2013 - 04:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Static Classes

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Static Classes

Posted 23 September 2013 - 04:57 PM

Here is the MSDN page on Static Classes and when to use them.

MSDN said:

A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class. That is, you apply the members of the class by specifying the class name and the method name


There is an excellent discussion here at SO.

The Math class is a good example of their use.

A rough summary would be that they can be useful for a utility-class, containing methods that you might normally create as (standalone) functions in other languages. A Singleton is typically more useful for a Class from which an instance is required, but only once, whereas a Static Class may not even be used in a Project.

This post has been edited by andrewsw: 23 September 2013 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Static Classes

Posted 23 September 2013 - 05:08 PM

Rhino1111 said:

But what if the class manages some kind of "state", but you only ever need 1 instance of the class, and it needs to live for the lifetime of the application?

This describes a Singleton, specifically not a Static Class which, by its definition, does not maintain state.

Rhino1111 said:

Would making it a normal class and keeping it as a global structure be better design?

This is an approach that is often taken, rather than specifically implementing the Singleton Pattern. However, if the possibility of there being two instances would be problematic, such as two connection objects or two library instances, then a Singleton resolves this concern. However:

What's wrong with Singletons? SO topic.

This post has been edited by andrewsw: 23 September 2013 - 05:20 PM

Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Static Classes

Posted 24 September 2013 - 06:59 AM

Another great use for static classes is a "constants" class. Since classes can contain subclasses that can also be static, you can create a structure that contains your constants in a logical arrangement.

For example:

public static class Constants
{
  public static class AccountAttributes
  {
    public const string Name = "col_name";
  }
}


var col = columns[Constants.AccountAttributes.Name];

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Static Classes

Posted 24 September 2013 - 07:04 AM

Surprised nobody has mentioned it yet. Extension Methods need to be implement inside of a static class.

public static class Exts
{

  public bool IsBetween(this int value, int lower, int upper)
  {
    return (lower <= value) && ( value < upper);
  }
}


Example

if( x.IsBetween(0, 100) ) 


This post has been edited by AdamSpeight2008: 24 September 2013 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Static Classes

Posted 25 September 2013 - 03:24 AM

You can also use classes to make variables that are accessible by all forms. Although there are better ways of doing this.

A plastic example: If you catch yourself doing something multiple times throughout a single program, you make a method. If you want that method to be globally accessible you make it static and put it in a static class. If you want it to be available to multiple programs you put it in a DLL file.

This post has been edited by RexGrammer: 25 September 2013 - 03:25 AM

Was This Post Helpful? 0
  • +
  • -

#7 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: Static Classes

Posted 25 September 2013 - 03:42 AM

@AdamSpeight2008 Extension methods need to be static.
Was This Post Helpful? 0
  • +
  • -

#8 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Static Classes

Posted 26 September 2013 - 07:09 AM

The key advantage to static classes or methods are as you have mentioned the global availability of them without the need to instantiate them.

In theory, there is nothing stopping a static class from having state other than it being a bad practice. If it's accessed from multiple places and isn't properly designed to be a singleton then it will also cause lot's of headaches.

The big disadvantage of static classes is that they are harder to test. They don't allow for dependency injection which is a pretty much fundamental pattern if you want to be able to write a decent library of unit test's for your project.

I would say the biggest potential use for static methods not classes is a way you could implement the factory pattern but I would still avoid that and use a factory object as then it can be tested more successfully

This post has been edited by andrewsw: 26 September 2013 - 01:11 PM
Reason for edit:: Removed full quote of OP

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1