Saturday 11 February 2017

C# basic interview questions Part-3

1. Ques: What is the difference between Var and Dynamics in C# ?
Answer:
var :
  • var keyword type variable is decided by the compiler at compile time. So it is strongly typed variable.
  • var type of variables are required to be initialized at the time of declaration or otherwise it will give compile time error: e.g., var str=”I am a var variable”; 
  • It throw an Errors at compile time.
  • var variables cannot be used for property or return values from a function. They can only be used as local variable in a function.
  • Introduced in C# 3.0
dynamic:
  • dynamic keyword type variable is decided by the compiler at run time.So it is dynamically typed variable.
  • In dynamic type variable No need to initialize at the time of declaration.e.g., dynamic strr;
  • It throw an Errors at run time.
  • dynamic variables can be used to create properties and return values from a function.
  • Introduced in C# 4.0.

2. Ques: What is tuples ? When we use Tuples in C#?
Answer:
  • Tuple is new in c# 4.0.
  • A tuple allows you to combine multiple values of different types into a single object without having to create a class.
  • Tuple provides us to group elements of diffrent data type together.
  • Tuple are reference types.
  • We can create a Tuple using the Create method. This method provide to 8 overloads, so we can use a maximum of 8 data types in it. 
ex: var tuplevar= new Tuple<string, string, int>("India","mumbai",400001);

var tuplevar= Tuple.create<string, string, int>("India","mumbai",400001);

to get list item from tuplee use as
geo.Item1; //state
geo.Item2; //city
geo.Item3;//pincode

Advantages 
Tuple returns multiple values from a method.
It provides an alternative to "ref" or "out" if method that needs to return multiple new objects.


3. Ques: What is the difference between string and String Builder in c#? 
Answer:
String:-
  • String is immutable. means once we create string object we cannot modify. Any operation like insert, replace or append happened .it will create new instance in memory to hold the new value.
  • String class come under System namespace.
  • It performance degrades when continuous change of value occurs
String builder
  • String builder is mutable it means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.
  • String builder class come under System namespace.Text.
  • It shows better performance because any new changes are made to existing instance.

4. Ques: Difference between Direct casting, is and as operator in C# ?
Answer:
Direct casting: Direct cast is most common way of casting one type to another, however it yields exception if casting can’t be done.

Is operator: Is operator checks whether an object is compatible with given type and if it is true (if casting can be done) or false (if casting can’t be done)

As operator: As operator is like a casting except that it yields null on conversion failure instead of raising an exception. So run time exception can be avoided using null check.


5. Ques: What are the differences between Const & Readonly ?
Answer:
Const :
  • Const can only be initialized at the time of declaration of the field.
  • Const values will evaluate at compile time only.
  • Const value can’t be changed these will be same at all the time.
  • Can't be static.
  • This type of fields are required when one of the field values remains constant throughout the system like Pi will remain same in your Maths Class.

Readonly :
  • The value will be initialized either declaration time or the constructor of the class allowing you to pass the value at run time.
  • Read only values will evaluate at runtime only.
  • Can be either instance-level or static.

6. Ques: How many types of collection in C#?
Answer:
There are the 2 types of collections:
1.Generic
2.Non-Generic


Generic
Non-generic  
   ArrayList    

         List
HashTable  
       Dictionary

SortedList  

       SortedList  
Stack           
        Stack

Queue        
       Queue














7. Ques: What are generics in C#.NET?
Answer:
Generics are the most powerful feature of C# 2.0.
Generic types to maximize code reuse, type safety, and performance.
They can be used to create collection classes.
Generic collection classes present in the System.Collections.Generic namespace.


8. Ques: What are the advantage generics in C#.NET?
Answer:
Generics is advantageous as:
- its provide type safety.
- its improved performance.
- its reduced the code.
- They promote the usage of parameterized types.
- it is type-safe data. there is no boxing & unboxing requred with Generic type.
- Code Re-usability provide by Generics.

9. Ques:  What is the difference between break and continue statement?
Answer:
The break statement is used to terminate the current enclosing loop or conditional statements in which it appears. We have already used the break statement to come out of switch statements.
The continue statement is used to alter the sequence of execution. Instead of coming out of the loop like the break statement did, the continue statement stops the current iteration and simply returns control back to the top of the loop.
        

No comments:

Post a Comment

Thank you for comment