C#2.0: struct & class constraints

  • Thread starter Thread starter Alex Sedow
  • Start date Start date
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?
 
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/
 
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.
 
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.
 
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"?
 
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/
 
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?
 
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/
 
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.
 
Back
Top