C#2.0:struct&类约束 [英] C#2.0: struct & class constraints

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

问题描述

您好。

为什么以下代码是在vs2005beta2中编译的?


class C< S,T>

其中S :struct,T

其中T:class

{}


1. S如何既是值类型又是引用-type?

2.有人可以给出C型正确实例的例子吗?


-

Alex。

Hello.
Why following code is compiled in vs2005beta2?

class C<S,T>
where S : struct, T
where T : class
{}

1. How S can be both value-type and reference-type?
2. Can anybody give example of correct instantiation of type C?

--
Alex.

推荐答案

> 1. S如何既可以是值类型又可以是引用类型?


结构(即使在.NET 1.1中)是特殊类型,它们是值类型,但具有某些语义

引用类型,但实际上是值类型。

-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是反击的艺术形式


使用IntraWeb让你的ASP.NET运转起来!
http://www.atozed.com/IntraWeb/
> 1. How S can be both value-type and reference-type?

Structs (Even in .NET 1.1) are special types that are value types, but have certain semantics of
reference type, but in fact are value types.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/


2005年6月18日星期六14 :06:46 +0400,Alex Sedow

< al ** @ csharpparser.com>写道:
On Sat, 18 Jun 2005 14:06:46 +0400, "Alex Sedow"
<al**@csharpparser.com> wrote:
你好。
为什么在vs2005beta2中编译下面的代码?

C类< S,T>
其中S: struct,T
其中T:class
{}

1。 S可以是值类型还是引用类型?


不可能,而且上述陈述实际上并没有这么说。它实际上是这样说的:

* S是结构

* T是类

* S isa T(换句话说,S是T,S继承自T或S实现T)

2.任何人都能举例说明C类型的正确实例吗?
Hello.
Why following code is compiled in vs2005beta2?

class C<S,T>
where S : struct, T
where T : class
{}

1. How S can be both value-type and reference-type?
It can''t and the above statement doesn''t actually say that. It
actually says the following:

* S is struct
* T is class
* S isa T (In other words, S is T, S inherit from T or S implement T)
2. Can anybody give example of correct instantiation of type C?



C< int,object> a;

C< int,System.ValueType> b;


以上两个声明都遵循三个规则,因为int确实

确实从System.ValueType和Object继承。


-

MarcusAndrén



C<int, object> a;
C<int, System.ValueType> b;

Both the above declarations follows the three rules since int does
indeed inherit from System.ValueType and Object.

--
Marcus Andrén


谢谢Marcus!

你们非常帮助我。


-

Alex。
Thanks Marcus!
You very help me.

--
Alex.
1。 S如何既可以是值类型又可以是引用类型?
1. How S can be both value-type and reference-type?



它不能和上面的语句实际上并没有这么说。它实际上说的如下:

* S是结构
* T是类
* S isa T(换句话说,S是T,S继承自T或S实现T)



It can''t and the above statement doesn''t actually say that. It
actually says the following:

* S is struct
* T is class
* S isa T (In other words, S is T, S inherit from T or S implement T)

2。任何人都可以给出C型正确实例化的例子吗?
2. Can anybody give example of correct instantiation of type C?



C< int,object> a;
C< int,System.ValueType> b;

以上两个声明都遵循三个规则,因为int确实从System.ValueType和Object继承。

-
MarcusAndrén



C<int, object> a;
C<int, System.ValueType> b;

Both the above declarations follows the three rules since int does
indeed inherit from System.ValueType and Object.

--
Marcus Andrén



这篇关于C#2.0:struct&amp;类约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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