泛型的值/原始类型约束 [英] Value / primitive type constraints on generics

查看:80
本文介绍了泛型的值/原始类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将泛型约束为值或基本类型?


(如果直接答案为否,则欢迎任何kludges / workarounds ....)

Is it possible to constrain a generic to be a value or primitive type?

(Any kludges/workarounds welcome if the immediate answer is no....)

推荐答案

戴夫,


是的,确实如此。你会这样做:


公共类MyClass< T>其中T:struct


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Dave Booker <分贝****** @ newsgroup.nospam>在消息中写道

news:86 ********************************** @ microsof t.com ...
Dave,

Yes, it is. You would do this:

public class MyClass<T> where T : struct

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:86**********************************@microsof t.com...
是否可以将泛型约束为值或基本类型?

(如果立即答案为否,则欢迎使用任何kludges / workarounds。 ...)
Is it possible to constrain a generic to be a value or primitive type?

(Any kludges/workarounds welcome if the immediate answer is no....)



" Nicholas Paldino [。NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:uz ************* @ TK2MSFTNGP10.phx.gbl ...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uz*************@TK2MSFTNGP10.phx.gbl...
public class MyClass< T>其中T:struct
public class MyClass<T> where T : struct




是否在哪里T:struct也为int工作?


- Alan



Does "where T : struct" work for an int too?

-- Alan


Alan,


你的意思是T是一个int?是的,确实如此。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Alan Pretre" <无@垃圾>在消息中写道

news:eG ************** @ TK2MSFTNGP11.phx.gbl ...
Alan,

You mean for T being an int? Yes, it does.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan Pretre" <no@spam> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
" Nicholas Paldino [ .NET / C#MVP]" < mv*@spam.guard.caspershouse.com>在消息新闻中写道:uz ************* @ TK2MSFTNGP10.phx.gbl ...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uz*************@TK2MSFTNGP10.phx.gbl...
public class MyClass< T>其中T:struct
public class MyClass<T> where T : struct



是否在哪里T:struct也为int工作?

- Alan



Does "where T : struct" work for an int too?

-- Alan



这篇关于泛型的值/原始类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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