To simplify some aspects of the upcoming development, I am writing a small library. It is required to expand the double type a little - to add several methods. Looked at how this type is declared in .NET , it turned out that this is a structure that inherits several interfaces. The first thought is to try to inherit this structure, but in C # structures can only inherit interfaces. Then in my structure I implemented all the interfaces that are in double: IComparable, IFormattable, IConvertible, IComparable<double>, IEquatable<double> . Now my structure was supposed to be a double clone, but this did not happen. I cannot directly assign a value to this structure, that is, I cannot write like this

 MyDouble a = 20; 

What do you advise?

    2 answers 2

    It is required to somewhat expand the double type - add several methods

    For this, there are extension methods ( Extension Methods ) .

    • Yes, thanks, the method is suitable for solving my problem. But the question remains open: what happens in the bowels of a double when assigned? - maestro
    • There is an implicit conversion of int to double. It is implemented as follows: public static implicit operator double (int that) {return (double) that; } - nitrocaster
    • And the field so you can add? - maestro
    • No, only methods. - nitrocaster
    • one
      The assignment operator is not overloaded in C #. - nitrocaster

    In .NET, structures are inherited from System.ValueType, and ValueType is inherited from System.Object. As far as I remember, the inheritance of structures is prohibited due to memory allocation.

    For example:

     struct A{ int a; } struct B : A{ int b; } 

    Then, for structure B, you will need to allocate 2 times more memory, therefore with such things as:

     A _a = new B(); 

    Accordingly, since the structures are stored on the stack, some unknown part of the stack must be rewritten => exception.

    • How do you remember this? In the same C ++, inheritance is not prohibited, heirs are assigned to parents and nothing falls. - nitrocaster
    • 2
      @Flammable: the problem is that with A _a = new B (); The _a variable will have an instance of A , not B That is, the information will be lost. And the whole C # behaves differently: string s = "Test"; object o = s; string restored = (string) o; // information recovered That is, C # has a reference conversion, but not a value conversion. Value conversion also sometimes happens, but this requires an explicit cast and a conversion operator for the type: long l = long.MaxValue; int i = (int) l; - VladD
    • No, let's consider specifically value-types, since we are talking about them. > the problem is that ... This is not a problem at all. As you have already noticed, nobody calls the conversion of long -> int and vice versa a problem? [Here] [1], for example, believe that the inheritance of structures is prohibited because of a not quite obvious way to shoot one’s leg while traversing arrays (which, by the way, [available in C ++] [2]). [1]: stackoverflow.com/questions/1222935/… [2]: parashift.com/c++-faq-lite/array-derived-vs-base.html - nitrocaster