MVC interview questions on razor

1) What is Razor in MVC?
Ans) It’s a light weight view engine. Till MVC we had only one view type, i.e., ASPX. Razor was introduced in MVC 3.

2) Why Razor when we already have ASPX?
Ans) Razor View engine is a part of new rendering framework for ASP.NET web pages. Razor is clean, lightweight, and syntax's are easy as compared to ASPX.
 For example, in ASPX to display simple time, we need to write:

<%=DateTime.Now%>  (Asp.net rendering engine uses opening and closing brackets to denote code (<% %>))

In Razor, it’s just one line of code:

@DateTime.Now 

3) Which is a better fit, Razor or ASPX?
Ans) As per Microsoft, Razor is more preferred because it’s light weight and has simple syntax's.


 

C# Interview Questions on Methods / Functions

1) Is the following code compile?
using System;
namespace MethodOverloadingDemo
{
 class AddProgram
 {
  public static void Main()
  {
  }
  public void Sum(int FN, int SN)
  {
   int Result = FN+ SN;
  }
  public int Sum(int FN, int SN)
  {
   int Result = FN+ SN;
  }
 }
}
Ans)  No, The above code does not compile. You cannot overload a method based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the signature of the method for the purposes of method overloading. 


2) What is the difference between method parameters and method arguments. Give an example?
namespace MethodOverloadingDemo
{
 class AddProgram
 {
  public static void Main()
  {
   int FN = 10;
   int SN = 20;
   //FN and LN are method arguments
   int Total = Sum(FN, SN);
   Console.WriteLine(Total);
  }
  //FirstNumber and SecondNumber are method parameters
  public static int Sum(int FirstNumber, int SecondNumber)
  {
   int Result = FirstNumber + SecondNumber;
   return Result;
  }
 }
}

In the example above FirstNumber and SecondNumber are method parameters where as FN and LN are method arguments. The method definition specifies the names and types of any parameters that are required. When calling code calls the method, it provides concrete values called arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method.

3) Explain the difference between passing parameters by value and passing parameters by reference with an example?


We can pass parameters to a method by value or by reference. By default all value types are passed by value where as all reference types are passed by reference. By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method.An example is shown below.


Output : 0

Output : 101


4) Can you pass value types by reference to a method?
Ans) Yes, we can pass value types by by reference to a method. An example is shown below.
using System;
namespace MethodDemo
{
 class Program
 {
  public static void Main()
  {
   int I = 10;
   Console.WriteLine("Value of I before passing to the method = " + I);
   Function(ref I);
   Console.WriteLine("Value of I after passing to the method by reference= " + I);
  }
  public static void Function(ref int Number)
  {
   Number = Number + 5;
  }
 }
}
5) If a method's return type is void, can you use a return keyword in the method?
Ans) Yes, Even though a method's return type is void, you can use the return keyword to stop the execution of the method as shown in the example below.
using System;
namespace MethodDemo
{
 class Program
 {
  public static void Main()
  {
   HelloMethod();
  }
  public static void HelloMethod()
  {
   Console.WriteLine("Hello method called");
   return;
   Console.WriteLine("This statement will never be executed");
  }
 }
}
6) What is the syntax of Method?
Ans) 
access-modifiers return-type method-name (parameters) 
{
Method Body
}
7) What are static methods?
Ans) When a method declaration includes a static modifier, that method is said to be a static method. Static method is invoked using the class name.

C# Interview Questions on Enumerations

1) Enums stands for ?
Ans) Enums stands for Enumerations 

2) What is underlying type for enum?
Ans) The default underlying type of an enum is int.
The default value for first element is ZERO and gets incremented by 1.

3) Is it posible to customize the underlying type and values?
Ans) Yes, It is possible to customize the underlying type and values.

4) Is Enums are value types?
Ans) Yes, Enums are value types.

5) Enum keyword (all small letters) is used to create enumerations, where as Enum class, contains static GetValues() and GetNames() methods which can be used to list Enum underlying type Values and Names.

6) What is the use of Enum?
Ans) If a program used set of integral numbers, better replacing them with enums, which makes the program more 
Readable
Maintainable

Example : 



























The above code is more readable. 

7) Write a code to get Enum values and names?
Ans) 


8) Can you write a code to customize enum type? 
Ans)
9) Can you write a code to customize Enum values?
Ans) 








C# Interview Questions on Delegates

1) What is delegate ?

Ans) Delegate is a type which  holds the method(s) reference in an object. It is also referred to as a type safe function pointer.
Using delegates we can pass methods as a parameters. To pass methods as a parameter to delegate, the signature of the method must match of the signature of the delegate.


2) What is the syntax of the delegate?

Ans) public delegate return_type delegate_name(parameters);

3) What are the advantages of delegate?

Ans)  1. Effective use of delegate improves the performance of application
          2. Used to call a method asynchronously

4) Can you check the following code compile?


Ans) No, the code does not compile. For the code to compile, the signature of MethodTest should match the signature of TestDelegate.