Static vs. Non-Static method in C#

I stum­bled across some inter­est­ing dis­cus­sions on how they dif­fer from non-sta­t­ic meth­ods, here’s a sum­ma­ry of what I learnt.

The Static Keyword

In C# terms, “sta­t­ic” means “relat­ing to the type itself, rather than an instance of the type”. You access a sta­t­ic mem­ber using the type name instead of a ref­er­ence or a val­ue, e.g. Guid.NewGuid().

In addi­tion to meth­ods and vari­ables, you can also declare a class to be sta­t­ic (since C# 2.0). A sta­t­ic class can­not be instan­ti­at­ed and can only con­tain sta­t­ic mem­bers. There are a few rules around sta­t­ic class­es:

  • Sta­t­ic class­es always derive from object, you can’t spec­i­fy a dif­fer­ent base type
  • Sta­t­ic class­es can­not imple­ment an inter­face
  • Sta­t­ic class­es can­not have any instance mem­bers
  • Sta­t­ic class­es can’t declare any con­struc­tors and the com­pil­er doesn’t cre­ate a para­me­ter­less con­struc­tor by default
  • Sta­t­ic class­es are implic­it­ly abstract, you can’t add the abstract mod­i­fi­er your­self
  • Sta­t­ic class­es may be gener­ic
  • Sta­t­ic class­es may be nest­ed, in either non-sta­t­ic or sta­t­ic class­es
  • Sta­t­ic class­es may have nest­ed types, either non-sta­t­ic or sta­t­ic
  • Only sta­t­ic, top-lev­el non-gener­ic class­es can con­tain exten­sion meth­ods (C# 3.0)


This quote from DotNetPerls’s page on sta­t­ic method just about sums every­thing up:

Here we note that sta­t­ic meth­ods are nor­mal­ly faster to invoke on the call stack than instance meth­ods. There are sev­er­al rea­sons for this in the C# pro­gram­ming lan­guage. Instance meth­ods actu­al­ly use the ‘this’ instance point­er as the first para­me­ter, so an instance method will always have that over­head. Instance meth­ods are also imple­ment­ed with the cal­lvirt instruc­tion in the inter­me­di­ate lan­guage, which impos­es a slight over­head. Please note that chang­ing your meth­ods to sta­t­ic meth­ods is unlike­ly to help much on ambi­tious per­for­mance goals, but it can help a tiny bit and pos­si­bly lead to fur­ther reduc­tions.

The arti­cle on CA1823 (see Ref­er­ences sec­tion) offers some insight into the ‘over­head’ men­tioned above:

After you mark the meth­ods as sta­t­ic, the com­pil­er will emit non­vir­tu­al call sites to these mem­bers. Emit­ting non­vir­tu­al call sites will pre­vent a check at run­time for each call that makes sure that the cur­rent object point­er is non-null. This can achieve a mea­sur­able per­for­mance gain for per­for­mance-sen­si­tive code.

As stat­ed above, in any real world appli­ca­tion you’re not like­ly to ever feel the dif­fer­ence in per­for­mance when you con­vert non-sta­t­ic meth­ods to sta­t­ic or vice ver­sa. If your appli­ca­tion is not per­form­ing to the lev­el you have come to expect then I’d rec­om­mend using a pro­fil­er and get an accu­rate pic­ture of what’s going inside your code at run­time. I have used both RedGate’s ANTS pro­fil­er and Jet­Brains’ Dot­Trace and both will do the job more than suf­fi­cient­ly!

Parting Thoughts…

Scott Wis­niews­ki said in his answer to this Stack­Over­flow ques­tion that in a large project (rough­ly defined as a project with over 200k lines of code) you should avoid mak­ing meth­ods that ‘can be made sta­t­ic’ sta­t­ic because:

In a large code base, how­ev­er, the sheer num­ber of call sites might make search­ing to see if it’s pos­si­ble to con­vert a sta­t­ic method to a non sta­t­ic one too cost­ly. Many times peo­ple will see the num­ber of calls, and say “ok… I bet­ter not change this method, but instead cre­ate a new one that does what I need”.

Whilst this state­ment cer­tain­ly has some truth to it, avoid­ing sta­t­ic meth­ods should nev­er be the solu­tion to this par­tic­u­lar prob­lem, espe­cial­ly when you con­sid­er that there are some very good pro­duc­tiv­i­ty tools out there that can do this for you! So if you don’t want to waste pre­cious man hours on iden­ti­fy­ing meth­ods to be made sta­t­ic then you should seri­ous­ly con­sid­er pur­chas­ing tools such as Resharp­er.

Sta­t­ic class­es on the hand, impose a few more restric­tions (in The Sta­t­ic Key­word sec­tion above) which make them dif­fi­cult to incor­po­rate with­in a high­ly flex­i­ble struc­ture as they can­not imple­ment inter­faces and do not sup­port poly­mor­phism (can­not derive from a base type). See Mark Rasmussen’s answer to this Stack­Over­flow ques­tion, he goes into detail on a num­ber of draw­backs with using sta­t­ic class­es.


Dot­Net­Perls on Sta­t­ic Method

Stack­Over­flow ques­tion – Sta­t­ic vs. Non-Sta­t­ic method per­for­mance C#

Stack­Over­flow ques­tion – Should C# meth­ods that can be sta­t­ic sta­t­ic

Stack­Over­flow ques­tion – What is a sta­t­ic class

Stack­Over­flow ques­tion – When to use sta­t­ic class

MSDN – Writ­ing faster man­aged code : know what things cost

Code Analy­sis CA1822 : Mark mem­bers as sta­t­ic

Dot­Net­Perls on Sin­gle­ton vs. Sta­t­ic Class