0

Does a compiler compiles an abstract class in C#?

If yes then how does compiler treats an abstract class, like a normal class or there is some difference.

And if no then how does it allow inheritance of the data members and a member function of an abstract class in derived class.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • 2
    Yes, the compiler compiles abstract classes. It generates the IL for abstract classes - because the CLR understands abstract classes too. It's different from a non-abstract class in all the ways described here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract Could you be a bit more specific about what you're asking? – Jon Skeet Jun 10 '18 at 07:47
  • I think you can answer yourself by using a decompiler to check if the abstract class is present or not :-) – CodeNotFound Jun 10 '18 at 07:51
  • If you have an abstract class called `C`, it is compiled to following IL: `.class private auto ansi abstract beforefieldinit YourNamespace.C extends [mscorlib]System.Object {}` – Sebastian Hofmann Jun 10 '18 at 08:30
  • 2
    You can compile a library project containing an abstract class into a dll, and then use that dll elsewhere to extend from that abstract class - so that abstract class must have been compiled into that dll. – Hans Kesting Jun 10 '18 at 09:38
  • I am curios, how come you think that abstract classes may not compile ? – Amit Jun 10 '18 at 10:06
  • Because an Abstract class is not to be considered as real class for c# . Compiler consider it as imaginary class. – Prashant Choudhary Jun 11 '18 at 13:17
  • @PrashantChoudhary what makes you think that? Why shouldn't it be a "real" class or even "imaginary"? – Sebastian Hofmann Jun 12 '18 at 19:21

1 Answers1

1

An abstract class indeed behaves like a normal class in IL, the only difference is the added abstract flag/keyword.

A generic public class in IL looks like this (without body)

.class public auto ansi beforefieldinit 
  NAMESPACE.CLASS_NAME
    extends [mscorlib]System.Object

an abstract class looks like this

.class public abstract auto ansi beforefieldinit 
  NAMESPACE.CLASS_NAME
    extends [mscorlib]System.Object

I can recommend you creating test assemblys and browsing them with dotPeek from JetBrains (its a free tool). You can look trough the code and the IL code at the same time.

2gJava
  • 59
  • 6