开发者

Abstract classes vs Static classes in C# [duplicate]

This question already has a开发者_如何学Cnswers here: Closed 11 years ago.

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


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.


It's true that it's not possible to create an instance of an abstract or static class but that's about where the similarities end.

  • Can inherit from abstract cannot inherit from static
  • Can have instance methods on abstract cannot have instance on static
  • An abstract class can implement an interface a static class cannot

Fundamentally they are trying to serve two different purposes

  • An abstract class forms a blue print / pattern which is then implemented in derived classes in different and (hopefully) transparent ways
  • A static class is simply a container for a collection of possibly related static methods


An abstract class is a class that must be inherited to be used — it can only be inherited.
You can create instances of classes that inherit it.

A static class is a class that cannot have instances at all; such a class only has static members.
static classes cannot be inherited, nor can they inherit other classes.


True, an abstract class cannot be instantiated, but instead forms the base of other classes. The benefit is that you can put functionality into the abstract class to aid reuse.

A static class is one that is instantiated by the CLR when required. There can only be one instance of it any time. Using static classes is very useful, but care must be taken around threading and simultaneous access.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜