Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




What is the difference between a Struct and a Class

o The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

o When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

o It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.

o It is an error to initialize an instance field in a struct.

o There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

o A struct is a value type, while a class is a reference type.

Share this article   |    Print    |    Article read by 1878 times
Author:
Guest
Guest user is that user who have done some activity on website without login. Activity contains commenting on any article, uploading an image on website etc.
Related Articles:
Related Interview Questions: