By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Struct a lightweight class type having value based semantics?

P: n/a
I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.

Looking at his example I cant see any difference from a class here
other than it is defined with the struct keyword? what is the
difference between these two?

Also, Troelson speaks of the class type as having 'value based
semantics' what does that mean?

Thanks very much for your help.

Gary-

// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{ xPos = x; yPos = y;}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}

Dec 8 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Noooo.... not again..... ;-p

Jon gives an answer here:
http://www.yoda.arachsys.com/csharp/...html#preamble2

The short answer it that C# structs should really only generally represent
simple, immutable "values", like "an integer", "a complex number" etc. You
never change them; you simply assign a new one. They tend to clone
themselves almost at will (as a direct memcopy), and can live either on the
stack (for a variable) or the managed heap (if a field inside a class). They
should also be small (to support efficient memcopy).

It is *very* rare that you need to create a struct in C#.

Marc
Dec 8 '06 #2

P: n/a
Value types are, effectively, labels for areas of memory. Reference types
are pointers.

Therefore the values in value types can be accessed directly and very
quickly; and such data structure can be allocated memory on the stack.

Reference types always have to go through an indirection layer, and always
allocate memory on the heap. They therefore have to be garbage collected.

Obviously, value types cannot be NULL, because there will always be
something in the area of memory they define, even if it's garbage.
Reference types, equally obviously, can be NULL, if they aren't pointing to
any area in memory. This can cause problems with data structures like dates
(in .NET 1.1 anyway), which are value types, and therefore can't be null.
If no valid date has been assigned to a Date type, it will contain some
default value which, IIRC, is something like 11-11-1111, or something
equally unhelpful - which can make it hard to store a NULL value in a
database Date field.

People more knowledgable than I am can probably correct and expand on the
above.

HTH
Peter

<ga********@myway.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.

Looking at his example I cant see any difference from a class here
other than it is defined with the struct keyword? what is the
difference between these two?

Also, Troelson speaks of the class type as having 'value based
semantics' what does that mean?

Thanks very much for your help.

Gary-

// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{ xPos = x; yPos = y;}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}

Dec 8 '06 #3

P: n/a
ga********@myway.com wrote:
I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.
That's unfortunate, as it's not a class... I'm not sure that
"lightweight" is really a good description either (it entirely depends
on the types involved).

The primary difference between a struct and a class is that structs are
value types and classes are reference types. I realise that doesn't
help on its own, but hopefully these articles will help to explain
things further:
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

(I'm still working on an article about *just* this matter...)

Jon

Dec 8 '06 #4

P: n/a
Thank you all for the replies. I'm finding your articles very useful
Jon.

In the first article, you state: -

" In .NET (and therefore C#) there are two main sorts of type:
reference types and value types. "

Troelson has stated: "In the world of .NET, "type" is simply
a generic term used to refer to a member from the set {class,
structure, interface, enumeration, delegate}."

I hope Troelsons quite is agreeable to people here because if it is, i
might be seeing some light at the end of the tunnel. I might actually
have a basic idea of what type means.

There are five different types available in C# class, structure,
interface, enumeration, delegate. Each of these types is of a certain
sort. Either value based, or reference based.
If it is value based the type directly contains its value, if it is
reference based it does not contain it's value, but instead refers to
that part of computer memory that contains its value.

Please tell me the above paragraph is sound?

(i'm still a bit confused about about native types like int etc.. i
don't quite see how they fit into the picture)

But! I will not post again on this until i have read both of Jon's
articles, and am confident I have a fair grasp of them. And until I am
at least until I have finished the current chapter of this book.
Because I know for each of you your time is precious!

Thanks!

Gary-


Jon Skeet [C# MVP] wrote:
ga********@myway.com wrote:
I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.

That's unfortunate, as it's not a class... I'm not sure that
"lightweight" is really a good description either (it entirely depends
on the types involved).

The primary difference between a struct and a class is that structs are
value types and classes are reference types. I realise that doesn't
help on its own, but hopefully these articles will help to explain
things further:
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

(I'm still working on an article about *just* this matter...)

Jon
Dec 8 '06 #5

P: n/a
Nooooooooooooooooooooooo! Not again.

But for the record, classes, structs, interfaces, enumerations and delegates
are all types that allow users to define their own types. So think of them
as meta-types or something.

Hope this is near enough to the truth not to start another monster thread.
Peter

<ga********@myway.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Thank you all for the replies. I'm finding your articles very useful
Jon.

In the first article, you state: -

" In .NET (and therefore C#) there are two main sorts of type:
reference types and value types. "

Troelson has stated: "In the world of .NET, "type" is simply
a generic term used to refer to a member from the set {class,
structure, interface, enumeration, delegate}."

I hope Troelsons quite is agreeable to people here because if it is, i
might be seeing some light at the end of the tunnel. I might actually
have a basic idea of what type means.

There are five different types available in C# class, structure,
interface, enumeration, delegate. Each of these types is of a certain
sort. Either value based, or reference based.
If it is value based the type directly contains its value, if it is
reference based it does not contain it's value, but instead refers to
that part of computer memory that contains its value.

Please tell me the above paragraph is sound?

(i'm still a bit confused about about native types like int etc.. i
don't quite see how they fit into the picture)

But! I will not post again on this until i have read both of Jon's
articles, and am confident I have a fair grasp of them. And until I am
at least until I have finished the current chapter of this book.
Because I know for each of you your time is precious!

Thanks!

Gary-


Jon Skeet [C# MVP] wrote:
>ga********@myway.com wrote:
I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.

That's unfortunate, as it's not a class... I'm not sure that
"lightweight" is really a good description either (it entirely depends
on the types involved).

The primary difference between a struct and a class is that structs are
value types and classes are reference types. I realise that doesn't
help on its own, but hopefully these articles will help to explain
things further:
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

(I'm still working on an article about *just* this matter...)

Jon

Dec 8 '06 #6

P: n/a
Hey, at least he didn't ask either of the "big two"... language choice; and
brace / indentation choice ;-p

(Gary: please, please, please don't make either of these your next post...)

Marc
Dec 8 '06 #7

P: n/a
ga********@myway.com wrote:
Thank you all for the replies. I'm finding your articles very useful
Jon.

In the first article, you state: -

" In .NET (and therefore C#) there are two main sorts of type:
reference types and value types. "

Troelson has stated: "In the world of .NET, "type" is simply
a generic term used to refer to a member from the set {class,
structure, interface, enumeration, delegate}."
Right. Delegates and classes are reference types. Structures and
enumerations are value types. Interfaces are *sort of* reference types.
Certainly if you have something like:

IDisposable x = ...;

then x is a reference rather than a value type value. But value types
can implement interfaces... basically, interfaces are "a bit
different"!
I hope Troelsons quite is agreeable to people here because if it is, i
might be seeing some light at the end of the tunnel. I might actually
have a basic idea of what type means.
Yup, I have no argument with the quote above.
There are five different types available in C# class, structure,
interface, enumeration, delegate. Each of these types is of a certain
sort. Either value based, or reference based.
If it is value based the type directly contains its value, if it is
reference based it does not contain it's value, but instead refers to
that part of computer memory that contains its value.

Please tell me the above paragraph is sound?
Not quite. There are lots of different types available - System.String
is a type, System.Object is a type, etc. It's better to say there are
five different *kinds* of type available in C#.
(i'm still a bit confused about about native types like int etc.. i
don't quite see how they fit into the picture)
It's probably not worth worrying about them for the moment. All the
primitive types are value types, and you can probably just think of
them as normal structs for the most part, which just happen to get
their own IL etc.
But! I will not post again on this until i have read both of Jon's
articles, and am confident I have a fair grasp of them. And until I am
at least until I have finished the current chapter of this book.
Because I know for each of you your time is precious!
No problem :)

Jon

Dec 8 '06 #8

P: n/a
Hi,

(lol)

and here's that thread in case the OP wants to read it:

http://groups.google.com/group/micro...60b043afab22f7

--
Dave Sexton

"Peter Bradley" <pb******@uwic.ac.ukwrote in message
news:uF**************@TK2MSFTNGP02.phx.gbl...
Nooooooooooooooooooooooo! Not again.

But for the record, classes, structs, interfaces, enumerations and
delegates are all types that allow users to define their own types. So
think of them as meta-types or something.

Hope this is near enough to the truth not to start another monster thread.
Peter

<ga********@myway.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
>Thank you all for the replies. I'm finding your articles very useful
Jon.

In the first article, you state: -

" In .NET (and therefore C#) there are two main sorts of type:
reference types and value types. "

Troelson has stated: "In the world of .NET, "type" is simply
a generic term used to refer to a member from the set {class,
structure, interface, enumeration, delegate}."

I hope Troelsons quite is agreeable to people here because if it is, i
might be seeing some light at the end of the tunnel. I might actually
have a basic idea of what type means.

There are five different types available in C# class, structure,
interface, enumeration, delegate. Each of these types is of a certain
sort. Either value based, or reference based.
If it is value based the type directly contains its value, if it is
reference based it does not contain it's value, but instead refers to
that part of computer memory that contains its value.

Please tell me the above paragraph is sound?

(i'm still a bit confused about about native types like int etc.. i
don't quite see how they fit into the picture)

But! I will not post again on this until i have read both of Jon's
articles, and am confident I have a fair grasp of them. And until I am
at least until I have finished the current chapter of this book.
Because I know for each of you your time is precious!

Thanks!

Gary-


Jon Skeet [C# MVP] wrote:
>>ga********@myway.com wrote:
I have 'cli via c# on order', and in the mean time am reading 'Pro C#
2005 and the .NET platform' (Andrew Troelson). I'm just reading about
the 'five types defined in the CTS'. Specifically Struct. Now Troelson
described the struct type as 'a lightweight class type having value
based semantics'.

That's unfortunate, as it's not a class... I'm not sure that
"lightweight" is really a good description either (it entirely depends
on the types involved).

The primary difference between a struct and a class is that structs are
value types and classes are reference types. I realise that doesn't
help on its own, but hopefully these articles will help to explain
things further:
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

(I'm still working on an article about *just* this matter...)

Jon


Dec 8 '06 #9

P: n/a
Erm... isn't that the OP's thread anyway?

Marc
Dec 8 '06 #10

P: n/a
Hi Marc,

Actually, that thread got so out of control I forgot who started it ;)

It's not a bad idea to link the two anyway since they are both referring to
the CTS, so no harm done I hope.

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:Ox**************@TK2MSFTNGP06.phx.gbl...
Erm... isn't that the OP's thread anyway?

Marc

Dec 8 '06 #11

P: n/a
Gary,

I think you are getting very close, but don't worry that this is
difficult for you. Computer scientists have worked on defining type
semantics for decades.

ga********@myway.com wrote:
There are five different types available in C# class, structure,
interface, enumeration, delegate. Each of these types is of a certain
sort. Either value based, or reference based.
If it is value based the type directly contains its value, if it is
reference based it does not contain it's value, but instead refers to
that part of computer memory that contains its value.

Please tell me the above paragraph is sound?

(i'm still a bit confused about about native types like int etc.. i
don't quite see how they fit into the picture)
I think I would make a small change to your paragraph and say "There
are five different kinds of user definable types". As another poster
said, there are lots of types, so these five categories are kinds of
types. A sixth kind of type is "numeric", and include int, float,
double, etc. However, numeric types are built in to the language, and
users of the language cannot define new numeric types. They are also
value types, so as far as how they are stored and passed as parameters,
numeric types like int and float behave like single field structs.
Finally, bools would be a predefined enumeration that has special
meaning to control statements like "if" and "while".

Dec 9 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.