Geeks With Blogs
Mark Pearl

 

I have always been slightly confused about the difference between classes and structures in C#. For many years, structures seemed identical to classes, but were simply not as extensible. Recently I had a relook at them and came up with two key identifying features that help me differentiate the two..

  1. Where they are stored
  2. Value and Reference values

So, the first main difference for me is that structure instances are stored on the stack and class instances are stored on the heap.

The second main difference is that structures are value types while classes are reference types. With this in mind it is interesting to see the differences between structures and classes in the following code snippet and output.

using System;

namespace DifferenceBetweenClassesAndStructures
{

    struct StrucutreExample
    {
        public int x;        
    }

    class ClassExample
    {
        public int x;        
    }

    class Program
    {
        static void Main(string[] args)
        {
            StrucutreExample st1 = new StrucutreExample(); // Not necessary, could have done StructureExample1 st1;
            StrucutreExample st2;

            ClassExample cl1 = new ClassExample();
            ClassExample cl2;                        

            cl1.x = 100;
            st1.x = 100;

            cl2 = cl1;
            st2 = st1;

            cl2.x = 50;
            st2.x = 50;

            Console.WriteLine("st1 - {0}, st2 - {1}", st1.x, st2.x);            
            Console.WriteLine("cl1 - {0}, cl2 - {1}", cl1.x, cl2.x);            
            Console.ReadLine();
        }
    }
}

 

As you can see form the above code, we have instantiated 2 instances of a structure and 2 instances of a classe identically, and then pointed the second instance of the structure and the class to the first instance of each. The output is different, while the second instance of the class is pointing via reference to the first instance of the class, with the structure the second instance makes an independent copy of the first structure and so when you change the 2nd structures value, it does not effect the first structures value.

So with that in mind, the output would be as follows…

2011-07-19 01-07-19 PM

 

And there you go, two major differences for me between structures and classes in C#

Posted on Tuesday, July 19, 2011 4:33 PM C# | Back to top


Comments on this post: 2 main differences between Structures and Classes in C#

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Another important difference: Structs cannot be set to null. An object based upon a class may be null.

-Greymarch
www.greymarch.com
Left by Greymarch on Jul 19, 2011 6:02 PM

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Yup, true as well - thanks for the comment.
Left by MarkPearl on Jul 19, 2011 6:06 PM

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Classes will always be in the heap, but Structs may be in the stack or the heap depending on the situation. It is possible to make an array of a struct type that's bigger than your stack space.
Left by Chris Nash on Jul 19, 2011 6:08 PM

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Chris, that's an excellent point... thanks for expanding on it!
Left by MarkPearl on Jul 20, 2011 7:43 AM

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Just to add to the comment from Chris, if a sturct is the field of a class that it will be stored on the heap along with the class. There are other situations where this happens also.
Left by Matt Warren on Jul 20, 2011 11:37 AM

# re: 2 main differences between Structures and Classes in C#
Requesting Gravatar...
Struct is value type and Class is Reference type. So All the value type Vs reference type difference apply here.. Nice discussion here.. Even you can visit below link where i got good answer.

http://www.hyperneed.com/ShowSearchAnswers.aspx?searchstring=&category=Programming&questionid=f578b704-69a2-4fdb-a1b1-1809da2b1757
Left by michael on Jun 14, 2012 12:10 PM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net