c#.net interview questions


What's the difference between IEnumerable<T> and List<T> ?
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.

Explicit Interface Implementation
This C# Interview Question was asked my a member of this blog. Please refer to the example code below.
How to implement the void Method() of the interface in the following case ?
class B

{
   public void Method()

   {
   // some code here
       //...

  }

}
interface I

{
   void Method();
}

class D : B, I
{
   // how to implement the void Method() of the interface

   // public void I.Method() { ... }
}

To implement void Method, we use explicit interface implementation technique as shown below.
using System;
namespace SampleConsole
{
class Program
{
   static void Main()
  {
      //To Call Class B Method
      D d = new D();
      d.Method();

      //To Call the Interface Method
      I i = new D();
      i.Method();
      //Another way to call Interface method
      ((I)d).Method();
   }

}
class B
{
   public void Method()
   {
      Console.WriteLine("Void Method - B");
   }

}
interface I
{
   void Method();
}
class D : B, I
{
   void I.Method()
   {
      Console.WriteLine("Void Method - I");
   }

}

}

Unit Test private method in C# .NET
This is a very common c# interview question. As a developer all of us know, how to unit test public members of a class. All you do is create an instance of the respective

class and invoke the methods using the created instance. So, unit testing public methods is very straight forward, but if the method that we want to unit test is a private method, then we cannot access it outside the class and hence cannot easily unit test it.
Consider the example class shown below. CalculatePower() method with in the Maths class is private and we want to unit test this method. Also, note that CalculatePower() is an instance private method. In another articel we will discuss the concept of unit testing a private static method. Microsoft's unit testing assembly contains a class called PrivateObject, which can be used to unit test private methods very easily. Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject is the fully qualified name. Click here, to read an articel on, unit testing a private static method with an example.

public class Maths

{
   private int CalculatePower(int Base, int Exponent)
   {

      int Product = 1;
      for (int i = 1; i <= Exponent; i++)

      {
         Product = Product * Base;

      }
      return Product;
  }
}
To unit test this method, We create an instance of the class and pass the created instance to the constructor of PrivateObject class. Then we use the instance of the PrivateObject class, to invoke the private method. The fully completed unit test is shown below.

[TestMethod()]
public void CalculatePowerTest()

{
   Maths mathsclassObject = new Maths();

   PrivateObject privateObject = new PrivateObject(mathsclassObject);
   object obj = privateObject.Invoke("CalculatePower", 2, 3); 

   Assert.AreEqual(8, (int)obj);
}

Difference between EXE and DLL
1. .EXE is an executable file and can run by itself as an application, where as .DLL is usullay consumed by a .EXE or by another .DLL and we cannot run or execute .DLL directly.

2. For example, In .NET, compiling a Console Application or a Windows Application generates .EXE, where as compiling a Class Library Project or an ASP.NET web application generates .DLL. In .NET framework, both .EXE and .DLL are called as assemblies.
3. .DLL's can be reused, where as .EXE's cannot be reused.

4. .EXE stands for executable, and .DLL stands for Dynamic Link Library

No comments:

Post a Comment