为什么我不能从长期获得? [英] Why I cannot derive from long?

查看:178
本文介绍了为什么我不能从长期获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数会返回一些长期价值 其中载有低和高32位的两个值

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

我还以为来处理返回值的最佳方式 是从长期得到我的自定义类型,并提供 型增量喜欢GetLowerValue(),GetHigherValue()。

I thought the best ways to handle return value is to derive my custom type from long and provide type extenders like GetLowerValue(), GetHigherValue().

的问题是,.NET不允许从长导出

The problem is that .NET does not allow to derive from long

如果您编译

public class SubmitOrderResult : long
{
}

您可以:

cannot derive from sealed type 'long'

这是为什么这样设计?如何克服呢?

Why is that designed so and how can overcome it?

感谢

推荐答案

由于.NET中已经提到的值类型是密封的,所以没有办法,你可以从长派生。作为建议,您应该创建扩展方法。

As already mentioned value types in .NET are sealed so there is no way you can derive from long. You should create extension methods as suggested.

示例

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

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

这篇关于为什么我不能从长期获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆