Difference between classes and structs in C sharp Arabic #28


 Difference between classes and structs in C sharp Arabic #28

Suggested Videos

Structs in C sharp Arabic #27

❑ Difference between classes and structs.




structs

❑ We understood that structs are very similar to classes. We use struct keyword to create a struct.

❑ Just like classes structs can have fields, properties, constructors and methods.

❑ However , there are several differences between classes and structs, which we will explore in this session.



Classes VS Structs

A struct is a value type where as a class is a reference type .

❑ All the differences that are applicable to value types are also applicable to classes and structs.
❑ Structs are stored on stack, where as classes a are stored on the heap.
❑ Value types hold their value in memory where they are declared, but reference types hold a Reference to an object in memory.
Value types are destroyed immediately after the scope is lost, where as for reference types only the reference variable is destroyed after the scope is lost . The object is later destroyed by garbage collector.(we will talk about this in the garbage collection session).
❑ When you copy a struct into another struct, a new copy of that struct gets created and modifications on one struct will not affect the values contained by another struct.
❑ When you copy a class into another class, we only get a copy of reference variable. Both the reference variables point to the same object on the heap.so ,operations on one variable will affect the values contained by another reference variable.

Stack and Heap






Classes VS Structs



❑ Structs can’t have destructors, but classes can have destructors.
❑ Structs cannot have explicit parameter less constructor where as a class can.
❑ Struct can’t inherit from another class where as a class can, Both structs an classes can inherit form an interface.
❑ Examples of structs in the. NET Framework-int(System.int32), double(System .Double) etc

Note1: A class or a struct cannot inherit from another struct. Struct are sealed types.
Note2: How do you prevent a class from being inherited? Or what is the significance of sealed keyword?


Follow Us

YouTube

 If you want to download pptx : click Telegram

Facebook

Twitter

Instagram

Tik Tok

إرسال تعليق

أحدث أقدم