Search This Blog

Wednesday, October 10, 2018

Struct


  • Struct is an encapsulated entity. Struct doesn't uses complete oops concept but are used for user defined data type. All the member of the struct has to be initialized, as it is value type.
  •  A struct is a simple user-defined type, a lightweight alternative to a class.
  •  Similar to classes, structures have behaviors and attributes. As a value type, structures directly contain their value so their object or instance is stored on the stack.
  •  Struts support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties.
  •  Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs might be a good option. In the other words when we have small and frequent use of some work use structs over classes.
  •  Structs reside on the stack, so we should keep them small.
  •  The default constructor of a structure initializes each field to a default value. You cannot replace the default constructor of a structure.
  •  You can't define destructor for structs


E.G.

public struct Student { 
    int id; 
    int zipcode; 
    double salary; 
}

  
Q: Is Structure reference type?
A: No, it is value type

Q: Is Structure support default (parameter less) constructor?
A: No

Q: Is Structure support parameterized constructor?
A: Yes

Q: Is Structure support uninitialized valued (attributes)?
A: No, all values must be initialized within constructor. We cannot initialize value during declaration.

Q: Can Structure be inherited?
A: No, they are sealed.

Q: Can class be inheriting to Structure?
A: No. Struct can't derive from any class.

Q: Can Interface be inheriting to Structure?
A: Yes. Struct can be derive from interface only.

Q: Can Structure member be declared protected?
A: No, as Structure cannot inherited.


Structure VS Class

Structs
Class
Value Type
Reference Type
Stored in stack
Stored on managed heap
Doesn’t support inheritance with class
Support Inheritance
Cannot define default constructor
Constructor is fully supported
Cannot define default destructor
Destructor is fully supported
Uninitialized member not possible
Uninitialized member possible


No comments:

Post a Comment


This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.