Difference between array, arraylist, List, Hashtable, Dictionary and SortedList in c#

Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performance that a list, a list would be far more flexible since you don't need to know the required size initially. Array - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insertion, retrieve and speed. ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET List - one of my favorites - can be used with generics, so you can have a strongly typed array, e.g. List . Other than that, acts very much like ArrayList. Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs. Dictionary - same as above only strongly typed via generics, such a...

MVC Interview Question with Answer Part 2

What is difference between TempData and ViewData ?

“TempData” maintains data for the complete request while “ViewData” maintains data only from Controller to the view.
Does “TempData” preserve data in the next request also?
“TempData” is available through out for the current request and in the subsequent request it’s available depending on whether “TempData” is read or not.
So if “TempData” is once read it will not be available in the subsequent request.
What is the use of Keep and Peek in “TempData”?
Once “TempData” is read in the current request it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below.

@TempData[“MyData”];

TempData.Keep(“MyData”);

The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.

string str = TempData.Peek("Td").ToString();

What are partial views in MVC?

Partial view is a reusable view (like a user control) which can be embedded inside other view. For example let’s say all your pages of your site have a standard structure with left menu, header, and footer

How can we do validations in MVC?

One of the easiest ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties. For example, in the below code snippet we have a simple Customer class with a property customercode.
This CustomerCode property is tagged with a Required data annotation attribute. In other words if this model is not provided customer code, it will not accept it.

public class Customer
{
    [Required(ErrorMessage="Customer code is required")]
    public string CustomerCode
    {
        set;
        get;
    }

In order to display the validation error message we need to use the ValidateMessageFor method which belongs to the Html helper class.

 @using (Html.BeginForm("PostCustomer", "Home", FormMethod.Post))  
{
@Html.TextBoxFor(m => m.CustomerCode)
@Html.ValidationMessageFor(m => m.CustomerCode)
<input type="submit" value="Submit customer data" />
}

Later in the controller we can check if the model is proper or not by using the ModelState.IsValid property and accordingly we can take actions.

public ActionResult PostCustomer(Customer obj)
{
    if (ModelState.IsValid)
    {
        obj.Save();
        return View("Thanks");
    }
    else
    {
        return View("Customer");
    }
}

How can we enable data annotation validation on client side?

It’s a two-step process: first reference the necessary jQuery files.



The second step is to call the EnableClientValidation method.

@Html.EnableClientValidation()

What is Razor in MVC?

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

Why Razor when we already have ASPX?

Razor is clean, lightweight, and syntaxes are easy as compared to ASPX. For example, in ASPX to display simple time, we need to write:

<! =DateTime.Now >

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

@DateTime.Now

So which is a better fit, Razor or ASPX?
As per Microsoft, Razor is more preferred because it’s light weight and has simple syntaxes.

How can you do authentication and authorization in MVC?

You can use Windows or Forms authentication for MVC.

How to implement AJAX in MVC?

You can implement AJAX in two ways in MVC:
AJAX libraries
jQuery
Below is a simple sample of how to implement AJAX by using the “AJAX” helper library. In the below code you can see we have a simple form which is created by using the Ajax.BeginForm syntax. This form calls a controller action called getCustomer.
So now the submit action click will be an asynchronous AJAX call.

Comments

Popular posts from this blog

C# Interview Questions on Inheritance

C# Interview Questions on value types and reference types

Why to use UpdatePanel control in AJAX ASP.NET?