How this sum operation of the two Timespan structures works?
This works because C# allows operator overload, defining static methods using the keyword operador
.
To overload an operator it is necessary to create in the class a static method named with the operator symbol being overloaded.
For unary operators the method declares a parameter in the case of binary operators two parameters.
The parameter(s) (s) shall (m) be of the same type as the class/structure declaring the operator.
In the case of Timespan method that’s how it is:
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2)
{
return t1.Add(t2);
}
Out of curiosity the method add() that’s how it is:
public TimeSpan Add(TimeSpan ts) {
long result = _ticks + ts._ticks;
// Overflow if signs of operands was identical and result's
// sign was opposite.
// >> 63 gives the sign bit (either 64 1's or 64 0's).
if ((_ticks >> 63 == ts._ticks >> 63) && (_ticks >> 63 != result >> 63))
throw new OverflowException(Environment.GetResourceString("Overflow_TimeSpanTooLong"));
return new TimeSpan(result);
}
This can be done in other classes, as a way to facilitate mathematical or conversion operations, there are examples to demonstrate?
Yes, this can be applied to any structure/class, just needs to overload the operators you want to make available.
Example of operator overload +
in a structure representing a complex number.
public struct Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
}
With regard to conversions, it is possible to declare them implicitly or explicitly.
Example of a type conversion operator to allow implicit conversion (without using cast) of that kind in another.
class Digit
{
public Digit(double d) { val = d; }
public double val;
// Conversão de Digit to double
public static implicit operator double(Digit d)
{
return d.val;
}
// Conversão de double para Digit
public static implicit operator Digit(double d)
{
return new Digit(d);
}
}
Example of use:
Digit dig = new Digit(7);
//Conversão implícita de Digit para double
double num = dig;
//Conversão implícita de double para Digit
Digit dig2 = 12;
Example use of a type conversion operator to allow explicit conversion (invoked using cast) of that kind in another.
class Celsius
{
public Celsius(float temp)
{
degrees = temp;
}
public static explicit operator Fahrenheit(Celsius c)
{
return new Fahrenheit((9.0f / 5.0f) * c.degrees + 32);
}
public float Degrees
{
get { return degrees; }
}
private float degrees;
}
Example of use:
Fahrenheit fahr = new Fahrenheit(100.0f);
//Conversão explicita de Fahrenheit para Celsius
Celsius c = (Celsius)fahr;
Knowing that you know the subject, is there any specific one you wish to know? What is there is correct. That is, what do you mean by "How does this operation work"? What other classes for example? With some certainly, with others not? Or it could, it depends on the definition of what you want to do. For example, there is no
DateTime
oneAdd(DateTime)
and it wasn’t forgetfulness. So at this time I’m finding it not clear or broad, but I know you can improve.– Maniero
It has no specifics, I spoke generally. How this operation works in the broad sense of architecture features
.net
proper, not to restrict onlyTimeSpan
, but, yes if I can do it in other classes and structures. @bigown. It’s a general answer. Yes I know the subject and have no question about it specifically.– novic
I understood then, then I see if I can answer and if you need since you have two answers that can be good, I’ll read later.
– Maniero
Perhaps with examples of
cast
@bigown would be a differential!– novic