Why can't I learn from a long time?

My function returns some long value that contains two values ​​in the lower and higher 32 bits.

I thought the best ways to handle the return value is to deduce my own type from the long one and provide such as GetLowerValue (), GetHigherValue ().

The problem is that .NET does not allow output from long

If you compile

public class SubmitOrderResult : long
{
}

You are getting:

cannot derive from sealed type 'long'

Why is it designed this way and how to overcome it?

thanks

+5
source share
6 answers

, .NET , . , .

public static class LongExtensions
{
    public static long GetLowerValue(this long value)
    {
        ...
    }

    public static long GetHigherValue(this long value)
    {
        ...
    }
}
+10

.NET: .

, GetLowerValue GetHigherValue ?

+6

, 32 .

, . , (, C), - .

OO , , . , , .

+5

, . , :

public struct SubmitOrderResult
{
    private long _result;

    public SubmitOrderResult(long result)
    {
        _result = result;
    }

    public long Result
    {
        get { return _result; }
        set { _result = value; }
    }

    public int GetHigherValue()
    {
        return (int)(_result >> 32);
    }

    public int GetLowerValue()
    {
        return (int)_result;
    }

    public static implicit operator SubmitOrderResult(long result)
    {
        return new SubmitOrderResult(result);
    }

    public static implicit operator long(SubmitOrderResult result)
    {
        return result._result;
    }
}

:

SubmitOrderResult result = someObject.TheMethod();
Console.WriteLine(result.GetHigherValue());
Console.WriteLine(result.GetLowerValue());

... , .

+4

, , , long, . , :

public class SubmitOrderResult
{
    private readonly long value_;

    public int OneValue
    { 
        get { return (int)(value_ >> 32); } 
    }

    public int TheOtherValue
    { 
        get { return (int)(value_ & 0xFFFFFFFF); } 
    }

    public SubmitOrderResult(long value)
    { value_ = value; }
}
+3

.

long , . . MSDN

, .

For more information, see Inheritance (C # Programming Guide) .

+1
source

All Articles