What is the difference between a Struct and a Class

Answer Posted / kirti

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.

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. 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.

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

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.

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

How big is the datatype int in .NET ?

32 bits.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How you can implement custom validation in asp.net MVC?

794


How to Redirect Tracing to a File

786


Explain the advantages of asp.net mvc over asp.net?

832


Is .net framework 4.8 the last version?

752


What are Model Binders in ASP.Net MVC?

873


Explain the .net framework.

728


mention what are the various methods provided by the dataset object to generate xml?

804


How can you return string result from Action in ASP.Net MVC?

767


What is entity client data provider? : Entity framework

756


Mention the core components of .net framework?

758


What is difference between viewbag and viewdata and tempdata?

756


what is .edmx file and what it contains?

790


List the new features added in .net framework 4.0.

708


What is the advantage of mvc?

803


Explain unit test done by tester on development team?

736