Static vs. Non-Static method in C#

You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!

I stumbled across some interesting discussions on how they differ from non-static methods, here’s a summary of what I learnt.

The Static Keyword

In C# terms, “static” means “relating to the type itself, rather than an instance of the type”. You access a static member using the type name instead of a reference or a value, e.g. Guid.NewGuid().

In addition to methods and variables, you can also declare a class to be static (since C# 2.0). A static class cannot be instantiated and can only contain static members. There are a few rules around static classes:

  • Static classes always derive from object, you can’t specify a different base type
  • Static classes cannot implement an interface
  • Static classes cannot have any instance members
  • Static classes can’t declare any constructors and the compiler doesn’t create a parameterless constructor by default
  • Static classes are implicitly abstract, you can’t add the abstract modifier yourself
  • Static classes may be generic
  • Static classes may be nested, in either non-static or static classes
  • Static classes may have nested types, either non-static or static
  • Only static, top-level non-generic classes can contain extension methods (C# 3.0)

Performance

This quote from DotNetPerls’s page on static method just about sums everything up:

Here we note that static methods are normally faster to invoke on the call stack than instance methods. There are several reasons for this in the C# programming language. Instance methods actually use the ‘this’ instance pointer as the first parameter, so an instance method will always have that overhead. Instance methods are also implemented with the callvirt instruction in the intermediate language, which imposes a slight overhead. Please note that changing your methods to static methods is unlikely to help much on ambitious performance goals, but it can help a tiny bit and possibly lead to further reductions.

The article on CA1823 (see References section) offers some insight into the ‘overhead’ mentioned above:

After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code.

As stated above, in any real world application you’re not likely to ever feel the difference in performance when you convert non-static methods to static or vice versa. If your application is not performing to the level you have come to expect then I’d recommend using a profiler and get an accurate picture of what’s going inside your code at runtime. I have used both RedGate’s ANTS profiler and JetBrains’ DotTrace and both will do the job more than sufficiently!

Parting Thoughts…

Scott Wisniewski said in his answer to this StackOverflow question that in a large project (roughly defined as a project with over 200k lines of code) you should avoid making methods that ‘can be made static’ static because:

In a large code base, however, the sheer number of call sites might make searching to see if it’s possible to convert a static method to a non static one too costly. Many times people will see the number of calls, and say “ok… I better not change this method, but instead create a new one that does what I need”.

Whilst this statement certainly has some truth to it, avoiding static methods should never be the solution to this particular problem, especially when you consider that there are some very good productivity tools out there that can do this for you! So if you don’t want to waste precious man hours on identifying methods to be made static then you should seriously consider purchasing tools such as Resharper.

Static classes on the hand, impose a few more restrictions (in The Static Keyword section above) which make them difficult to incorporate within a highly flexible structure as they cannot implement interfaces and do not support polymorphism (cannot derive from a base type). See Mark Rasmussen’s answer to this StackOverflow question, he goes into detail on a number of drawbacks with using static classes.

References:

DotNetPerls on Static Method

StackOverflow question – Static vs. Non-Static method performance C#

StackOverflow question – Should C# methods that can be static static

StackOverflow question – What is a static class

StackOverflow question – When to use static class

MSDN – Writing faster managed code : know what things cost

Code Analysis CA1822 : Mark members as static

DotNetPerls on Singleton vs. Static Class

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and I help companies go faster for less by adopting serverless technologies successfully.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you enrol now you can also get 15% OFF with the promo code “yanprs15”.

Enrol now and SAVE 15%.


Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.


Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!


Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!