Showing posts with label Interview Questions on C#. Show all posts
Showing posts with label Interview Questions on C#. Show all posts

Sunday, 18 May 2014

User control execution order?


Step 1: A Master page Int and PreLoad.
Step 2: An ASPX web form page Int,PreLoad and Load.
Step 3: A master page Load
Step 4: User control inside the page Int, preLoad, Load.
Step 5: Button to fire some code in a btnOK_Click event.

Thursday, 15 May 2014

What is AutoEventWireup?

The ASP.NET page framework also supports an automatic way to associate page events and methods. If the AutoEventWireup attribute of the Page directive is set to true (or if it is missing, since by default it is true), 

  • AutoEventWireup is an attribute in Page directive.  
  • AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired. 
  • AutoEventWireup will have a value true or false. By default it is true
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"Inherits="WebApplication2._Default" %>


the page framework calls page events automatically, specifically the Page_Init andPage_Load methods. In that case, no explicit Handles clause or delegate is needed.

Example 1

With AutoEventWireup="true"

Code

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Sri");
}

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("
Button Click"
);
}

When we click Button then both page load and button click event get fired.

OUTPUT

Sri
Button Click





Example 2

With  AutoEventWireup="false"

Now set the AutoEventWireup propoerty false

Again click the Button Event this time only click event get fired.

OUTPUT

Button Click

Wednesday, 14 May 2014

stringbuilder vs string in c#

String

String is immutable. Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create new instance in memory to hold the new value.

Example

string str = "hi";
// create a new string instance instead of changing the old one
str += "test";
str += "help";



String Builder

String builder is mutable it means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.

Example

StringBuilder sb = new StringBuilder("");
sb.Append("hi");
sb.Append("test ");
string str = sb.ToString();

Differences between Hashtable and Dictionary

Dictionary:
  • It returns error if we try to find a key which does not exist.
  • It is faster than a Hashtable because there is no boxing and unboxing.
  • Only public static members are thread safe.
  • Dictionary is a generic type which means we can use it with any data type.

Example:

    Dictionary dictionary = new Dictionary();
    dictionary.Add("cat", 2);
    dictionary.Add("dog", 1);
    dictionary.Add("llama", 0);
    dictionary.Add("iguana", -1);

    //dictionary.Add(1, -2); // Compilation Error

    foreach (KeyValuePair pair in dictionary)
    {
        lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
    }
Hashtable:

  • It returns null if we try to find a key which does not exist.
  • It is slower than dictionary because it requires boxing and unboxing.
  • All the members in a Hashtable are thread safe,
  • Hashtable is not a generic type,

Example:

    Hashtable objHashTable = new Hashtable();
    objHashTable.Add(1, 100);    // int
    objHashTable.Add(2.99, 200); // float
    objHashTable.Add('A', 300);  // char
    objHashTable.Add("4", 400);  // string

    lblDisplay1.Text = objHashTable[1].ToString();
    lblDisplay2.Text = objHashTable[2.99].ToString();
    lblDisplay3.Text = objHashTable['A'].ToString();
    lblDisplay4.Text = objHashTable["4"].ToString();

Monday, 12 May 2014

Which exception will catch first and what about finally statement?





try
{
    int b = 0;
    int c = 10 / b;
}
catch (Exception e)
{
}
catch (DivideByZeroException ae)
{
}
finally
{
}


It is throw compile time error

Exception Message:
Error 1 A previous catch clause already catches all exceptions of this or of a super type ('System.Exception')


Saturday, 10 May 2014

Differece between ds.clone and ds.copy?

The Clone method of the DataSet class copies only the schema of a DataSet object. It returns a new DataSet object that has the same schema as the existing DataSet object, including all DataTable schemas, relations, and constraints. It does not copy any data from the existing DataSet object into the new DataSet. 

The Copy method of the DataSet class copies both the structure and data of a DataSet object. It returns a new DataSet object having the same structure (including all DataTable schemas, relations, and constraints) and data as the existing DataSet object.

private DataSet CreateClone(DataSet myDataSet, string myTable, string myCol, decimal myValue) {
DataSet myCloneDS;

myCloneDS = myDataSet.Clone();
DataRow[] copyRows = myDataSet.Tables[myTable].Select(myCol + " = " + myValue); DataTable custTable = myCloneDS.Tables[myTable]; //Insert into all filtered row data into the cloned Dataset foreach (DataRow copyRow in copyRows) custTable.ImportRow(copyRow); return myCloneDS; }