Johnny posted on June 27, 2008 05:20

A few days ago I was in a discussion with a friend of mine who interviewed for a senior level developer position. He mentioned that one of the questions he was asked in his technical interview was "Describe a scenario where you would use a Struct instead of a Class.". He told me he could not think of one. So this article is an attempt to both explain the differences between a Class and a Struct and also describe when a struct should be used instead of a class.

Reference Type vs. Value Type
One of the primary differences between a Class and a Struct is that a Class is a reference type and a Struct is a value type. What does this mean? Well, let's take a look at other value types. All of the primitive types such as int, bool, decimal etc. are value types. Value types are stored in the stack as a single space in memory whereas refernce types are created in memory and a reference to that space is also create. The reference type is always accessed via the reference and not directly. For example;

Point p1 = new Point();       //Point is a Struct
Form f1 = new Form();       //Form is a Class

Point p2 = p1;
Form f2 = f1;

In the above code example p2 is an independent copy of p1. If you were to change a value of a property of p2, p1 would not be affected. Whereas f2 is a copy of the refence to f1 there for f1 and f2 are both references to the same object in memory. If you were to change a value of a property of f2, f1 would also be changed.

Heap vs. Stack
The stack is a simple FILO (first in last out) memory structure. The Stack is a highly efficient memory structure that "bookmarks" the stack when a method begins execution, Dumps data into the stack during the method execution and once the method exection completes the stack is reset to the bookmark releasing all the method's allocated memory.

The heap is just the opposite. The heap can be considered, for lack of a better term, a random access memory structure. Objects can be allocated and deallocated in a random order. The heap requires the use of a memory manager and garbage collector to maintain it's structure.

When you instantiate a class the instance of the class itself is stored in the heap whereas the reference to the instance of the class is stored in the stack.

Form f1 = new Form();

The actual object created by the above code will be placed in the heap and the reference, f1, to that object is stored in the stack.

When to use a Struct
Keep the following rules in mind when using a struct:
- a struct should represent a single value
- a struct should have a memory footprint less than 16 bytes
- a struct should not be changed after creation.


Posted in: General , Programming  Tags: , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Johnny posted on April 22, 2008 07:19

The inspiration for the name of this blog came to me when I was searching the web for methods of teaching my ten year old son how to program. So I thought the subject would be a good topic for my very first blog post. Well, here goes. Go easy on me!

Getting Started

When I first began researching the best method for introducing my son to software development I came across numerous resources. The first one to really catch my eye was Microsoft's Beginning Developer Learning Center.  This site, of course, focuses mainly on the .NET development world but offers some great introduction to key programming concepts like, problem solving, how computers process data and even an introduction to HTML. The site provides a great set of lessons for my son and me to do together.

What I found out next

While these type of sites and lessons are interesting to me, my son did not find them so. After I thought about it I could see his point. I remember why I started programing, I want to play games. I started with a Commodore 64 with and a subscription to Compute Gazette and not even a single game cartridge. If I wanted to play a game I had to sit for countless hours typing line by line, games from the back of the magazine. Even then, once the game code had all been entered there would still be many more hours of debugging to find all the places where I had misspelled a word or used a period when it should have been a comma. Finally, the game was coded, debugged and ready to run.... What... oh no... the game SUCKS! What to do now? Having the spirit of youth I did not give up. If the game had a guy that ran I would make him run faster. If he shot bullets I would make him shoot more bullets. This is how I learned to program.

So what should I do?

Obvious! We should develop a simple game to introduce my son to the concepts of software development. I briefly fiddled around with Microsoft's XNA platform and found that although very interesting I just did not have the time nor the reason to dig very deep. Well, now I do! What better reason than to teach my son software development.

What next?

Over the next few weeks I will be continuing to post updates to our progress. This weekend we will be sitting down to our first design session to brainstorm about ideas for a simple game. If you have any ideas please feel free to post a comment below.


Posted in: Kids and Software  Tags: , ,

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search Blog Post

Follow me on

Twitter Updates

    Recent Comments

    Disclaimer
    The opinions expressed herein are my own personal opinions.

    © Copyright 2010 Johnny Can't Code