What is the difference between Class and structure?

Author Topic: What is the difference between Class and structure?  (Read 4179 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • We Helpline
     Structure used to bundle data’s of different data types to perform a particular function. Extended function of the structure is the class.
The major difference is that any variable declared inside the structure is public whereas any variable declared inside the class is private, meaning the variable has meaning only inside the class.


Offline michral86

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 1
  • Posts: 34
  • Karma: 0
  • Gender: Female
Re: What is the difference between Class and structure?
« Reply #1 on: 04-25-2017, 03:16:16 »
You can differentiate  Class and Structure as below:
Class
Class is a reference type and its object is created on the heap memory. It can inherit the another class. It can have the all types of constructor and destructor.
Example:
obj=new Demo();
Structure
Structure is a value type that is why its object is created on the stack memory.
It does not support the inheritance. It can only have the parametrized constructor. it means a structure can not have the non-parametrized constructor,default constructor and destructor also.
Example:
Demo obj;
I hope my answer helps you.

Offline richardmsmith

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 3
  • Posts: 221
  • Karma: 0
  • Gender: Male
    • Trucking App Solution
Re: What is the difference between Class and structure?
« Reply #2 on: 07-17-2018, 02:52:40 »
A structure is a value type so it is stored on the stack, but a class is a reference type and is stored on the heap. A structure doesn't support inheritance, and polymorphism, but a class supports both. By default, all the struct members are public but class members are by default private in nature.

Offline greatshivam

  • Trade Count: (0)
  • Newbie
  • *
  • Thank You 0
  • Posts: 5
  • Karma: 0
Re: What is the difference between Class and structure?
« Reply #3 on: 02-24-2020, 05:53:11 »
1: Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
2: Classes can be inherited whereas structures not.
3: A structure couldn't be null like a class.
4: A structure couldn't have a destructor such as a class.
5: A structure can't be abstract, a class can.
6: You cannot override any methods within a structure except the following belonging to the type object:
 
Equals()
GetHashCode()
GetType()
ToString()

And the other polymorphism technique used for structures is implementing interfaces.
 
7: Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
1218 Views
Last post 07-20-2012, 01:09:31
by tomjohn
0 Replies
939 Views
Last post 02-13-2016, 00:36:44
by chinmay.sahoo
3 Replies
2335 Views
Last post 12-13-2016, 23:31:12
by saravjeet
0 Replies
1112 Views
Last post 09-03-2016, 03:43:26
by beingchinmay
1 Replies
3445 Views
Last post 01-18-2017, 00:26:30
by wellliving