473,418 Members | 2,340 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,418 software developers and data experts.

Struct a lightweight class type having value based semantics?

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
11 2339
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
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
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
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
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
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
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
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
Erm... isn't that the OP's thread anyway?

Marc
Dec 8 '06 #10
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods,...
4
by: Danny Tan | last post by:
Hi folks, I'm a newbie, trying to pick up C#. This is something, I'm currently working on for my assignment. I understand that struct is a "value type". And I'm trying to pass a struct by...
9
by: ^MisterJingo^ | last post by:
I'm new to C# (literally 2 days) and need a bit of help understanding an error. struct Blah { int x; int y; public int X {
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
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...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.