What is the difference between a Struct and a Class



What is the difference between a Struct and a Class..

Answer / 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

More Dot Net Framework Interview Questions

What is viewbag?

0 Answers  


Speaking of Boolean data types, what's different between C# and C/C++ ?

1 Answers  


How we can call a javascript function on the change of a dropdown list in mvc?

0 Answers  


Explain the methods used to render the views in ASP.Net MVC?

0 Answers  


If Controller is not there in MVC Pattern what happens?

2 Answers  






what is ADO.NET

8 Answers   NIIT,


How many .NET languages can a single .NET DLL contain

1 Answers  


What is filters in web api?

0 Answers  


what is minimum requirement for entity framework applications to run?

0 Answers   Microsoft,


Describe the gac in the .net framework.

0 Answers  


What is entity framework? : Entity framework

0 Answers  


what is linq to entities?

0 Answers   Microsoft,


Categories