为什么类样本< T>其中T:Stream,类ILLEGAL [英] Why is class Sample<T> where T : Stream, class ILLEGAL

查看:67
本文介绍了为什么类样本< T>其中T:Stream,类ILLEGAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会解释为什么这个声明是非法的:class Sample< T>

其中T:Stream,class

解决方案

< blockquote> 11月3日,1:54 * pm,puzzlecracker< ironsel2 ... @ gmail.comwrote:


有人解释为什么这个声明是非法的:class样本< T>

其中T:流,类



它们取自C#深度书

还有一些也是非法的,我不明白为什么

(编译器不太有帮助)。


class Sample< Twhere T :Stream,class


class Sample< Twhere T:new(),Stream


class Sample< T,Uwhere T:struct where U :class,T


class Sample< T,Uwhere T:Stream,U:IDisposable


你可以限制泛型实现通过基本类型或

值/引用类型。不是两个。


Stream已经是一个类了。因此,如果你将
限制为T,那么T将是一个引用类型(类)。所以,基本上你是想说第2课

次。

puzzlecracker写道:


有人解释为什么这个声明是非法的:class Sample< T>

其中T:Stream,class


任何''class''或者''struct''约束应该首先出现,但是如果你想使用这个泛型类

with stream,你为什么要添加

''所有类''约束和派生类?

-

Stanimir Stoyanov
http://stoyanoff.info


" puzzlecracker" < ir ********* @ gmail.comwrote in message

news:45 ********************* ************* @ z6g2000p re.googlegroups.com ...


有人会解释为什么这个声明是非法的:class Sample< ; T>

其中T:Stream,class


Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class

解决方案

On Nov 3, 1:54*pm, puzzlecracker <ironsel2...@gmail.comwrote:

Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class

They are taken from C# In depth book
And a few more that are also illegal, and I don''t understand why
(compiler is less than helpful).

class Sample<Twhere T : Stream, class

class Sample<Twhere T : new(), Stream

class Sample<T,Uwhere T : struct where U : class, T

class Sample<T,Uwhere T : Stream, U : IDisposable


You can restrict the generic implementation by either a base type or
value/reference type. Not both.

Stream is already a class. So, T will be a reference type (class) if you
restrict T to be a Stream. So, basically you are trying to say class 2
times.
puzzlecracker wrote:

Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class


Any ''class'' or ''struct'' constraints should come first but why are you adding
an ''all classes'' constraints if you would like to use this generic class
with Stream and derivative classes?
--
Stanimir Stoyanov
http://stoyanoff.info

"puzzlecracker" <ir*********@gmail.comwrote in message
news:45**********************************@z6g2000p re.googlegroups.com...

Would someone explain why this declaration is illegal: class Sample<T>
where T : Stream, class


这篇关于为什么类样本&lt; T&gt;其中T:Stream,类ILLEGAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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