Instance/Static Methods
Accessible members: public
, protected
, private
(cannot be accessed if inherited)
Definition: Same class/struct/interface (can be split across files with the partial
keyword)
Called as: object.Method()
In this context, I mean that Static Methods are methods defined within the class they manipulate. That is, they are defined alongside the other class objects. (Static methods defined within the MyPoint
class in your example code.)
We all know (or should know) what these are and the benefits to them, I won't go in to much detail except to say:
Instance methods have access to all private
, protected
, and public
members of the class. As do static
methods.
In most cases, if you have a large number of methods and/or properties to add, or they significantly change the operation of the object, you should inherit the original object (if possible). This gives you access to all public
and protected
members of the class/struct/interface
.
Static Helper Class Methods
Accessible members: public
Definition: Any class/namespace
Called as: HelperClass.Method(object)
By Static Helper Class Methods I am implying that the actual definition of the static
methods referred to by this section is not within the actual class definition. (I.e. a class like MyPointHelpers
or similar, using your code example.)
Static Helper class methods only have access to the public
members of an object (much like Extension methods, I wrote this section after the extension method section).
Static Helper classes and Extension Methods are closely related, and in many instances are the same. Therefore I'll leave the benefits to them in the Extension Methods section.
Extension Methods
Accessible members: public
Definition: Any class/namespace
Called as: object.Method()
Extension methods only have access to the public
members of an object. Though they appear to be members of the class, they are not. This limits what they are useful for. (Methods that require access to any of the private
or protected
members should not be extensions.)
Extension methods serve three huge benefits in my opinion.
Say you are developing class A
, and class A
has about 7 methods in it. You also know that you would like to develop a few methods that you won't always need, but that would be handy if you ever do. You could use extension methods for this. These methods would be abstracted away in another class that you can include (by class, thanks to C# 6.0) later if you ever need them. Uncommon methods that you know you want to use later, but you know you don't always need.
Say you are developing programme A
, and you are using a class from DLL Something.Other.C
, that you do not own the source to. Now, you want to add a method that interacts with class Something.Other.C
in a way that would make sense with an instance or regular static method, but you haven't the source so you can't! Enter extension methods, where you can define a method that appears to be a member of class Something.Other.C
, but is actually part of your code.
Say you develop your own library that you use with a lot of your own applications, and you realize in the midst of developing application X
that you could really use a method Y
on class A
again. Well, instead of modifying the definition of class A
(because that's a lot more work, and you don't use method Y
anywhere except application X
), you can define an extension method Y
, on class A
that is only present in application X
. Now your overhead of the method is limited strictly to application X
. Application Z
doesn't need to have this extension method.
Performance
As far as performance, this would depend on the methods, what they do, and how they do it. You would be subject to whatever public
properties/methods/fields were on the objects you are altering, and would need to gauge performance on that. (If calling public Value
instead of private value
causes some significant validation overhead, an instance method makes more sense as it can work with the private
fields or properties.)
A
by including methodDoSomethingCooler()
, but the definition for classA
exists in namespaceEBrown.NamespaceB
in a separate DLL you do not own the source-code to, you can define an extension method to classA
that allows you to callA.DoSomethingCooler()
just as if you owned the source. The disadvantage: private members are still private. – Vlf