Structures in C# and difference between C# Structure and C++ Structure, easy Tutorial

What is Structure in C#:

Let us first understand that what are structures, the answer is that Structure in a DATA TYPE that can hold different types of data within a single group. It can include following type of data:

  • Custom Data Types
  • Can have methods
  • Can have constructor
Remember that it can not implement inheritance. Example of a constructor is given below:

class Program
        struct ExampleStruct // Declares a algorithms
            public ExampleStruct(string a)
                //Constructor implementation
            public void method()
                // Method Implementation
            public int datamember;


Defining a Structure:

Structure can be defined by:
ExampleStruct es;

Accessing the Member of a Structure:

The member of a structure can be accessed by: Structure Variable.Member Variable. For Example:

            es.datamember; // es is the definition of Structure we discussed above

Classes VS Structs:

Reference type
Value type
Can inherit
No inheritance
Can have a destructor
No destructor

C# Structs VS C++ Structs:

There is a big difference between C# Structures and C++ Structures

C++ Struct
C# Struct
Same as C++ class, but all members are public
User-defined value type
Members are public by default, but you can change access modifier
Members can be public, internal or private

Hit Like Button if you understood, It helps a lot to keep us Motivated.


Post a Comment