可以返回对象和原始数据类型的方法的方法签名... [英] Method Signature For A Method That Can Return Objects and PrimitiveData Types...

查看:103
本文介绍了可以返回对象和原始数据类型的方法的方法签名...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java开发人员,正在用C#编写类库,

所以请耐心等待。


我是试图写一个方法,可以返回一个通用的对象或一个

十进制值。在Java中,我只是让方法返回一个Object,因为所有Java原始数据类型都有Object包装器,如Integer

和Double。


如果我有一个在C#中返回Object的方法签名,我可以在方法体中返回一个

十进制值吗?我正在网上看它

看来Decimal是一个Struct,而不是一个Class,所以我不确定这个

是否合法。


感谢您提供解决方案的帮助。


晒伤测量师

I''m a Java developer in the process of writing a class library in C#,
so please bear with me.

I''m trying to write a method that can return a generic Object OR a
Decimal value. In Java I would just have the method return an Object,
since all Java primitive data types have Object wrappers like Integer
and Double.

If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I''m not sure if this
is legal.

Thanks for help with the solution.

The Sunburned Surveyor

推荐答案

2007年12月20日星期四09:57:20 -0800,晒伤的测量师

< su **************** @ gmail.comwrote:
On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<su****************@gmail.comwrote:

[...]

如果我有一个在C#中返回Object的方法签名,我可以返回一个

方法体内的十进制值?我在网上看它

看起来十进制是一个结构,而不是一个类,所以我不确定这是否合法。
[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I''m not sure if this
is legal.



它是。


您可能想要阅读拳击,因为返回值类型为

" object"将强制值类型包装在引用类型中

实例(已装箱)。回流到十进制将取消装箱,允许它将

分配给正确值类型的变量(在这种情况下为十进制,当然是

)。


当然,您可以尝试一下它是否有效。 :)


Pete

It is.

You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).

Of course, you could have just tried it to see if it works. :)

Pete




" Peter Duniho" < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...

2007年12月20日星期四09:57:20 -0800,晒伤测量师

< su **** ************ @ gmail.com写道:
On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<su****************@gmail.comwrote:

> [...]
如果我有方法签名在C#中返回Object,我可以在方法体中返回一个十进制值吗?我在网上看它似乎是十进制是一个结构,而不是一个类,所以我不确定这是否合法。
>[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I''m not sure if this
is legal.



它是。


您可能想要阅读拳击,因为返回值类型为

" object"将强制值类型包装在引用类型中

实例(已装箱)。回流到十进制将取消装箱,允许它将

分配给正确值类型的变量(在这种情况下为十进制,当然是

)。


当然,您可以尝试一下它是否有效。 :)


Pete


It is.

You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).

Of course, you could have just tried it to see if it works. :)

Pete



Pete的优秀建议附录:拳击很慢。


如果你打算经常调用这个方法,你应该认真考虑

只需创建一个新方法。


另一种选择是使用带有out的重载方法。参数

而不是函数结果。当然,这假定您知道结果的

类型。在你打电话给方法之前。


如果我不得不猜测,我会说你写的是某种WinAPI - 函数

如果成功则返回一个对象,如果失败则返回错误代码。在

的情况下,您可以考虑使用out返回小数(0 =成功)和

。成功的参数结果。


An addendum to Pete''s excellent advice: "boxing" is very slow.

If you intend to call this method very often, you should seriously consider
simply creating a new method.

Another option would be to use overloaded methods with an "out" parameter
instead of a function result. This assumes, of course, that you know the
type of the "result" before you call the method.

If I had to guess, I''d say you''re writing some sort of "WinAPI"-ish function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success) and
using an "out" parameter for your success "result".


12月20日下午12:07,Scott Roberts < srobe ... @ no.spam.here-webworks-

software.comwrote:
On Dec 20, 12:07 pm, "Scott Roberts" <srobe...@no.spam.here-webworks-
software.comwrote:

" Peter Duniho" < NpOeStPe ... @nnowslpianmk.com写在消息中


新闻:op *************** @ petes-computer.local .. 。


"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...



2007年12月20日星期四09:57:20 -0800,晒伤测量师

< ; sunburned.surve ... @ gmail.comwrote:
On Thu, 20 Dec 2007 09:57:20 -0800, Sunburned Surveyor
<sunburned.surve...@gmail.comwrote:


[...]

如果我有一个在C#中返回Object的方法签名,我可以在方法体中返回一个

十进制值吗?我在网上看它

看起来十进制是一个结构,而不是一个类,所以我不确定这是否合法。
[...]
If I have a method signature that returns Object in C#, can I return a
Decimal value within the method body? I was looking online at it
appears that Decimal is a Struct, not a Class, so I''m not sure if this
is legal.


它是。
It is.


您可能想要阅读拳击,因为返回值类型为

" object" ;将强制值类型包装在引用类型中

实例(已装箱)。回流到十进制将取消装箱,允许它将
分配给正确值类型的变量(在这种情况下为小数,当然是
)。
You may want to read up on "boxing", as returning a value type as an
"object" will force the value type to be wrapped in a reference type
instance (boxed). Casting back to decimal will unbox it, allowing it to
be assigned to a variable of the correct value type (decimal in this case,
of course).


当然,您可以尝试一下它是否有效。 :)
Of course, you could have just tried it to see if it works. :)


Pete
Pete



对Pete的优秀建议的附录:" ;拳击"很慢。


如果你打算经常调用这个方法,你应该认真考虑

只需创建一个新方法。


另一种选择是使用带有out的重载方法。参数

而不是函数结果。当然,这假定您知道结果的

类型。在你打电话给方法之前。


如果我不得不猜测,我会说你写的是某种WinAPI - 函数

如果成功则返回一个对象,如果失败则返回错误代码。在

的情况下,您可以考虑使用out返回小数(0 =成功)和

。成功的参数结果.-隐藏引用的文字 -


- 显示引用的文字 -


An addendum to Pete''s excellent advice: "boxing" is very slow.

If you intend to call this method very often, you should seriously consider
simply creating a new method.

Another option would be to use overloaded methods with an "out" parameter
instead of a function result. This assumes, of course, that you know the
type of the "result" before you call the method.

If I had to guess, I''d say you''re writing some sort of "WinAPI"-ish function
which returns an object if it succeeds and an error code if it fails. In
that case, you might consider always returning a decimal (0 = success) and
using an "out" parameter for your success "result".- Hide quoted text -

- Show quoted text -



感谢回应斯科特彼得。我实际上是将

方法定义为接口的一部分。在大多数情况下,

这个接口的实现只会从这个特定的

方法返回一个Decimal值。但是,这个界面的偶然实现可能会让b $ b想要从这个方法返回一个自定义对象。


听起来我可能需要提供一个方法对于定制

对象访问。


再次感谢您的帮助。


晒伤的验船师


这篇关于可以返回对象和原始数据类型的方法的方法签名...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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