Tuesday 21 February 2017

C# basic interview questions Part-4

1. Ques: What is indexer ?
Answer:
Indexer Concept is object act as an array.When you define an indexer for a class, this class behaves similar to a virtual array. You can then access the instance of this class using the array.
  • An indexer is also similar to a property.
  • Indexer an object to be indexed in the same way as an array.
  • Indexer modifier can be private, public, protected or internal.
  • The return type can be any valid C# types.
  • Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.
public dataType this[int index]
{
    get
    {
         return aValue;
    }
    set
    {
    }
}

2. Ques: What is the difference between string and StringBuilder in c#?
Answer:
String:-
  • It’s an immutable. means once we create its object of string and if we modify it will create new instances.
  • Any action it will create new instance every time.
  • We don’t have any append keyword in string.
  • During string concatenation, additional memory will be allocated.
  • It is under System namespace.
  • Performance wise string is slow.
StringBuilder:-
  • StringBuilder is mutable.
  • StringBuilder will use same instance of object to perform any action
  • we can use append keyword along with StringBuilder.
  • During string concatenation, additional memory will be allocated if and only if the string buffer’s capacity is reached.
  • Stringbuilder belongs to System.Text namespace
  • StringBuilder gives better performance while performing repeated operation over a string. 

3. Ques: What are the differences between IEnumerable and IQueryable?
Answer:
IEnumerable:-
  • IEnumerable exists in the System.Collections namespace.
  • IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.
  • IEnumerable doesn’t move between items, it is forward only collection.
IQueryable:-
  • IQueryable exists in the System.Linq Namespace.
  • IQueryable is suitable for querying data from out-memory (like remote database, service) collections.
  • If we need to do any manipulation with the collection like Dataset and other data sources, use IQueryable. 

4. Ques:  What is difference between is and as operator in C# ?
Answer:
"is" operator to check the object type. If the two objects are of the same type, it returns true and false if not.
"as" operator to check the object type. If the two objects are of the same type, it returns object else it returns null.


5. Ques: What is Automapper ?
Answer:
AutoMapper is an object-to-object mapper, which allows you to solve issues with mapping of the same properties in one object of one type to another object of another type.


6. Ques: How do install and uninstall assembly in GAC ?
Answer:
install- gacutil -i <assembly name>
uninstall-gacutil -u <assembly name>


7. Ques: What is Just In Time Compiler in dot net framework ?
Answer:
It is a compiler which converts MS IL (Microsoft Intermediate Language) code to Native Code (i.e. CPU-specific code that runs on the same computer architecture). Just-In-Time compiler- it converts the language that you write in .Net into machine language that a computer can understand.

There are 3 types of JITs
  • Pre-JIT compiler (Compiles entire code into native code completely in a single Operation)
  • Econo JIT compiler (Compiles only methods(Code) called at Runtime)
  • Normal JIT compiler (Compiles only that part of code called at Runtime and places in cache)

8. Ques: What are the advantages and disadvantages of get and set properties in C#?
Answer:
Advantages of properties (get/set):
  • Ability to add code later on without recompiling assemblies that reference this.
  • Ability to provide private/protected/internal set and public get (or any other combination).
  • Public fields are not CLS compliant.
Disadvantages of properties (get/set):
  • Slower to access (both read and write).
  • Can't pass as ref arguments to methods.

9. Ques: Do events have return type ?
Answer:
No, events do not have return type. 


10. Ques: Can event’s have access modifiers ?
Answer:
Event’s are always public as they are meant to serve every one registering to it. But you can access modifiers in events.You can have events with protected keyword which will be accessible only to inherited classes.You can have private events only for object in that class. 

No comments:

Post a Comment

Thank you for comment