C# Interview Questions and Answers (958) - Page 7

Which keyword is used to prevent one class from being inherited by another class in C#

NOTE: This is objective type question, Please click question title for correct answer.
Can you store multiple datatypes in an Array?

NOTE: This is objective type question, Please click question title for correct answer.
An array is fixed length type that can store group of objects.

NOTE: This is objective type question, Please click question title for correct answer.
Which namespace is used to work with RegularExpression in C#?

NOTE: This is objective type question, Please click question title for correct answer.
Can we throw exception from catch block ?

Yes.
The exceptions which cant be handled in the defined catch block are thrown to its caller.
How to Find Number of Days for provided date range?

Code extract :

TimeSpan DayDifference = Convert.ToDateTime("2009-1-7").Subtract(Convert.ToDateTime("2009-1-1"));


To find the difference of the days, here we have used Subtract method. The Subtract method does not take start date into consideration. Hence, to get the exact number of days for the date range we need to add one more day to the result.

Response.Write("Number of Days : " + DayDifference.Days+1);
You have to make user to enter just integer values with TextBox. Can you do this with CompareValidator?

Yes. With the validator control, you need to set type attribute to the desired datatype which in this case is “Integer” and Operator to DataTypeCheck.

<asp:CompareValidator ID="CompareValidator1" ControlToValidate="TextBox1" Type="Integer" Operator="DataTypeCheck"

runat="server" ErrorMessage="CompareValidator"></asp:CompareValidator>

How can you cache Multiple Versions of a Page?

Using output cache you can cache multiple versions of the page.

We can either use,…
- @ OutputCache directive at design time declaratively for Declarative Approach
OR
- Response.Cacheclass at runtime for Programmatic Approach.

With @ OutputCache directive four parameters of VaryByParam , VaryByControl, VaryByHeader, VaryByCustom allows user to cache page depending on query string, control value, request's HTTP header, request's HTTP header respectively.
For an example:

To turn off caching,
Declarative Approach:
<%@ OutputCache Location="None" VaryByParam="None" %>

Programmatic Approach:
Response.Cache.SetCacheability(HttpCacheability.NoCache);


To cache the output for each HTTP request that arrives with a different ID:
Declarative Approach:
<%@ OutputCache duration="60" varybyparam=" ID" %>

Programmatic Approach:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.VaryByParams["ID"] = true;

Is it possible to notify application when item is removed from cache ?

Yes.
You can use CacheItemRemovedCallback delegate defining signature for event handler to call when item is removed from cache.

For an example:
HttpRuntime.Cache.Insert(

"CacheItem1", //insert cache item
"",
null,
Cache.NoAbsoluteExpiration,
new TimeSpan(0, 0, 15),
CacheItemPriority.Default,
new CacheItemRemovedCallback(RemovalMethod)); //define method which needs to be called when item is removed from cache


the method will be like,

private static string RemovedAt = string.Empty;
public static void RemovalMethod(String key, object value, //method is declared static so that it can be available when cache item is deleted
CacheItemRemovedReason removedReason)
{
RemovedAt = "Cache Item Removed at: " + DateTime.Now.ToString(); //Shows the time when cache item was removed
}

Can we declare an array variable with var keyword in C# 3.0?

No, var keyword is used to declare implicitly typed local variable. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

So you can declare a local variable of any type using var keyword but you can't declare an array of variable using var keyword.

Correct
 string[] str = { "ram", "sham" };

foreach (var s in str)
{

Response.Write(s + "<br />");
}



Wrong
var[] str = { "ram", "sham" };

foreach (var s in str)
{

Response.Write(s + "<br />");
}


This will throw "CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) " error.

For more info on var keyword see http://msdn.microsoft.com/en-us/library/bb383973.aspx

Thanks
Give Some Examples of Generic Classes?

List<T>,Queue<T>,Stack<T>,LinkedList<T>,HashSet<T>
A Queue is a collection where elements are processed in

NOTE: This is objective type question, Please click question title for correct answer.
Stack is collection where elements are processed in

NOTE: This is objective type question, Please click question title for correct answer.
The Items that is put in the Queue is Reads ?

NOTE: This is objective type question, Please click question title for correct answer.
Which method is used to add an Item to the end of the Queue?

NOTE: This is objective type question, Please click question title for correct answer.
Whcih method is used to add an Item on stack?

NOTE: This is objective type question, Please click question title for correct answer.
Which method is used to reads and removes an item from the head of the Queue.

NOTE: This is objective type question, Please click question title for correct answer.
Which method is used to return an item from the Top of the Stack?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories