Wednesday, October 1, 2008

C#.net interview questions

  1. My switch statement works differently than in C++! Why? - C# does not support an explicit fall through for case blocks. The following code is not legal and will not compile in C#:
    switch(x)
    {
    case 0: // do something
    case 1: // do something as continuation of case 0
    default: // do something in common with
    //0, 1 and everything else
    break;
    }

    To achieve the same effect in C#, the code must be modified as shown below (notice how the control flows are explicit):

    class Test
    {
    public static void Main() {
    int x = 3;
    switch(x)
    {
    case 0: // do something
    goto case 1;
    case 1: // do something in common with 0
    goto default;
    default: // do something in common with 0, 1, and anything else
    break;
    }
    }
    }
Is there an equivalent of exit() for quitting a C# .NET application? - Yes, you can use System.Environment.Exit(int exitCode) to exit the application or Application.Exit() if it’s a Windows Forms app

Is there a way to force garbage collection? - Yes. Set all references to null and then call System.GC.Collect(). If you need to have some objects destructed, and System.GC.Collect() doesn’t seem to be doing it for you, you can force finalizers to be run by setting all the references to the object to null and then calling System.GC.RunFinalizers().

  1. Is there a way of specifying which block or loop to break out of when working with nested loops? - The easiest way is to use goto:
    using System;
    class BreakExample
    {
    public static void Main(String[] args) {
    for(int i=0; i<3; i++)
    {
    Console.WriteLine("Pass {0}: ", i);
    for( int j=0 ; j<100 ; j++ )
    {
    if ( j == 10)
    goto done;
    Console.WriteLine("{0} ", j);
    }
    Console.WriteLine("This will not print");
    }
    done:
    Console.WriteLine("Loops complete.");
    }
    }
  2. Solution: using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    int i = 0;
    switch (i)
    {
    case 1:

    Console.WriteLine("Hi.This is 1 st block:");

    break;

    case 2:

    Console.WriteLine("Hi.This is 2 st block:");

    break;

    case 3:

    Console.WriteLine("Hi.This is 3 st block:");

    break;

    case 4:

    Console.WriteLine("Hi.This is 4th block:");

    break;

    default:

    Console.WriteLine("Hi.This is default block:");

    Console.ReadLine();

    break;
    }
    }
    }
    }
    What is mean "Death of Diamod"?
As every object is treated as a diamond in a Class we can say that destruction of a class object after it's functionality in order to recover the memory associated with this object is known as "Death of Diamond".

All the .NET languages have the following in common
?

Base class library.





No comments: