C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 3rd, 2006, 01:29 PM
C_rookie C_rookie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 35 C_rookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 14 m 5 sec
Reputation Power: 10
Difference between struct and class in C#

I am just learning C# and in the book "Presenting C#" by Christoph Wille it says "the difference between struct and class in C# is that struct is a value type and class is a reference type". It goes on to say that a struct in C# is "more suitable for scenarios in which you need a large number of small objects".

My question is, if you can store objects of different types inside a struct, as well as define functions and so on, how could a struct be a value type in the first place? Can someone explain this to me.

Reply With Quote
  #2  
Old September 3rd, 2006, 02:28 PM
voltaire's Avatar
voltaire voltaire is offline
Ninja philosophism
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: London, England
Posts: 440 voltaire User rank is Sergeant (500 - 2000 Reputation Level)voltaire User rank is Sergeant (500 - 2000 Reputation Level)voltaire User rank is Sergeant (500 - 2000 Reputation Level)voltaire User rank is Sergeant (500 - 2000 Reputation Level)voltaire User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 5 h 22 m 38 sec
Reputation Power: 26
Suppose I have this setup:
Code:
public class Example
{
  // it's a class
}

public class Helper
{
    public void Foo(Example e)
    {
        // here
    }

    public static void Main(string[] args)
    { 
        Helper h = new Helper();
        Example myExample = new Example();
        h.Foo(myExample);
    }
}


Since Example is a class, and myExample is an instance of that class, myExample is a reference. When the Foo method gets hold of the argument passed in to it (i.e. where it's labelled 'here'), it's a reference to the same myExample object that was created in the static Main.

Underneath, it just represents a memory location where all it's information is stored. The Foo method gets a copy of the same memory location.


Now suppose we have this:


Code:
public struct Example
{
  // it's now a struct
}

public class Helper
{
    public void Foo(Example e)
    {
        // here
    }

    public static void Main(string[] args)
    { 
        Helper h = new Helper();
        Example myExample = new Example();
        h.Foo(myExample);
    }
}


Now, since it's a struct, instead of being the memory location pointing to the object's data, it's the memory location of the data stored in the struct.
This time around, when we pass it in to the method (labelled 'here') we get a whole new copy of all the data, and underneath it's pointing to the memory location of this copied data.


Quote:
Originally Posted by C_rookie
My question is, if you can store objects of different types inside a struct, as well as define functions and so on, how could a struct be a value type in the first place?


If a struct 'store objects of different types' then it's only storing references to these objects, not the object's data themselves. The reference is a memory location plus a few other bits. The object being referred to in this way could be huge, but the reference to it is always the same size.
__________________
Le mieux est l'ennemi du bien.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Difference between struct and class in C#

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap