泛型,使用+ / - / *运算符等 [英] Generics, use +/-/* operator etc

查看:49
本文介绍了泛型,使用+ / - / *运算符等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#泛型中使用*运算符?例如


class foo< T_>

{

T_ v1,v2:


public T_ squar()

{

return v1 * v2; ///错误!

}

}

How can I use * operator in C# generics? e.g.

class foo<T_>
{
T_ v1, v2:

public T_ squar()
{
return v1 * v2; /// wrong!
}
}

推荐答案

周五,2007年12月14日11:25:27 -0800,我的兴趣< my ******** @ gmail.com>

写道:
On Fri, 14 Dec 2007 11:25:27 -0800, My interest <my********@gmail.com>
wrote:

如何在C#泛型中使用*运算符?例如


class foo< T_>

{

T_ v1,v2:


public T_ squar()

{

return v1 * v2; ///错!

}

}
How can I use * operator in C# generics? e.g.

class foo<T_>
{
T_ v1, v2:

public T_ squar()
{
return v1 * v2; /// wrong!
}
}



我认为你不能。要使用*运算符(或任何运算符,对于那个

问题),编译器必须知道您正在处理具有

a定义的重载的类型。运算符。


如果你有一个情况,你可以将你的泛型类型

参数约束到一个有效运算符*重载的参数,那么

会起作用。但是没有你的任何其他信息,似乎很可能

你想要获得价值类型的这种行为,当然

他们没有任何信息继承关系,这将允许你

定义一个有用的约束。


我想你可以在你的类中定义重载,涵盖所有

您想要处理的类型。我自己没有尝试过,但希望

编译器能够在编译时选择正确的重载。对于

例如:


class foo< T_>

{

T_ v1,v2;


public static T_ squar(int t1,int t2)

{

return(T _)(t1 * t2);

}


public static T_ squar(float t1,float t2)

{

return (T _)(t1 * t2);

}


//等等


公共T_平方( )

{

返回小方(v1,v2);

}

}


实际上,在代码中看到这一点,我不太确定。编译器可能会发现

找到要抱怨的东西。但无论如何都值得一试。


Pete

I don''t think you can. To use the * operator (or any operator, for that
matter), the compiler has to know that you''re dealing with types that have
a defined overload for the operator.

If you have a situation where you can constrain your generic type
parameter to one that does have a valid operator* overload, then that
would work. But without any other information from you, it seems likely
that you''re looking to get this behavior with value types and of course
they don''t have any inheritance relationship that would allow you to
define a useful constraint like that.

I suppose you could define overloads within your class, covering all the
types you want to handle. I haven''t tried it myself, but hopefully the
compiler would be able to pick the right overload at compile time. For
example:

class foo<T_>
{
T_ v1, v2;

public static T_ squar(int t1, int t2)
{
return (T_)(t1 * t2);
}

public static T_ squar(float t1, float t2)
{
return (T_)(t1 * t2);
}

// etc.

public T_ squar()
{
return squar(v1, v2);
}
}

Actually, seeing that in code, I''m not so sure. The compiler might sitll
find something to complain about. But it''s worth a try anyway.

Pete




我的兴趣 ; < my ******** @ gmail.comwrote in message

news:53 ********************** ************ @ b40g2000 prf.googlegroups.com ...

"My interest" <my********@gmail.comwrote in message
news:53**********************************@b40g2000 prf.googlegroups.com...

如何在C#泛型中使用*运算符?例如


class foo< T_>

{

T_ v1,v2:


public T_ squar()

{

return v1 * v2; ///错!

}

}
How can I use * operator in C# generics? e.g.

class foo<T_>
{
T_ v1, v2:

public T_ squar()
{
return v1 * v2; /// wrong!
}
}



这是一个众所周知的问题 - 你可以't。


搜索代码项目C#generic arithmetic"解决方法。

It''s a well known problem -- you can''t.

Search codeproject for "C# generic arithmetic" for workarounds.


我的兴趣< my ******** @ gmail.comwrote:
My interest <my********@gmail.comwrote:

如何在C#泛型中使用*运算符?例如


class foo< T_>

{

T_ v1,v2:


public T_ squar()

{

return v1 * v2; ///错误!

}

}
How can I use * operator in C# generics? e.g.

class foo<T_>
{
T_ v1, v2:

public T_ squar()
{
return v1 * v2; /// wrong!
}
}



你不能,正确 - 它''这是相当多工作的原因。

但是,如果你使用.NET 3.5,Marc Gravell有一种非常狡猾的方式

of用表达树做这件事,我打算在博客处写一些

点...


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~silet 博客: http://www.msmvps .com / jon.skeet

英国的世界级.NET培训: http://iterativetraining.co.uk

You can''t, properly - it''s been the cause of a fair amount of work.

However, if you''re using .NET 3.5 Marc Gravell has a very cunning way
of doing it with expression trees, which I plan to blog about at some
point...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


这篇关于泛型,使用+ / - / *运算符等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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