Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a difference between a struct in c++ and a struct in c#?

Tags:

is there a difference between a struct in c++ and a struct in c#?

like image 580
coder Avatar asked May 18 '09 20:05

coder


People also ask

Can you put a struct in a struct in C?

A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.

Are structs the same in C and C++?

Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.

What is the difference between C struct and C class?

Main differences between the structure and classBy default, all the members of the structure are public. In contrast, all members of the class are private. The structure will automatically initialize its members. In contrast, constructors and destructors are used to initialize the class members.

Is struct in C like an object?

C has structs and is not object oriented.


2 Answers

In C# you use structs to define value types (as opposed to reference types declared by classes).

In C++, a struct is the same thing as a class with a default accessibility level of public.

So the question should be: are structs in C# different from classes in C++ and, yes, they are: You cannot derive from C# structs, you cannot have virtual functions, you cannot define default constructors, you don't have destructors etc.

like image 66
MartinStettner Avatar answered Oct 10 '22 00:10

MartinStettner


Structs in C# very different from classes, see Structs vs Classes for more

Structs in C++ are identical to classes, EXCEPT that members are public by default. Other than that, a C++ struct can do everything a C++ class can do.

like image 39
Binary Worrier Avatar answered Oct 10 '22 02:10

Binary Worrier