1. OnlineBuff

What are structures? Differentiate between structures and classes

What are structures?

Structures are value types.
They get allocated on stack.
They are group of logically related data item.
They do not follow or implement object orient concepts.
All value types such as int, bool and char are structs.
The structure in C# can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.

Structures are useful in

C# Structures are useful for small data structures like having value semantics for creating a graph points and many more such areas where you can apply structures.

Syntax

 
struct StructName{
……Code
}   

Syntax of a structure starts with "struct" keyword followed by structure name

For Example

struct MalingList
{
  private string _name;
  private int _doornumber;
  private string _street;
  private string _city;
  private string _pincode;
} 

So this is all about structures in c-sharp. Now let's go and understand about classes.

What is a Class?

In OOPs concept Class is a general thing.
Class is nothing but a blue print or template.
Classes are reference types and they get allocated on heap.

Syntax

class className{
……Code
}  

Syntax of a class starts with "class" keyword followed by classname

For Example

class MalingList
{
  private string _name;
  private int _doornumber;
  private string _street;
  private string _city;
  private string _pincode;
} 

For Your References

Stack- It holds value type variables plus return addresses for functions. All numeric types, ints, floats and doubles along with enums, chars, bools and structs are value types.

Heap- The heap hold variables created dynamically known as reference variables and mainly instances of classes or strings.

Value Types - A data type is a value type if it holds the data within its own memory allocation.

Reference Types - A reference type contains a pointer to another memory location that holds the data.

Differentiate between structures and classes

1st Difference

Structures are value types.
Classes are reference types.

2nd Difference


Since structures are value types so they get allocated on stack.
Since classes are reference types so they allocated on heap.

3rd Difference


It terms of memory consumption structs takes less memory as same compared to classes.
It terms of memory consumption classes takes more memory as same compared to structs.

Checking Memory Usage on Stack and Heap

Now let's demonstrate structures and classes by checking the memory consumption of classes and structures on stack and heap.

For this example I will be using a Windows form application

1st Step

First step create a windows form application project and then drop a single button on Form 1 of windows screen as shown below.

2nd Step

Second step on a separate CS file create a structure "strGraph" with two properties of "int" data type as shown below.

public struct strGraph{

        private int _x;
        private int _y;
        public int X{
          get { return _x; }
          set { _x = value; }
        }               
        public int Y{
          get { return _y; }
          set { _y = value; }
        }

}

3rd Step

Third step again on a same CS file create class "clsGraph" with two properties of "int" data type as shown below.

public struct clsGraph{

        private int _x;
        private int _y;
        public int X{
          get { return _x; }
          set { _x = value; }
        }               
        public int Y{
          get { return _y; }
          set { _y = value; }
        }

}

The structures in C# seems too similar to classes. But they are two entirely different aspects of the language.

4th Step

Fourth step we will go back to our Form 1 double click on a button Form 1 CS file will open there on button  click event we will create loop of 100 * 1000 times using FOR Loop as shown below.

private void button1_Click(object sender, EventArgs e){
 
 for (int i = 0; i <= 100 * 1000; i++){

  }

}

Now first we will create an object of structure in For Loop and try to record memory usage using CLR Profiler as shown below.

CLR Profiler - It's a tool and it is been used to check memory consumption of each object been used in an application or website.You can download this tool from an official site of microsoft http://www.microsoft.com/en-in/download/default.aspx

private void button1_Click(object sender, EventArgs e){
 
 for (int i = 0; i <= 100 * 1000; i++){
   strGraph obj = new strGraph();
  }

}                    

Next step run the CLR profiler

Click on start application then select windowns application exe (your project exe) after that program will run automatically then click on the button on the form and close the application then automatically CLRProfiler will create a report as shown below.

As you can see from above screen shot in Allocated bytes structure has consumed 400,956 bytes of memory on stack.

Now let's repeat the same process for class we will create a class object in FOR Loop as shown below.

private void button1_Click(object sender, EventArgs e){
 
 for (int i = 0; i <= 100 * 1000; i++){
   clsGraph obj = new clsGraph();
  }

} 

So now using our CLR Profiler let's check the output of memory consumption of a class.

As you can see from above screen shot in Allocated bytes class has consumed 3,600,344 bytes of memory.

From this above example it is confirmed that.

It terms of memory consumption structs takes less memory as same compared to classes.
It terms of memory consumption classes takes more memory as same compared to structs.

So this is all about structures and difference between structures and classes. So friends if you have any doubts regarding this topic. Kindly let me know through your comments because your valuable comments will help me to grow. If you like this article please share this with your friends. Thanks

Author: Gurunatha Dogi

is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more. Follow me @ and twitter.... read more

Comments

64x64
By Sudhesh on 2014-11-27
nice article

Add a Comment