HI WELCOME TO SIRIS

C# Interview Questions by topic part 1

Leave a Comment

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

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

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");
   }

}

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. .EXE stands for executable, and .DLL stands for Dynamic Link Library

Unit Testing a private static method in C# .NET


In the previous article we have seen how to unit test private instance methods. In this article we will see unit testing static private methods

In general, to unit test a static public method, we invoke the respective method using the
class name in our test method. We then simply check for the expected and actual output. However, when it comes to unit testing a static private method, we cannot do the same, as the private members are not available outside the class. To make the process of unit testing static private members easier, microsoft unit testing framework has provided PrivateTypeclass.

In the example below, CalculatePower() is a private static method. The purpose of this method is to calculate the value, when a given number is raised to a certain power. For example 2 to the power of 3 should return 8 and 3 to the power of 2 sholuld return 9. So to unit test this method we create the instance of PrivateType class. To the constructor of thePrivateType class we pass the type of the class that contains the private static method that we want to unit test. We do this by using the typeof keyword. The PrivateType instance can then be used to invoke the private static method that is contained with in the Maths class. The Maths class that contains the static private CalculatePower() method and the unit test are shown below.

public class Maths
{
   private static int CalculatePower(int Base, int Exponent)
   {
      int Product = 1;

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

         Product = Product * Base; 
      } 
      return Product; 
   } 


[TestMethod()]
public void CalculatePowerTest()
{
   PrivateType privateTypeObject = new PrivateType(typeof(Maths));
   object obj = privateTypeObject.InvokeStatic("CalculatePower", 2, 3);

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

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. 

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); 
}

Why C# does not support multiple class inheritance

Why C# does not support multiple class inheritance?
or
What are the problems of multiple class inheritance?

C# does not support multiple class inheritance because of the diamond problem that is associated, with multiple class inheritance. Let us understand the diamond problem of multiple class inheritance with an example.

As shown in the image above:
1. I have 2 classes - ClassB and ClassC
2. Both of these classes inherit from ClassA
3. Now, we have another class, ClassD which inherits from both ClassB and ClassC

So, if a method in ClassD calls a method defined in ClassA and ClassD has not overriden the invoked method. But both ClassB and ClassC have overridden the same method differently. Now, the ambiguity is, from which class does, ClassD inherit the invoked method: ClassB, or ClassC?

In order not to have these problems, C# does not support multiple class inheritance.

What are the difference between interfaces and abstract classes

There are several differences between an abstract class and an interface as listed below.

1. Abstract classes can have implementations for some of its members, but the interface can't have implementation for any of its members.

2. Interfaces cannot have fields where as an abstract class can have fields.

3. An interface can inherit from another interface only and cannot inherit from an abstract class, where as an abstract class can inherit from another abstract class or another interface.

4. A class can inherit from multiple interfaces at the same time, where as a class cannot inherit from multiple classes at the same time.

5. Abstract class members can have access modifiers where as interface members cannot have access modifiers.

Another common C# Interview Question, that is commonly asked is, When do you choose interface over an abstract class or vice versa?
A general rule of thumb is, If you have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. So, when you have an interface, you can move your implementation to any class that implements the interface. Where as, when you have an abstract class, you can share implementation for all derived classes in one central place, and avoid code duplication in derived classes.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.