Some hints to help you during C# interview
Reference Vs Value types
As for many the idea
of reference type is stored on the heap and the value type is on the stack is
completely wrong otherwise will be called Heap types and stack types.
·
Mainly the difference is
about copying. The Assignment of a reference type variable copies a
reference to the object but not the
object itself. This differs from the
variables that are based on value types which contains directly the values.
·
From Eric Lippert Article :
"in the Microsoft implementation of C# on the desktop
CLR, value types are stored on the stack when the value is a local variable or
temporary that is not a closed-over local variable of a lambda or anonymous method,
and the method body is not an iterator block, and the jitter chooses to not
enregister the value."
- The choice of allocation mechanism has to do only with the known required lifetime of the storage.
Deep vs Shallow copy
Shallow
copy
- Field is a value type a bit-by-bit copy of the field is performed
- Field is a reference type the reference is copied but the referred object is not therefore the original object and its clone refer to the same object.
The method used by default to make a
shallow copy is Object.MemberwiseClone ().
Deep copy
- Creates a new object and then copying the nonstatic fields (value type or reference type) of the current object to the new object.
What is an interface?
Interface is contract defined as an abstract type that contains no data or code but defines behaviors as method signatures.
Interface is contract defined as an abstract type that contains no data or code but defines behaviors as method signatures.
Abstract Vs Interface
The concept of Interfaces and abstract classes is a bit
confusing for beginners
- When you create an interface you are creating a definition that can never change after the interface definition has been released
- Abstract class can be useful when you anticipate creating multiple versions of your component
- An interface by contrast is a totally abstract (this means no implementation for members ) set of members that can be thought of defining contract for conduct
- IS-A vs CAN-DO relationship: to use an abstract class as a base class the derived class should satisfy the IS-A relationship. Interface imply a CAN-DO relationship.
Understanding virtual, override and new keyword in C#:
·
The virtual keyword is used
to modify a method, property, indexer, or event declared in the base class and
allow it to be overridden in the derived class.
·
The override keyword is
used to extend or modify a virtual/abstract method, property, indexer, or event
of base class into derived class.
·
The new keyword is used to
hide a method, property, indexer, or event of base class into derived class.
Abstract functions vs Virtual functions
·
Abstract Function:
1.
It can be declared only
inside abstract class.
2.
It contains only method
definition not the implementation.
3.
It must be overridden.
·
Virtual Function:
4. It can be declared inside abstract as well as non-abstract class.
5. It contains method implementation.
6. It may be overridden.
Static
- · Static variable are special area inside Heap called High Frequency Heap
- So using static keyword will make your code a bit faster since no object creation is involved.
- Static class are sealed by default this references can never be garbage collected (unless you null the field).
- Static variables are not thread safe.
Generic Types
Generic types offers more
advantages over the normal System.Collections such as Array List.
Some of advantages are:
Some of advantages are:
- Performance: Generic types are strongly typed which means no needs for boxing and unboxing (Array List vs List<T>).
- Binary Code Reuse: algorithms are written without duplicating type specific code.
- Compile-time type checking: the correct data type it is enforced at compile time
Reflection
- From MSDN
Reflection provides objects
(of type Type) that describe assemblies, modules and types. You can use
reflection to dynamically create an instance of a type, bind the type to an
existing object, or get the type from an existing object and invoke its methods
or access its fields and properties
References