Monday, April 8, 2019

What are the new features introduced in c# 4.0?

This is very commonly asked c# interview question. This question is basically asked to check, if you are passionate about catching up with latest technological advancements. The list below shows a few of the new features introduced in c# 4.0. If you are aware of any other new features, please submit those using the from at the end of this post.

1. Optional and Named Parameters
2. COM Interoperability Enhancements
3. Covariance and Contravariance
4. Dynamic Type Introduction

What's the difference between IEnumerable and List ?

1. IEnumerable is an interface, where as List is one specific implementation of IEnumerable. List is a class.

2. FOR-EACH loop is the only possible way to iterate through a collection of IEnumerable, where as List can be iterated using several ways. List can also be indexed by an int index, element can be added to and removed from and have items inserted at a particular index.

3. IEnumerable doesn't allow random access, where as List does allow random access using integral index.

4. In general from a performance standpoint, iterating thru IEnumerable is much faster than iterating thru a List

Difference between AS and IS in C# ?


The Difference between IS and As is that..
IS - Is Operator is used to Check the Compatibility of an Object with a given Type and it returns the result as a Boolean (True Or False).
AS - As Operator is used for Casting of Object to a given Type or a Class.
Ex. Student s = obj as Student;
is equivalent to:
Student s = obj is Student ? (Student)obj : (Student)null;
Both 'is' and 'as' keywords are used for type casting in C#.

IS Operator

Is Operator is used to Check the Compatibility of an Object with a given Type and it returns the result as a Boolean i.e. True Or False.

if(obj is string)
{
     ...
}
In the above code the keyword checks whether the value on its left side is an instance of the type on the right side.

AS Operator

As Operator is used for Casting of Object to a given Type or a Class. The as keyword is used to cast nullable types if the specified value is not an instance of the specified type.

MyClass myObject = obj as MyClass;

Difference between call by ref and call by out ?

1) Call by ref should be assigned before using where as Call by Out not required to assign a value.


Key Differences Between Ref and Out in C#

  1. When a variable preceded by the ref keyword is passed to any method then the changes made to it inside the method reflects in its original value. When a variable passed to a method is preceded by out keyword the method returns it without using return keyword.
  2. The ref parameter should be initialized before it is passed to a method. On the other hand, the out parameter must be initialized inside the method it is passed to.
  3. While calling, declaring, or defining a method, ref parameter is explicitly declared as ref. On the other hand, while calling, declaring, or defining a method, out parameter is explicitly declared as out.