Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between extern, abstract, and partial for methods in an abstract class?

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?

like image 687
ProgrammingPope Avatar asked Apr 12 '11 18:04

ProgrammingPope


4 Answers

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.

like image 180
Reed Copsey Avatar answered Dec 22 '22 00:12

Reed Copsey


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.

like image 36
Marc Gravell Avatar answered Dec 21 '22 22:12

Marc Gravell


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. enter image description here

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:

  • Signatures in both parts of the partial type must match.
  • The method must return void.
  • No access modifiers are allowed. Partial methods are implicitly private.

The following example shows a partial method defined in two parts of a partial class:

enter image description here

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:

  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes
  • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature.
  • It is an error to use the static or virtual modifiers in an abstract method declaration.

In this example, the class Square must provide an implementation of Area because it derives from ShapesClass:

enter image description here

Source

Hope this helps in better understanding, Happy coding!

like image 43
StaticVariable Avatar answered Dec 21 '22 22:12

StaticVariable


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 :

  1. A partial method must be declared within a partial class or partial struct
  2. You can't have access modifier on Partial Method
  3. A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers
  4. Partial method can't have its implementation before separate declaration.
  5. Partial method can only be defined and can't be declared in the same partial class.

*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

like image 25
Ajay Suwalka Avatar answered Dec 21 '22 23:12

Ajay Suwalka