Thursday 15 December 2016

How to achieve method overloading in MVC? Or is it possible to achieve Method overloading in asp.net mvc?


Method overloading is possible in asp.net MVC.

This is general question which is asked in an interview.
Overloading or in other word we can say Polymorphism, it is the part of c# language. Http does not aware of this thing. Http works on URL concept and URL should be unique.

We can achieve overloading by the use of acceptverb attribute like HttpGet or HttpPost etc.
We can also use ActionMethod attribute above the method name in controller.

Example:-

public class HomeController : Controller
    {

        public ActionResult MethodName()
        {
            return Content("Method overloading with no parameter.");
        }

        [ActionName("TestMethod")]
        public ActionResult MethodName(string name)
        {
            return Content("Method overloading with with  parameter:" + name);
        }
    }

Note:-
If you didn’t put attribute ActionName above Method name then it will error at run time.

Sunday 11 December 2016

What is ISNUMERIC in SQL Server 2008R2?



Definition:-

ISNUMERIC () is a function to determines where an expression is valid numeric type or not.
ISNUMERIC return 1 when input expression is valid numeric data type, otherwise it return 0.

Syntax:-

ISNUMERIC (expression)

ReturnType:-

Int

Note:-
ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($).


Example:-



Select ISNUMERIC('123')

Select ISNUMERIC(123)

Output:

1

References taken from MSDN.

Friday 25 November 2016

Practical difference between constant and readonly in c#?


      
              Constant(const)


                    Readonly

Constant is immutable values and do not change their value for the life of program.
Constants are known as compile time.


ReadOnly is also immutable value and do not change their value for the life of program.
ReadOnly is known as runtime.
Assigned a value at the time of declaration.
Ex:-
public const int number = 0;

Assigned a value  either at run time or inside a constructor(instance initialization).

Ex:-
class Program
    {
        public readonly int number = 10;
        public Program()
        {
            number = 20;
        }
    }



Constant cannot be static i.e you cannot declared constant as static.

Ex:-
public static const int number = 10;

It will throw error at compile time.

Readonly can be static i.e you can declare readonly as static.

Ex:-
public static readonly int number = 10;

It will not give an error.
Constant value can access with class name.

Ex:-
class Program
    {
        public  const int number = 10;
       
        static void Main()
        {
            Console.Write(Program.number);
        }

    }



Readonly value can access by instance of class.

Ex:-
class Program
    {
        public  readonly int number = 10;
        
        static void Main()
        {
            Program p=new Program();
            Console.Write(p.number);
        }
    }

Tuesday 18 October 2016

What will happen if exception will occur in exception block in c #?



If exception will occur in try block then exception or error is cached by catch block. If final block is with try catch then after exception final block will execute.
In some scenario, if exception occurs in catch block, then code will abort in catch block because no catch block is there for catching an error or exception. So we check our code and will use nested try catch in application.

Example:-
static void Main(string[] args)
        {


            try
            {
                int a = 10;
                int b = 0;
                int c = a / b;
                Console.WriteLine("divide by zero");
            }
            catch(Exception ex) // exception will catch here
            {
                string path=@"D:\Test\";
                File.Copy(path + "xml.xml", path + "xml.xml"); // trying to copy a file but here path does not exist so again exception throw and then code will abort
                Console.WriteLine("");
            }
            finally
            {
                Console.WriteLine("Final");
            }
        }
In the above example, exception will occur in try block and it will catch by catch block. But again exception will occur in catch block because path does not exist. So after that code will abort here means execution will stop here. So final block will not execute.
So according to you requirement or you know that exception will again occur in catch block then you use nested try-catch block.