In .NET are two categories of types: reference typesand value types.
- Struct is a value types and classe is a reference types.
- A class is a reference type and its object is created on the heap. - A structure is a value type and its object is created on the stack. |
- A class variable can be assigned null, a struct variable cannot assign null. - When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference. - A class is defined by using the class keyword, a structure is defined by using the struct keyword.
For example, public class StudentReport{…}
For example, public struct StudentReport{…}
- We cannot have instance Field initializers in structs, but we can have instance Field initializers in classes.
Example:
class TestClass { int myNumber =25; // no error. public void MyMethod( ) { // code } } struct MyStruct { int myVar = 15; // error. public void MyMethod ( ) { // code } }
- To instantiate classes we must use the new operator, in struct can not be.
For example, StudentReport StudRepo = new StudentReport(stud_enroll, stud_name, stud_report);
For example, StudentReport StudRepo;
- Classes support inheritance, for structs is no inheritance.
- A class can be inherited from a class or struct. - A class is permitted to declare a destructor, a struct is not. - We use classes for complex and large set of data, structs are simple to use.
for more information Visit :
|