C# Interview Questions on value types and reference types

1) What are the different data types available in C#?
Ans)
1. Value Types
2. Reference Types

2) Is struct  value type or reference type?
Ans) Value Type

3) Is class   value type or reference type?
Ans) Reference type

4) Are Value types sealed?
Ans) Yes, Value types are sealed.


5) Can Value Types inherit from another class or struct?
Ans) No. 

6) Can Struct inherit from another interface?
Ans) Yes.

7) What is the base class from which all value types are derived?
Ans) System.ValueType

8) Give examples for value types?
Ans)
 Enum
Struct

9) Give examples for reference types?
Ans)
Class
Delegate
Array
Interface

10) What are the differences between value types and reference types?
Ans)
1. Value types are stored on the stack where as reference types are stored on the managed heap.
2. Value type variables directly contain their values where as reference variables holds only a reference to the location of the object that is created on the managed heap.
3. There is no heap allocation or garbage collection overhead for value-type variables. As reference types are stored on the managed heap, they have the over head of object allocation and garbage collection.
4. Value Types cannot inherit from another class or struct. Value types can only inherit from interfaces. Reference types can inherit from another class or interface. 

11) Can you create object of a struct?
Ans) Yes, we can create object of struct like how we create object for class.

12) Can Structs contain explicit parameterless constructors?
Ans) No, Structs cannot contain explicit parameterless constructors.


No comments:

Post a Comment