views:

424

answers:

8

What is the easiest way to make C# not instantiate a class unless inherit?

Sounds weird but i dont want to explain the why. I have a base class and two class that inherit it. I want to use the derived class only and not the base. The derive class does not have any extra functions. Whats the easiest way to NOT allow me to write new BaseClass(); so i dont accidentally use it? I do have functions that operate on the base class and not the derived.

+27  A: 

Make the class an abstract base class.

abstract class Person {

}

class Programmer : Person {

}

var person = new Person();          // compile time error
var programmer = new Programmer();  // ok
Finglas
You can't even see the class name on the intellisense list when trying to instantiate it which is awesome for awareness :)
Braveyard
+1 @Dockers (15char)
acidzombie24
+4  A: 

Can the base class be Abstract? That would do it.

Terry Donaghe
+5  A: 

Set the class to abstract:

public abstract BaseClass
{



}

http://msdn.microsoft.com/en-us/library/sf985hc5.aspx

Kevin
+4  A: 
public abstract class BaseClass {
...
}
chillitom
+4  A: 

instead of the normal:

"public Class MyClass"

make it

"public abstract Class MyClass"
Jamie Keeling
+2  A: 

Make the constructor of BaseClass protected:

public class BaseClass {

   protected BaseClass()
   {
      // DO SOMETHING
   }

}


public class Derived : BaseClass {

   public Derived() {}
}
Dominik Fretz
Only if he can't make the base class abstract for some weird reason.
Terry Donaghe
I wouldn't do this.
Finglas
A: 

You make an abstraction with the keyword Abstract.

If you type abstract into a google definition it will tell you that it means "part of" or "not completetly defined" which means exactly what you are looking for.

You cannot create what you do not have meaning that you need to have a complete structure before you can create it, and an abstract class is not complete untill it's inherited!

It's the fundamentals of Object Oriented Programming. Which might bring you to something called Polymorphism which let's you do other very interesting stuff!

Filip Ekberg
Could the one who downvoted please tell me what was wrong with the provided answer?
Filip Ekberg
-1: `abstract` is not related to polymorphism. You can have polymorphism without `abstract`.`
John Saunders
I didn't downvote you but its probably some ignorant person who wants sample code (in addition, some one wants to be spoon fed rather then researching). Don't let losing rep points get to you. It happens all the time to me :).
JonH
@John Saunders, it does not relate to the keyword abstract, no. But once you get into Object Oriented Programming it will come in hand to know both Polymorphism and Abstraction. Therefore, in my point of view they go hand in hand. I edited my answer to reflect more on my thoughts.
Filip Ekberg
@Filip: I downvoted you, and I said why.
John Saunders
@Filip: I'm not sure the OP was looking for a lecture on OO, but rather on how to prevent the base class from being instantiated.
John Saunders
I downvoted you to because i would love to see a -3 accepted answer and i shall accept it too. It works like a charm :) (leave a comment if you want me to change my down to an upvote)
acidzombie24
@John, The code-snippet to solve the problem was already provided. In addition to that it's important to know the concepts and he might find other interesting posibilities. And knowing OOP, Abstraction and the fact that you cannot instanciate an abstract class is fundamental so a lecture in OOP might be good.
Filip Ekberg
@acidzombie24, I would love to see that too ;)
Filip Ekberg
I also liked the way Jamie Keeling phrased his answer. http://stackoverflow.com/questions/2066841/easiest-way-to-make-c-not-instantiate-a-class-unless-inherit/2066865#2066865
acidzombie24
@Filip: we disagree on the purpose of answering a question. My downvote stands, and apparently several others agree. You have no reason to believe that the OP had any interest in these interesting possibilities.
John Saunders
Hehe there were a lot of good answers.
Filip Ekberg
@John, We agree to disagree. I still however think that when it comes to abstraction, you enter the field of OOP and once you do, You need to have some knowledge about not only Abstraction but also Polymorphism.
Filip Ekberg
@Filip: the OP gave no indication he wanted anything more than to get his problem solved. For all you know, he may already be well-versed in polymorphism, and just wanted a way to make his base class not be instantiable. The fact that the mechanism to solve his problem is also used in polymorphism is not relevant to his question.
John Saunders
@John, I did not read this only as a "Give me some code now please". Its up to the reader of the question to interpret it.
Filip Ekberg
BTW i do know much about polymorphism, oops design and programming. I just forgot the abstract keyword existed since i normally work with C++. (which is why i mention i had no virtual functions. I didnt want to force each derive to implement a dummy virtual function)
acidzombie24
A: 

The way you describe your need, making your class abstract is the way to go. It will stop you from instantiating the base class and will allow you to call the functions on it. This will also allow you to mark functions and properties as abstract and force the inherited classes to implement them.

If you had no need to share code, an interface would be a nice option. It allows you to implement it in a way very similar to an abstarct class and casting your implementations into it.

Hope this adds some value.

Haas