July 25, 2015

LINQ Tool

LINQPad is great tool to work on LINQ queries. It is free available on https://www.linqpad.net/.
You can connect with your database and execute your queries in LINQPad and then paste working code in your solution.
There are some free learning lessons available so you can get basics of LINQ.

image

April 24, 2015

C# Oops constructor related question

What will be the output of below program

    class ConstructorConcept
    {
       
static void Main()
        {
           
ExtendedSample objExtendedSample = new ExtendedSample("Oops is very interesting");
           
Console.ReadKey();
        }

       
public class Sample
        {
         
           
public Sample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => Sample"); 
            }
        }

       
public class  ExtendedSample:Sample
        {
           
public ExtendedSample()
            {
               
Console.WriteLine("I am in zero argument constructor => ExtendedSample"); 
            }

           
public ExtendedSample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => ExtendedSample"); 
            }
        }
    }

This program will not compile because in base class we don’t have zero argument constructor.

April 23, 2015

C# Oops constructor related question part 2

I have modified base class. Now we have not defined any constructor inside base class.
Is this program will compile?

    class ConstructorConcept
    {
       
static void Main()
        {
           
ExtendedSample objExtendedSample = new ExtendedSample("Oops is very interesting");
           
Console.ReadKey();
        }

       
public class Sample
        {
         
           
public Sample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => Sample"); 
            }
        }

       
public class  ExtendedSample:Sample
        {
           
public ExtendedSample()
            {
               
Console.WriteLine("I am in zero argument constructor => ExtendedSample"); 
            }

           
public ExtendedSample(string Val)
            {
               
Console.WriteLine("I am in one argument constructor => ExtendedSample"); 
            }
        }
    }

Yes this program will compile because we have default constructor in base class.

April 22, 2015

C# Oops constructor related question 3

What should be the output of this program?

public class Example
        {
           
public static Example()
            {

            }
           
public static void Show()
            {
               
Console.WriteLine("Oops ");
            }
        }

       
static void Main()
        {
           
Example obj = new Example();
        }


This program will not compile because we have used access specifier with static constructor.

April 21, 2015

Try catch in SQL Server

What will be the output of below script?

BEGIN TRY
       SELECT 1/0
       SELECT 'Try Catch Test'
END TRY
BEGIN CATCH
              SELECT ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH


After exception it will go to catch block.
And Output:

image

April 15, 2015

Exception in catch block

Consider below program if there will be exception inside catch block then what will be the output of this program?

static void Main()
        {
           
int A = 0;
           
int Y = 5;
           
int X = 0;

           
try
            {
                A = 0;
                Y = 5;
                X = Y / A;
            }

           
catch (Exception ex)
            {
                X = Y / A;
            }

           
finally
            {
               
Console.WriteLine("I am in finally block");
               
Console.ReadKey();
            }
        }


This program will generate run time exception and it will get terminated.