When do you generally use a class over a struct?



When do you generally use a class over a struct?..

Answer / Ankit Srivastava

Use a class when:
1. You need to create an object with state that can change during the lifetime of the object.
2. The object needs to be subclassed or inherit from other classes.
3. The object requires methods in addition to properties.nUse a struct when:
1. The value is small and frequently used as a parameter for methods without needing to change its state.
2. It's more efficient memory-wise than using a class (due to stack allocation instead of heap allocation).

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

Distinguish between a class and struct?

1 Answers  


How do you serialize an object?

1 Answers  


Why would you use a class property in c#?

1 Answers  


What is visual c# net?

1 Answers  


What does readonly mean in c#?

1 Answers  


What is the difference between interface and functional interface?

1 Answers  


What is difference between write and writeline?

1 Answers  


Explain the difference between boxing and unboxing.

1 Answers   Accenture,


Describe the process of “exception handling implementation” in c#?

1 Answers  


Explain how many types of exception handlers are there in .net?

1 Answers  


Are there constructors in c sharp?

1 Answers  


What is the wildcard character in SQL? Let?s say you want to query database with LIKE for all employees whose name starts with La.

1 Answers  


Categories