Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the ways to declare a class that cannot be instantiated?

What are the ways to create a non-instantiable class? One way is by declaring it as an abstract class. Is it possible to do it by making the class constructor as private? Is a sealed class, non-instantiable? And, are there any other ways to do it in C#?

like image 662
kakkarot Avatar asked Oct 08 '15 07:10

kakkarot


1 Answers

Marking a class as abstract or static (they are mutually exclusive) are the only two ways. Marking all constructors as private does not make the class uninstantiateable since the class can still construct itself, and others might be able to do it via reflection.

like image 189
sara Avatar answered Nov 14 '22 23:11

sara