Showing posts with label .NET Interview Question. Show all posts
Showing posts with label .NET Interview Question. Show all posts

09 May 2013

Minimum threads in .NET Application

What is the bare minimum number of threads will be running in .NET application?. This is a .NET interview question.

To answer this question, first let's try to understand what all different thread will be there in any .net application or .NET process.

Below are the different threads could running in any .NET process or .NET application:

  • Main thread or UI thread.
  • Any worker threads created.
  • .NET garbage collection thread.

Worker threads will be there in the .NET process, depending on the logic you have created. Main/UI thread and .NET garbage collection thread will always be running in any .NET process or .NET application.

So, to answer the interview question, there will always be minimum of two thread will be running in .NET application.

17 April 2013

What is type safety in C# .net

When reading about the advantages of using generics, you will come to know you can write type-safe collections and these are the collections which avoid boxing and unboxing.

After reading such statements if you are getting questions on type safety, then you are in the right place to understand about type safety concept.

Let's try to find the answers for the below questions on type safety:

  1. What is type safety in .net?
  2. When does the type safety check happen and who ensures the type safety in .net?
  3. How type safety checks makes developer life easy?

Now let's find the answers for the type safety questions:

What is type safety in .net?

Type safety prevents assigning a type to another type when are not compatible.
public class Employee{}

public class Student{}
In the above example, Employee and Student are two incompatible types. We cannot assign an object of employee class to Student class variable. If you try doing so, you will get an error during the compilation process.

Cannot implicitly convert type 'Program.Employee' to 'Program.Student'.

As this type safety check happens at compile time it's called static type checking.

public class Employee {}
public class Engineer : Employee {}
public class Accountant : Employee {}

public static void Main(string[] args)
  {
   Accountant accountant = new Accountant();
   Engineer engineer = (Engineer)(accountant as Employee);
  }

In the above example, Engineer and Accountant class derives from the same Employee base class. When tried to type cast object of Accountant class to Engineer class variable, it throws System.InvalidCastException at runtime:

Unable to cast object of type 'Accountant' to type 'Engineer'.

Above type checking happens at runtime, hence it is called runtime type checking.

When does the type safety check happens and who ensures the type safety in .net?

As discussed type safety check happens both at Compile time and at runtime. The compiler will do the compile-time type safety check and CLR will do the runtime safety check.

How type safety checks makes developers life easy?

The advantages of type safety are pretty straightforward.

At compile time, we get an error when a type instance is being assigned to an incompatible type; hence preventing an error at runtime. So at compilation time itself, developers come to know such errors and code will be modified to correct the mistake. So developers get more confidence in their code.

Run time type safety ensures, we don't get strange memory exceptions and inconsistent behavior in the application.

You might be interested in my recent blog posts as well:

Events in C# .NET tutorial: What are events, when to use events, etc

Delegates in C# .NET tutorial: Delegates explained in detail

Generics delegates in c#

Dependency Injection explained

16 March 2013

Interview question on function overloading

Recently I heard an interesting question on function overloading. In this post let's see that question and find the answer.

In one of the previous post explained about what is function overloading. You can read the post to know more on function overloading.

The program showing function overloading interview question:

namespace CSharpInterviewQuestion
{
  class Program
  {
    static void Main(string[] args)
     {
       // Question 1
       Method(new object());

       // Question 2
       Method("Interview Question");

       // Question 3
       bject obj = GetText();
       Method(obj);

       // Question 4
       Method(null);
            
       Console.ReadLine();
    }

    public static object GetText() 
    {
     return "function overloading";
    }

    public static void Method(object obj)
        {
            if (obj == null){ Console.WriteLine("Object is null.");}
            else { Console.WriteLine(obj.GetType().Name); }
        }
        public static void Method(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                Console.WriteLine("String is null.");
            }
            else
            {
                Console.WriteLine(text));
            }
        }
    }
}

Answers for the function overloading interview questions:

  1. Question 1 answer: It's straight forward question. It prints "Object" to the console. In function overloading binding happens at compile time and hence the method which takes the object as parameter is invoked.

  2. Question 2 answer: It's another simple one. It prints "Interview Question" to the console. In function overloading binding happens at compile time and hence the method which takes the string as parameter is invoked.

  3. Question 3 answer: It prints "string" to the console and not "function overloading". At compile time obj is of type object and not string type.

  4. Question 4 answer: It's needs thinking. Both object type and string type variable can be null. When null is passed as parameter value, the more specific type, in this case string parameter method is bounded at the time of compilation. Hence it prints "String is null" to the console.

The actual output as seen in console output:

Object
Interview Question
String
String is null.

10 February 2013

What is explicit interface implementation

In a previous post mentioned about different types interface implementation and also discussed on implicit interface implementation.

In this post let's discuss about what is explicit interface implementation.

public interface Interface1
 {
   void Method(int parameter);
 }

public interface Interface2
 {
   void Method(int parameter);
 }

Two interfaces, Interface1 & Interface2. Both the interfaces have the same method name or method signature.Let's create a class which implements the two interfaces implicitly and see what happens.

public class ClassExample : Interface1, Interface2
{
 public void Method(int parameter)
   {
     Console.WriteLine("Am I implementation of 
              Interface1 or Interface2?"
);
   }
}

As you can see looking at the class which implements the two interfaces, it's not possible to tell whether Method() is of Interface1 implementation or it is of Interface2 implementation. We cannot distinguish them, as it is just one implementation. But if you have design requirement where a class has to provide different implementation of Method(), then implementing the interface implicitly will not work.

To address this problem, the implementing class has to name the interface method implementations explicitly. Let's see what does this mean.

public class ClassExample : Interface1, Interface2
 {
  public void Interface1.Method(int parameter)
  {
    Console.WriteLine("I'm the implementation of 
                     Interface1"
);
  }
  public void Interface2.Method(int parameter)
  {
    Console.WriteLine("I'm the implementation of 
                     Interface2"
);
  }
}

So the Methods names are prefixed with the interface names. Interface names are explicitly specified along with the interface implementation and hence the name explicit implementation.

Let's see an example.

public class Program
{
  static void Main(string[] args)
  {
    ClassExample instance = new ClassExample();
    // Cannot access Method() by using 
    // the ClassExample type variable.

    Interface1 interface1 = instance;

    // Interface1.Method() can be accessed by using 
    // the variable declared of Interface1 type
    interface1.Method(1);

    Interface2 interface2 = instance;

    // Interface2.Method() can be accessed by using 
    // the variable declared of Interface2 type
    interface2.Method(1);    
  }
}

That means, when a class implements interfaces explicitly, the variables declared of class type cannot access any of the interface methods. However, when class instances are typecasted to interface variables, the respective interface methods can be accessed.

Interface implementation types

Interface can be implemented in 2 ways.

  1. Implicit interface implementation

    In implicit interface implementation, interface name will not be mentioned with the method names while implementing them

  2. Explicit interface implementation

    Interfaces will be implemented explicitly by a class when it is implementing more than one interface having the same method signature.

Implicit interface implementation

In implicit interface implementation, interface name will not be mentioned with the method names while implementing them. Let's consider an example:

public interface InterfaceExample
 {
   void Method1(int parameter1, string parameter2);
 }
Now consider a class implementing the InterfaceExample interface.
public class ClassExample : InterfaceExample
{
 public void Method1(int parameter1, string parameter2)
  {
    Console.WriteLine("Implementation of Method1 
                      of InterfaceExample"
);
  }
}
Let's see the usage of the class implementing an interface in an implicit manner.
public class Program
{
  static void Main(string[] args)
  {
    ClassExample instance = new ClassExample();
    instance.Method1(2, "parameter");
    Console.ReadLine();
  }
}
Since the class implemented the interface implicitly, we invokde the Method1() using the variable of class type. Hence the below code is possible.
ClassExample instance = new ClassExample();
instance.Method1(2, "parameter");

30 September 2012

BigInteger Data Type introduced in .NET 4

I find one good article which explains about the BigInteger.

Some pointers:

int, int64 and long have size limitation. Assigning a large than they can hold will result in a OverFlowException. But if we have computation requirement which needs no such max limit, we can use the  BigInteger which introduced in .NET 4.

There is no max limit on the BigInteger data type. MSDN says like below:

Because the BigInteger type is immutable (see Mutability and the BigInteger Structure) and because it has no upper or lower bounds, an OutOfMemoryException can be thrown for any operation that causes a BigInteger value to grow too large.

More info can be find from here on MSDN