I am writing an abstract class because I want to provide a few commonly used methods, require a few methods that will be too specific, and allow some methods to "extended". After bumping into a compiler error I am wondering if anybody can explain the differences between the extern, abstract, and partial keywords. What do they mean and when/where should I use them?
extern is unlikely to be something you want to use. It means that the method is implemented, but implemented externally - and typically used in interop scenarios where you're defining a method implelemented in external code.
abstract, on the other hand, means you're defining the API for the method, but not providing an implementation. The subclass will have to provide the implementation for any methods or properties marked abstract
, or be abstract
itself. If you want to make a base class and have a method or property that must be implemented by subclasses, you'll want to use abstract
.
partial classes and methods are merely a compilation tool. They allow you to use multiple files to define your type. This is mostly used with automatically generated code (ie: a designer will put the designer generated code into a separate file defining a partial class, so you can 'fill in' the missing pieces without looking at the implementation details). This is unlikely something you'll use directly for defining a class.
An extern
method is typically being implemented via a dll-import (P/Invoke) - so it does have an implementation - you just can't see it.
A partial
method is useful mainly with code-generation as a way to inject functionality into the generated code. They are optional, private only, and only exist if you provide the other half. As such there are also some limitations around return/out values to assure definite assignment. Calls to partial methods will be omitted entirely by the compiler if there is no implementation.
An abstract
method is where the implementation has to be provided by a derived type. The runtime ensures you can't have an instance if there are still unimplemented abstract methods, so you are assured that they will exist at runtime.
There seems to be some good answers here but I would still write to make it more clear
Extern
From C# specification
When a method declaration includes an extern modifier, that method is said to be an external method. External methods are implemented externally, typically using a language other than C#. Because an external method declaration provides no actual implementation, the method-body of an external method simply consists of a semicolon. An external method may not be generic. The extern modifier is typically used in conjunction with a DllImport attribute, allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided. When an external method includes a DllImport attribute, the method declaration must also include a static modifier.
Partial
A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods:
The following example shows a partial method defined in two parts of a partial class:
Abstract
Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.
Abstract methods have the following features:
In this example, the class Square must provide an implementation of Area because it derives from ShapesClass:
Source
Hope this helps in better understanding, Happy coding!
Extern will allow you use methods via dll-import and by this you are giving a special meaning to that method that it's coming from external sources
Partial :
*Most important Difference between Partial and Abstract method is Partial's Implementation is optional but Abstract method's implementation is compulsory *
Abstract methods strictly require the implementation in non abstract derived class
The basic use of abstract methods is, they are required to be implemented in order to use the class because those methods help to leverage that class efficiently
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With