473,431 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,431 software developers and data experts.

C#2.0: struct & class constraints

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.
Nov 17 '05 #1
11 1361
> 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/
Nov 17 '05 #2
On Sat, 18 Jun 2005 14:06:46 +0400, "Alex Sedow"
<al**@csharpparser.com> wrote:
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.

--
Marcus Andrén
Nov 17 '05 #3
Thanks Marcus!
You very help me.

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

--
Marcus Andrén

Nov 17 '05 #4
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1d1e0c44c0dc14d698c341
@msnews.microsoft.com:
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"?


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/
Nov 17 '05 #6
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1d1e0c44c0dc14d698c341
@msnews.microsoft.com:
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"?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP***********************@msnews.microsoft.co m:
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/
Nov 17 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
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.
Nov 17 '05 #9
Cool Guy <co*****@abc.xyz> wrote in news:12***************@cool.guy.abc.xyz:
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.


In many languages structs cannot contain methods though, and certainly not all the functions that
..NET offers with structs.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #10
Cool Guy <co*****@abc.xyz> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1d1e66cb92a5ac2d98c347
@msnews.microsoft.com:
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...


But C doesnt have objects either. So thus no methods even if it wanted to allow them on structs. ;)
--
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/
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
4
by: pallav | last post by:
hello, i'm sorry if this is a bit off topic but perhaps some of you here have experience with c++ with lex/yacc and can help me. i wrote a small lexer/parser using lex/yacc. i'm using c++ as...
2
by: dj3vande | last post by:
Is this code, as the complete contents of a translation unit, valid C90 that initializes the struct foo to contain copies of the values passed to bar()? -------- struct foo { int i; void *v;...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.