Norway


Here you will learn about between and .

A class and a structure, both are user defined types and understanding the difference between both of them might be confusing at times. In order to understand this, the following table shows clear differences between the two.

Class Structure
It is a reference data type and uses the keyword “class”. It is a value data type and uses the keyword “struct”.
Object for a class is created in the heap memory. Object for a structure is created in the stack memory.
We can always inherit another class. i.e., the concept of inheritance is applied . Structures can never be inherited.
Object is created using the “new” keyword. We may or may not use the keyword “new” while objects.
It occupies more space. A structure occupies less space.
Class allows both the parameterized and the non parameterized constructors. It only allows for the parameterized constructors, even the default constructors cannot be used.
Example:

class fruits

{

Fruit F1;

F1.apple= “red”;

F1.mango=”yellow”;

}

Example:

public struct fruit

{

public string apple;

public string mango;

}




Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here