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

Accepted answer
Score: 39

extern is unlikely to be something you want to 22 use. It means that the method is implemented, but 21 implemented externally - and typically used 20 in interop scenarios where you're defining 19 a method implelemented in external code.

abstract, on 18 the other hand, means you're defining the 17 API for the method, but not providing an 16 implementation. The subclass will have 15 to provide the implementation for any methods 14 or properties marked abstract, or be abstract itself. If 13 you want to make a base class and have a 12 method or property that must be implemented 11 by subclasses, you'll want to use abstract.

partial classes 10 and methods are merely a compilation tool. They 9 allow you to use multiple files to define 8 your type. This is mostly used with automatically 7 generated code (ie: a designer will put 6 the designer generated code into a separate 5 file defining a partial class, so you can 4 'fill in' the missing pieces without looking 3 at the implementation details). This is 2 unlikely something you'll use directly for 1 defining a class.

Score: 6

An extern method is typically being implemented 16 via a dll-import (P/Invoke) - so it does 15 have an implementation - you just can't 14 see it.

A partial method is useful mainly with code-generation 13 as a way to inject functionality into the 12 generated code. They are optional, private 11 only, and only exist if you provide the other half. As 10 such there are also some limitations around 9 return/out values to assure definite assignment. Calls 8 to partial methods will be omitted entirely 7 by the compiler if there is no implementation.

An 6 abstract method is where the implementation has 5 to be provided by a derived type. The runtime 4 ensures you can't have an instance if there 3 are still unimplemented abstract methods, so 2 you are assured that they will exist at 1 runtime.

Score: 5

There seems to be some good answers here 39 but I would still write to make it more 38 clear


From C# specification

When a method 37 declaration includes an extern modifier, that 36 method is said to be an external method. External 35 methods are implemented externally, typically 34 using a language other than C#. Because 33 an external method declaration provides 32 no actual implementation, the method-body 31 of an external method simply consists of 30 a semicolon. An external method may not 29 be generic. The extern modifier is typically 28 used in conjunction with a DllImport attribute, allowing 27 external methods to be implemented by DLLs 26 (Dynamic Link Libraries). The execution 25 environment may support other mechanisms 24 whereby implementations of external methods 23 can be provided. When an external method 22 includes a DllImport attribute, the method 21 declaration must also include a static modifier. enter image description here


A 20 partial method has its signature defined 19 in one part of a partial type, and its implementation 18 defined in another part of the type. Partial 17 methods enable class designers to provide 16 method hooks, similar to event handlers, that 15 developers may decide to implement or not. If 14 the developer does not supply an implementation, the 13 compiler removes the signature at compile 12 time. The following conditions apply to 11 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 10 a partial method defined in two parts of 9 a partial class:

enter image description here


Use the abstract modifier 8 in a method or property declaration to indicate 7 that the method or property does not contain 6 implementation.

Abstract methods have the 5 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 4 class Square must provide an implementation 3 of Area because it derives from ShapesClass:

enter image description here


Hope 2 this helps in better understanding, Happy 1 coding!

Score: 4

Extern will allow you use methods via dll-import 8 and by this you are giving a special meaning 7 to that method that it's coming from external 6 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 5 require the implementation in non abstract 4 derived class

The basic use of abstract 3 methods is, they are required to be implemented 2 in order to use the class because those 1 methods help to leverage that class efficiently

Score: 3

Extern: http://msdn.microsoft.com/en-us/library/e59b22c5%28v=vs.80%29.aspx

It is an error to use the abstract 15 (C# Reference) and extern modifiers together 14 to modify the same member. Using the extern 13 modifier means that the method is implemented 12 outside the C# code, while using the abstract 11 modifier means that the method implementation 10 is not provided in the class.

Abstract: http://msdn.microsoft.com/en-us/library/sf985hc5%28v=vs.80%29.aspx

Use 9 the abstract modifier in a class declaration 8 to indicate that a class is intended only 7 to be a base class of other classes. Members 6 marked as abstract, or included in an abstract 5 class, must be implemented by classes that 4 derive from the abstract class.

partial: http://msdn.microsoft.com/en-us/library/wbx7zzdd%28v=vs.80%29.aspx

Partial 3 type definitions allow the definition of 2 a class, struct or interface to be split 1 into multiple files.

More Related questions