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

Explain linq to entities? : Entity framework

552


What are the two ways to add constraints to a route?

547


When will the .net framework 3.0 be released?

554


explain how you can load related entities in ef (entity framework)?

550


How does servicing work for the .net framework 3.0?

543






Explain how to use multiple submit buttons in ASP.Net MVC?

654


What is layout in mvc?

571


what is eager loading?

574


How to Redirect Tracing to a File

599


If I have multiple filters implemented, what is the order in which these filters get executed?

530


What is entitycontainer? : Entity framework

585


Is .net core faster than .net framework?

558


What are Validation Annotations?

582


what is more complex to implement property, methods or event? how can I define criteria to compare the difficulty of implementation between them? for example the number of methods wanted to implements property is 2 methods. how many methods I need it to implements events?

1697


Describe the .net framework architecture.

588