Abstract classes vs Static classes in C#

Dan picture Dan · Apr 14, 2011 · Viewed 28.7k times · Source

Possible Duplicate:
What's the difference between an abstract class and a static one?

Hello
I Would like to know what are all the differences between abstract classes and static classes in C#
When do I use what and why?

Is it true the abstract class is a class which we cannot create instances of it?
Thanks

Answer

Eric Lippert picture Eric Lippert · Apr 14, 2011

I would like to know what are all the differences between abstract classes and static classes in C#.

Don't ask questions like that. I could spend hours listing hundreds of differences, none of which would be relevant to you.

What is the most important difference between abstract classes and static classes in C#?

That's more like it.

An abstract class is usually intended to model something in a type hierarchy. For example, a truck is a kind of vehicle, and an airplane is a kind of vehicle, so you might have a base class Vehicle and derived classes Truck and Airplane. But "Vehicle" is abstract; there are no vehicles which are just vehicles without being some more specific kind of thing. You represent that concept with an abstract class.

A static class by contrast is not intended to model anything at all. It's just a convenient way of storing a bunch of code. Really it shouldn't be a class at all; VB made a better choice by calling such things "modules" rather than "classes". Though technically they inherit from object, static classes are logically not really in a type hierarchy at all. They're just a bucket for holding static members.

Static classes are often used as containers of extension methods.

When do I use what and why?

Use an abstract class when you want to build a model of the form "an X is a kind of Y". Like "a Car is a kind of Vehicle" or "a Square is a kind of Shape" or "a Magazine is a kind of Publication", where the "Y" is an abstract concept. Don't use it for things like "an Employee is a kind of Person" -- Person should be concrete. Person is not an abstract concept; there are people who are just people, but there are no vehicles that are not something else.

Use a static class when you want to make extension methods, or when you have a bunch of code that fits logically together but does not associate with any object. For example, if you have a bunch of related math routines, that's a good candidate for a static class.

Is it true the abstract class is a class which we cannot create instances of it?

No. That is not true. You can create instances of an abstract class. You do so by creating an instance of a more derived class.

Vehicle v = new Car();

Clearly v refers to an instance of Vehicle, and therefore you can create an instance of an abstract class. What you cannot do is create an instance of an abstract class that is not also an instance of a more derived concrete class.

By contrast, you cannot create an instance of a static class at all.

Here's a question you didn't ask:

What is the implementation relationship between static classes and abstract classes?

Static classes actually do not really exist as a concept in the CLR. When you say "static" on a class, what we actually do is generate an abstract sealed class with no public constructors. Since it is abstract, you cannot create one directly. Since it is sealed, you cannot create a more derived class and instantiate that.