C# FAQ: Are structs the same in Cplusplus and CSharp

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Are structs the same in C++ and C#?

A C++ struct and a C++ class are virtually the same thing. The difference between them is the default visibility level—structs are public: classes are private.

On the other hand, C# structs and classes are very different animals. Actually, a C# struct is more like a C struct than a C++ struct. In C#, classes are reference types—instances stored on the heap and accessed indirectly via a reference. However, structs are value types—instances which are stored either directly on the stack or inlined inside heap objects. Although structs can implement interfaces, they cannot inherit from classes or other structs. Lastly, structs cannot have destructors.


Personal tools