C#2.0: struct & class constraints

A

Alex Sedow

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?
 
C

Chad Z. Hower aka Kudzu

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/
 
M

Marcus Andrén

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;

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

Alex Sedow

Thanks Marcus!
You very help me.

--
Alex.
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)


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.
 
J

Jon Skeet [C# MVP]

Chad Z. Hower aka Kudzu said:
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.

What exactly do you mean "but have certain semantics of reference
types"?
 
C

Chad Z. Hower aka Kudzu

Jon Skeet said:
What exactly do you mean "but have certain semantics of reference
types"?

Semantics was probably not the rigth choice of word, neither was value type in this context. Just had
a baby last week and am shorter on sleep than normal.

What I meant to say but so poorly wrote is that structs are value types, but like classes (which are
reference types) can still have many of the same functionality as classes, methods, etc because
of how .NET implements them (vs other languages which treat structs very differently than
classes and much more distinct).


--
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/
 
J

Jon Skeet [C# MVP]

Chad Z. Hower aka Kudzu said:
Semantics was probably not the rigth choice of word, neither was
value type in this context. Just had a baby last week and am shorter
on sleep than normal.

Congratulations - I remember what that lack of sleep is like :)
What I meant to say but so poorly wrote is that structs are value
types, but like classes (which are reference types) can still have
many of the same functionality as classes, methods, etc because of
how .NET implements them (vs other languages which treat structs very
differently than classes and much more distinct).

What I *think* you're trying to say is that value types in .NET are
more functional than structs in C/C++, as they can define behaviour
(methods and properties) as well as encapsulating data. Is that right?
 
C

Chad Z. Hower aka Kudzu

Jon Skeet said:
Congratulations - I remember what that lack of sleep is like :)
Thanks.

What I *think* you're trying to say is that value types in .NET are
more functional than structs in C/C++, as they can define behaviour
(methods and properties) as well as encapsulating data. Is that right?

Yes. :)


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
C

Cool Guy

Jon Skeet said:
What I *think* you're trying to say is that value types in .NET are
more functional than structs in C/C++, as they can define behaviour
(methods and properties) as well as encapsulating data. Is that right?

Minor point: C++ structs can contain methods.
 
J

Jon Skeet [C# MVP]

Cool Guy said:
Minor point: C++ structs can contain methods.

Apologies. I very nearly left it as just C, and it looks like I should
have done so...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top