Home > .Net / C#, C/C++, General Programming > Difference between structures in C, C++ and C#

Difference between structures in C, C++ and C#

Introduction

C, C++ and C# are all languages from the same family. They have a lot of similarities, the basic syntax is the same, a lot of their features and terminology are interchangeable. Yet there are also a lot of differences in these seemingly similar terms. 

One of these terms is the structure. There are structs in all three languages, but they have entirely different functionality and behavior. Keep reading to find out more about them. 

 

The structure in C

Let’s do this chronologically. 

We all know C is the ancestor of C++, Java and C#. It’s a procedural language, not object-oriented, with high level of manual memory control and no concept of classes and methods. But still, there is a way to modularize the data in a convenient manner – using the struct constructs. 

An example of a simple structure in C:

with the following usage:

The structures are by default allocated on the stack. 

 

Using the C struct in combination with typedef

Another interesting usage is the combination with typedef. For example, the above code would become:

After that you can use it like this:

The structures in C does not allow direct function declarations, but you can define a pointer to a function and use it in a similar way, like this:

 

The structure in C++

In fact, there is not much to be said regarding the structures in C++. The only different between the class and the structure in C++ is that all of the struct members are public by default, while all the members of the class are private. 

The definition of the struct is quite similar to the one in C:

One difference, however, is the fact that in C++ you can directly define a function in a struct, just as you would do in a class. 

 

The structure in C#

The struct in C# is yet another version, the most perfected one in my humble opinion. The relations between all the structures can be seen clearly from the following diagram I’ve created:

C#-struct-image

In fact, we very often use structures in our day-to-day programming. Every time we use int, float or double, we are actually creating an instance of a structure on the stack. Few of my favourite examples are:

  • int – an alias for the System.Int32 structure
  • float– an alias for the System.Single structure
  • void – yes, your return type is actually a structure
  • enum - all the enumerations are also structures

Strictly speaking the structure is also a class, because it inherits from Object.


Features of the C# struct

As I said, the struct in C# is quite similar to the class. The biggest difference is that it has somehow limited functionality, but it’s allocated on the stack instead on the heap. This makes it a perfect choice for objects with small lifetime, like primitive values.

The structure in C#:

  • Can have fields, methods, indexers, properties, evenets. 
  • Can have constructors, but no destructors
  • Have default constructors which cannot be changed
  • Can implement a number of interfaces
  • Can be instantiated with or without the New operator
  • If the New operator is used, the constructor will be called. Otherwise, all the fields will not be initialized. Note that even value types will not be initialized.
  • Cannot be used for base classes
  • Cannot inherit any classes or structures

Having said that, the following structure is a perfectly valid example:

 

When to use a struct instead of a class ?

From Microsoft's official documentation : 

Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

In short, use struct when you want something to behave like a value type, not a reference type. If speed is your goal, you'll find structures a little bit more benefitial than classes in case the above conditions are not met. 

 

The primitive types in C++, Java and C#

I think it will also be interesting to note the differences between the primitive types in these languages – int, float, double, etc. 

In C and C++, the primitive variables are nothing more than named cells in the memory. 

In Java, the primitive types are the same, but you also can use the wrappers (for example, java.lang.Integer) that provide a lot of extra functionality and can handle null references. The process of wrapping a primitive variable in a wrapper class is called boxing, and is in the technique heavily used in the Java generics implementation

In C#, the primitive types are in fact structures. int is System.Int32 in 32 bit operating systems, System.Single is float, System.Double is double. This allows us to utilize the speed benefits of storing values on the stack, and have extended functionality (although limited) compared to a class. Also note that the structure can be initialized on the heap as well, like when it is a class member. 

 



About the author:
Kosta Hristov (34 Posts)

Hi there ! My name is Kosta Hristov and I currently live in London, England. I've been working as a software engineer for the past 6 years on different mobile, desktop and web IT projects. I started this blog almost one year ago with the idea of helping developers from all around the world in their day to day programming tasks, sharing knowledge on various topics. If you find my articles interesting and you want to know more about me, feel free to contact me via the social links below. ;)




Like the article ? Share it ! ;)


  1. July 10th, 2013 at 20:58 | #1

    You made a mistake: "bool – an alias for the System.Single structure" This is incorrect. float is an alias for System.Single, bool is an alias for System.Boolean.

  2. Jon
    July 10th, 2013 at 20:59 | #2

    "Also, the struct in C++ is allocated on the heap." – Can you elaborate on this?  I believe this is incorrect.

  3. Pg
    July 10th, 2013 at 21:07 | #3

    Structures and classes in c++ are allocated on stack by default. To allocate them on heap you have to use operator new

  4. July 10th, 2013 at 21:59 | #4

    Hi Judah, 

    You are right, it was an oversight, Thanks. 

  5. July 10th, 2013 at 22:00 | #5

    Jon, Pg, 

    Yep, that's a mistake. I actually meant exactly the opposite. Thanks. ;)

    Regards, 
    Kosta

  6. Michael
    July 10th, 2013 at 22:17 | #6

    Very interesting article, thanks for sharing. ;]

  7. July 10th, 2013 at 22:25 | #7

    Glad you like it. ;)

  1. No trackbacks yet.

Current month ye@r day *


Copyright © Developing the future 2013. Licensed under the CC BY-NC-ND 3.0 Creative Commons license.