Thursday, 30 November 2017

Interview Question and answer first set-1



1- Can you serialize hashtable and Why?

No, You can’t Serialize Hash table.Because, the .NET Framework does not allow serialization of any object that implements the IDictionary interface
2. What are indexers in C# .NET? What is the difference between property & indexers?

An indexer is a pair of get and set accessors, similar to those of properties.

  • Like a property, an indexer does not allocate memory for storage.
  • Both indexers and properties are used primarily for giving access to other data members with which they are associated and for which they provide get and set access.
    − A property usually represents a single data member.
    − An indexer usually represents multiple data members.
  • Like a property, an indexer can have either one or both of the accessors.
  • Indexers are always instance members; hence, an indexer cannot be declared static.
  • Like properties, the code implementing the get and set accessors does not have to be associated with any fields or properties. The code can do anything, or nothing, as long as the get accessor returns some value of the specified type.

3-What are the differences between Dispose and Finalize ?


DisposeFinalize
It is used to free unmanaged resources at any time.It can be used to free unmanaged resources held by an object before that object is destroyed.
It is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.It is called by Garbage Collector and cannot be called by user code.
It is implemented by implementing IDisposable interface Dispose() method.It is implemented with the help of Destructors
There is no performance costs associated with Dispose method.There is performance costs associated with Finalize method since it doesn’t clean the memory immediately and called by GC automatically.

4-  What is Object Pooling in C#?

Object Pool is a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.So it reduces the load of object creation.

5. What is the difference between == and .Equals method in c#?

int x = 20;
int y = 20;
Console.WriteLine( x == y);
Console.WriteLine(x.Equals(y));
Output:
True
True

For Reference Type:

== performs an identity comparison, i.e. it will only return true if both references point to the same object. While Equals() method is expected to perform a value comparison, i.e. it will return true if the references point to objects that are equivalent.
For Example:
StringBuilder s1 = new StringBuilder(“Yes”);
StringBuilder s2 = new StringBuilder(“Yes”);
Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
Output:
False
True
In above example, s1 and s2 are different objects hence “==” returns false, but they are equivalent hence “Equals()” method returns true. Remember there is an exception of this rule, i.e. when you use “==” operator with string class it compares value rather than identity.
When to use “==” operator and when to use “.Equals()” method?
For value comparison, with Value Type use “==” operator and use “Equals()” method while performing value comparison with Reference Type.

6. What all are the Advantages and Disadvantages of Generics in C#?

Advantages of Generics:

  • Generics provide type safety without the overhead of multiple implementations.
  • Generics eliminates boxing and unboxing.
  • There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.
  • By providing strong typing, a class built from a generic lets visual studio provide IntelliSense.
  • Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types
  • Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.

Disdvantages of Generics:

  • Generic types can be derived from most base classes, such as MarshalByRefObject (and constraints can be used to require that generic type parameters derive from base classes like MarshalByRefObject). However, the .NET Framework does not support context-bound generic types. A generic type can be derived from ContextBoundObject, but trying to create an instance of that type causes a TypeLoadException.
  • Enumerations cannot have generic type parameters.
  • Lightweight dynamic methods cannot be generic.
  • In C#, a nested type that is enclosed in a generic type cannot be instantiated unless types have been assigned to the type parameters of all enclosing types

  • 7. What is Reflection and what all are the common use of Reflection?

    Reflection is a process by which a program can examine and manipulate program objects at run time.
    Common use of Reflection:
    – Load assemblies at runtime
    – it allows you to learn what assembly defines a particular item such as a class or enumeration
    – List a class’s field,properties, constructors, event and methods
    – Get information about a property such as type and if it is read only
    – Get and Set property’s value
    – Get information about item’s attribute etc..


    No comments:

    Post a Comment