Click here to Skip to main content
12,690,140 members (23,977 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked

Structs in C#, Structs vs. Classes, Heap or Stack? Structs Constructors

, 25 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes Structs in C#, Structs vs. Classes, Heap or Stack? Structs Constructors


This simple article describes structure syntax and usage of structs. This article also covers the important differences between classes and structs, default constructors and heap or stack?


A C# structure is a value type and the instances or objects of a structure are created in stack. The structure in C# can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.

A structure in C# is simply a composite data type consisting of a number of elements of other types.

The .NET Framework defines built-in value types, such as System.Int32 and System.Boolean, which correspond and are identical to primitive data types used by programming languages.

All built-in value types are structures in .NET. In .NET, all Value types and Reference types are derived from System.Object. The System namespace is the root namespace for fundamental types in the .NET Framework. This namespace includes classes that represent the base data types used by all applications: Object (the root of the inheritance hierarchy), Byte, Char, Array, Int32, String, and so on. Many of these types correspond to the primitive data types that your programming language uses.

All of the simple types are aliases of the .NET Framework System types. For example, int is an alias of System.Int32. The C# type keywords and their aliases are interchangeable. For example, you can declare an integer variable by using either of the following declarations:

int i=32; 

or: i=1;

As I have already described. The structure in C# can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.

Now let's have look at each step by step.

Using the Code

// MyFirst_Struct.cs
using System;
struct SimpleStruct
    //how the fields are declared in the structure.
    private int _value;
    // how the properties are declared in the structure.
    public int MyProperty 
            return _value;
            if (value < 100)
            _value = value;
    // how the methods are declared in the structure
    public void Display_value()
        Console.WriteLine("The stored value is: {0}", _value);
    // Default Constructors
    SimpleStruct ()
        //A struct type is however permitted to declare parameterized //constructors. 

class MyTestClass
    public static void <place w:st="on" />Main</place />


The stored value is: 310

This example shows that when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.

// MySecond_Struct.cs
using System;
class TheClass
 public int x; 

struct SecondStruct
 public int x; 
class TestClass
    public static void structtaker(SecondStruct _Struct) 
        _Struct.x = 5;
    public static void classtaker(TheClass _Class)  
        _Class.x = 5; 
    public static void <place w:st="on" />Main</place />


ss.x = 1
tc.x = 5

This example shows that when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.

Default Constructors

In C#, every value type implicitly has a public parameter less default constructor. Like any other constructor, the default constructor of a value type is invoked using the new operator. So it is not possible for a struct type to contain an explicit declaration of a parameter less constructor.

You are also introduced to the following topics:

  • Structs vs. Classes
  • Heap or Stack?
  • Constructors

Structs vs. Classes

Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?

When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.


Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on the stack and that you're not dealing with references to them but dealing with them directly. Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs might be a good option.


  • 25th September, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Pakistan Pakistan
I'm from Pakistan and have two years of experience in C# and in visual stdio 2005 ,2008, 2010 and SQL Server 2000/2005/2008

You may also be interested in...

Comments and Discussions

QuestionJust what I needed Pin
Nico Encarnacion7-Feb-13 6:14
memberNico Encarnacion7-Feb-13 6:14 
GeneralExplicit Parameter Constructors Pin
polczym27-Jul-09 4:08
memberpolczym27-Jul-09 4:08 
GeneralReference types are passed by value Pin
JohnWPowell23-Nov-07 3:23
memberJohnWPowell23-Nov-07 3:23 
Generalnice idea... Pin
blackjack215026-Sep-07 3:13
memberblackjack215026-Sep-07 3:13 
GeneralRe: nice idea... Pin
idreeskhan26-Sep-07 19:55
memberidreeskhan26-Sep-07 19:55 
General"enjoy their benefits as well" Pin
inetfly12325-Sep-07 4:32
memberinetfly12325-Sep-07 4:32 
GeneralRe: "enjoy their benefits as well" Pin
idreeskhan25-Sep-07 20:46
memberidreeskhan25-Sep-07 20:46 
Structures are generally used for small objects that contain few data members with a fixed size of 16 bytes or less.
They are also allocated on the stack without any involvement of the garbage collector. For each structure, an implicitly defined default (parameterless) constructor is always generated to initialize structure members to their default values. Therefore, unlike classes, explicit default constructors are not allowed. In C#, there is also no inheritance of classes for structures. Structures inherit only from the class System.ValueType, which in turn inherits from the root class object. Therefore, all members of a struct can only be public, internal, or private (by default). Furthermore, structures cannot be used as the base for any other type but can be used to implement interfaces. The structure Node encapsulates one reference and one value field, name and age, respectively. Neither name nor age can be initialized outside a constructor using an
struct Node
public Node(string name, int age)
{ = name;
this.age = age;
internal string name;
internal int age;
An instance of a structure like Node is created in one of two ways. As with classes, a structure can use the new operator by invoking the appropriate constructor. For example,
Node node1 = new Node();
creates a structure using the default constructor, which initializes name and age to null and 0, respectively. On the other hand, Node node2 = new Node ( "idrees", 18 );
creates a structure using the explicit constructor, which initializes name to idrees and age to 18. A structure may also be created without new by simply assigning one instance of a structure to another upon declaration:
Node node3 = node2;
■ Value Types
However, the name field of node3 refers to the same string object as the name field of node2.
In other words, only a shallow copy of each field is made upon assignment of one structure
to another. To assign not only the reference but the entire object itself, a deep copy
is required, Because a struct is a value rather than a reference type, self-reference is illegal.
Therefore, the following definition, which appears to define a linked list, generates a compilation error.
struct Node {
internal string name;
internal Node next;

GeneralRe: "enjoy their benefits as well" Pin
inetfly12326-Sep-07 3:59
memberinetfly12326-Sep-07 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 25 Sep 2007
Article Copyright 2007 by idreeskhan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid