乘以两个对象 [英] multiply two objects

查看:78
本文介绍了乘以两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传递两个对象的方法,它需要返回一个对象

,它代表那两个对象相乘。


公共对象Multiply (对象a,对象b)

{

返回a * b;

}


对象可以是任何类型的数字,我希望它以最有效和最准确的方式进行数学计算。


例如如果两个数字都是双数,则它们被乘以双数并且返回一个双b / b $ b $。


有没有办法在C#中执行此操作?


跳跳虎

I have a method that is passed two objects and it needs to return an object
that represents those two objects multiplied together.

public object Multiply(object a, object b)
{
return a * b;
}

The objects can be any type of number and I would like it to do the maths in
the most efficient and accurate way.

e.g. if both numbers are double, they are multipled as doubles and a double
is returned.

Is there any way to do this in C#?

Tigger

推荐答案

10月11日下午1:00,跳跳虎 < mccre ... @ bigfoot.comwrote:
On Oct 11, 1:00 pm, "Tigger" <mccre...@bigfoot.comwrote:

我有一个传递两个对象的方法,它需要返回一个对象

代表那两个对象相乘。


公共对象乘(对象a,对象b)

{

返回a * b;


}


对象可以是任何类型的数字,我希望它可以在
中进行数学运算
最有效和准确的方式。


例如如果两个数字都是双数,则它们被乘以双数并且返回一个双重



有没有办法在C#中执行此操作?
I have a method that is passed two objects and it needs to return an object
that represents those two objects multiplied together.

public object Multiply(object a, object b)
{
return a * b;

}

The objects can be any type of number and I would like it to do the maths in
the most efficient and accurate way.

e.g. if both numbers are double, they are multipled as doubles and a double
is returned.

Is there any way to do this in C#?



说实话,

这样做的最简单(如果不是美学上的吸引力)方式就是有很多重载,一个用于每个数字

类型。


Jon

To be honest, the simplest (if least aesthetically appealing) way of
doing this is simply to have lots of overloads, one for each numeric
type.

Jon


我几乎回复了同样的问题。 ..但为了工作,你要么需要

才能知道调用代码中的类型(所以编译器可以解析),

或者你'' d必须使用基于a.GetType()和b.GetType()的反射。

后者不是很吸引人,前者,那么,在这种情况下

你不能只使用a * b吗?在调用代码中,而不是

乘以(a,b)?


只是想一想......


Marc
I almost replied the same... but for that to work, you''d either need
to know the types in the calling code (so the compiler can resolve),
or you''d have to use reflection based on a.GetType() and b.GetType().
The latter isn''t very appealing, and the former, well, in that case
couldn''t you just use "a * b" in the calling code, rather than
Multiply(a,b)?

Just a thought...

Marc


10月11日下午1:34,Marc Gravell < marc.grav ... @ gmail.comwrote:
On Oct 11, 1:34 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:

我几乎回答了同样的问题......但为了工作,你要么需要

知道调用代码中的类型(因此编译器可以解析),

或者你必须使用基于a.GetType()和b的反射。 GetType()。

后者不是很有吸引力,前者,那么,在这种情况下,

你不能只使用a * b ;在调用代码中,而不是

乘以(a,b)?


只是想一想......
I almost replied the same... but for that to work, you''d either need
to know the types in the calling code (so the compiler can resolve),
or you''d have to use reflection based on a.GetType() and b.GetType().
The latter isn''t very appealing, and the former, well, in that case
couldn''t you just use "a * b" in the calling code, rather than
Multiply(a,b)?

Just a thought...



是的,是的。我认为它真的有*使用反射,不是吗,如果

给定的签名是理想的签名。

我想我是'' d仍然有适当的重载,但是然后做同样的工作,编译器通常会做的工作,正确的

转换使用等。


我看不出这是多么高效......


Jon

True, true. I think it''s *got* to use reflection really, hasn''t it, if
the given signature is the desired one.
I think I''d still have the appropriate overloads, but then do the same
sort of work that the compiler usually would, working out the right
conversion to use etc.

I can''t see how this is going to be terribly efficient...

Jon


这篇关于乘以两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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