Is it possible define an extension operator method?

c# extension methods
why we use extension method in c#
c# extension properties
extension methods java
c# static extension method
extension method in c# code project
c# generic extension method
c# override extension method

is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can't be applied. For this particular case i want to do this:

   somestring++;  //i really know that this string contains a numeric value

And i don't want to spread types conversions for all the code. I know that i could create wrapper class over an string and define that operator but i want to know if this kind of thing is possible to avoid search-and-replace every string declaration with MySpecialString.

Edited: as most have say string is sealed, so derivation isn't possible, so i modify "derived" to "wrapper", my mistake.

No, it is not possible to do from outside of the class. ++ operator should be defined inside class which is being incremented. You can either create your own class which will be convertible from string and will have ++ overload or you can forget about this idea and use regular methods.

Extension Methods, This tutorial explains what is Extension Method in C#. of the extension method specifies the type that it is going to operator on, preceded by the this keyword. Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

That is not possible in C#, but why not a standard extension method?

 public static class StringExtensions {
     public static string Increment(this string s) {

I think somestring.Increment() is even more readable, as you're not confusing people who really dont expect to see ++ applied to a string.

Are extension methods and extension properties are bad practice , In object-oriented computer programming, an extension method is a method added to an object Not all languages implement extension methods in an equally safe manner, however. been burdensome to require each of these implementations to implement the whole series of methods that are defined in the System. The existence of even a simple ( m, p)–extension operator for Ω guarantees that Wm,p (Ω) inherits many properties possessed by Wm,p ( Rn ). For instance, if an imbedding Wm,p ( Rn) → Lq ( Rn) is known to hold, so that. then the imbedding Wm,p (Ω) → Lq (Ω) must also hold, for if u ∈ Wm,p (Ω), then.

A clear example of where this would be useful is to be able to extend the TimeSpan class to include * and / operators.

This is what would ideally work...

public static class TimeSpanHelper
    public static TimeSpan operator *(TimeSpan span, double factor)
        return TimeSpan.FromMilliseconds(span.TotalMilliseconds * factor);

    public static TimeSpan operator *(double factor, TimeSpan span)  // * is commutative
        return TimeSpan.FromMilliseconds(span.TotalMilliseconds * factor);

    public static TimeSpan operator /(TimeSpan span, double sections)
        return TimeSpan.FromMilliseconds(span.TotalMilliseconds / factor);

    public static double operator /(TimeSpan span, TimeSpan period)
        return span.TotalMilliseconds / period.TotalMilliseconds);


Extension Methods in C#, @alrz I think you're imagining that pattern-matching does overload resolution on all of the possible operator is methods that it can find, based  An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example. The IntExtensions class will contain all the extension methods applicable to int data type.

No, you can't have an extension method which is also an operator. Extension methods can only be declared in static classes, which can't have instances and according to the C# spec,

User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. [7.3.2]

Therefore, it is impossible for an extension method to also be an overloaded operator.

Additionally, you can't override System.String since it is a sealed class.

What Are Extension Methods and How to Use Them, For the first two lambda expressions, both are expressed by using Function Query Operator methods are static methods defined in either Enumerable or Extension methods make it possible to write a method that can be called as if it were  Extension method An extension method is a new language feature of C# starting with the 3.0 specification, as well as Visual Basic.NET starting with 9.0 and Oxygene with 2.0. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

The string class is sealed in C#, so creating a string-derived class actually isn't possible.

That being said, an extension method will of course work just fine (as will a standard static method in a helper class) but it won't be an operator, just ordinarily-named method.

C# Extension Method, You define and use extension operators in the same way as extension methods. For example, you could define and use an extension plus operator, as follows: The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition. Not all languages implement extension methods in an equally safe manner, however.

Extension method, This is possible because deep within ActiveSupport is this snippet of code: Note that methods defines in modules/mixins are not extension  When you are using extension methods you can force C# to use particular methods. There is no solution if you want to point particular operator in C# code. Operators have only single syntax when you want to invoke an operator method. In addition IntelliSense will do not help, because it starts showing after first character.

Extension operators · Issue #515 · dotnet/csharplang · GitHub, Thus - 1 is defined on the test functions for such V. By (4.2.1) this implies But it does show that the self-adjoint operator corresponding to E is an extension of H I. to extend the definition of h(u, v) to as large a class of functions as possible. 5. Extension methods. Contents - Previous - Next. Individual methods of extension Group methods of extension Types of group extension methods . In the previous chapter, the mass communication methods that the extension agent can employ in his work with farmers were reviewed.

SQL Server Database Programming with Visual Basic.NET: Concepts, , In [17, 18 we deal with the subclass of operators in S(H) that are densely defined. In the present work we extend the results of 18 to the larger class defined  Hah! I was looking up "extension operator overloading" with exactly the same desire, for sb += (thing). After reading the answers here (and seeing that the answer is "no"), for my particular needs, I went with an extension method which combines sb.AppendLine and sb.AppendFormat, and looks tidier than either.

  • See this answer: "This is not currently possible, because extension methods must be in static classes, and static classes can't have operator overloads."
  • :( i see that but as it was written in 2008 i had the hope that things have changed
  • Indeed. Doing timespan maths with lots of .Multiply() .Divide() etc quickly becomes ugly
  • I try that and get "static classes cannot contain user defined operators", if it's not static then I get "one of the parameters of a binary operator overload should be of the containing type"
  • This is exactly what I was trying to do.
  • In addition to this answer, a list of overloadable, and non-overloadable operators can be found here: