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

what is immutable and mutable?

Strings are immutable . What this means is application is that modifying a string creates new string data on the heap and modifies the pointer on the stack to point to the new location. The data on the heap that is not referenced from the stack will be cleared at garbage collection time, but until then it’s wasteful.

In comparison, StringBuilders are mutable . Modifications/additions to the stringbuilder will update the data in the heap instead of making copies of it each time.

(StringBuilder sb = new StringBuilder; sb.Add(“test”); sb.Add(” and test2?);

That code will create a pointer on the stack and a data value on the heap, update that data value to add the second string and that’s it. Doing the same with strings would have left an abandoned string data value on the heap of value “test” waiting on the GC.
Can we declare event and delegates in an interface ?

No,we cannot declare delegates in interface however we can declare events in interface.
So interface can only contains the signature of the following members
Methods
Properties
Indexers
Events
Hashtable is a part of which namespace in Dotnet Framework ?

NOTE: This is objective type question, Please click question title for correct answer.
How to get the default value of the C# variables?

To get the default value of the C# variables, default keyword can be used.

        float f = default(float);

Response.Write(f.ToString());

bool b = default(bool);
Response.Write(b.ToString());

DateTime d = default(DateTime);
Response.Write(d.ToString());


Thanks
Can you Explain the following code ? public class Myclass() { private Myclass() { } }

NOTE: This is objective type question, Please click question title for correct answer.
What is the[B] using [/B] keyword in c#?

It defines a scope, outside of which an object or objects will be disposed.
It is useful for an Object with lifetime that does not extend beyond the method in which objects are constructed.
An Interface is a Value type or reference type?

NOTE: This is objective type question, Please click question title for correct answer.
Equals method in C# is defined by which class ?

NOTE: This is objective type question, Please click question title for correct answer.
what is the default Access specifier of class

NOTE: This is objective type question, Please click question title for correct answer.
Can you prevent a class from overriding ?

Yes you can prevent a class from overriding if you define a class as "Sealed " then you can not inherit the class any further.
Difference between Abstract class and interface?

1.Through the abstract class we cannot achieve the multiple inheritance in c-sharp. while by interface we can.
2. We can declare the access modifier in abstract class but not in interface. Because by default everything in interface is public
Can we assign values to read only variables?if yes then how?

yes, we can assign values to read only variables either at a time of declaration or in constructors
What is the basic difference between "var" keyword in Javascript and C# ?

In C#, we have "dynamic" keyword. This dynamic keyword is somewhat similar to javascript's "var" . Here, both create a variable whose type will not be known until runtime, and whose misuse will not be discovered until runtime. C#'s var creates a variable whose type is known at compile time.

For example:-

dynamic dt= new DateTime();

dt.bar();

//compiles fine but gives an error at runtime


Thanks and Regards
Akiii
How can we trace our view state information?

We can trace View State of our application by placing this tag at Page Directive.

<%@ Page Language="C#" AutoEventWireUp="true" Trace="True"


Now if we run the page then we can see the details of the view state along with Control tree.
In C# how do you find the last error which occurred ?

This can be done by implementing a method called GetLastError(). Generally this returns a ASPError object stating the error condition that has occurred. This method will work only before the .ASP page sent any content to the Client.

Sample Code

Exception LastErrorOccured;
String ShowErrMessage;
LastErrorOccured = Server.GetLastError();
if (LastErrorOccured != null)
ShowErrMessage = LastErrorOccured.Message;
else
ShowErrMessage = "No Errors";
Response.Write("Last Error Occured = " + ShowErrMessage);

What is Smart Navigation ?

When ever there is a request from a web browser which is IE 5.0 or greater Smart Navigation enhances the user feel of the page.

These are the different functions which this smart navigation performs.

1) It Persists the element focus between navigation's.

2) It also persists the scroll position when user traversing from one page to another.

3) It eliminates the flash content which was caused by navigation.

4) Most important feature is retaining the last page state in the browsers history.

This is how we can set the Smart Navigation :

We can set SmartNavigation attribute at the @ page directive in the .aspx page. So when the page is requested the dynamically generated class sets this attribute.

Note : This property is best useful when a particular web site has frequent postbacks.
How do we check Array out of range error in C#

In C# we have IndexOutOfRange exception which is used to check the index out of range exception.

Example:
try

{
------
}
catch(IndexOutOfRangeException ex)
{
Console.WriteLine("An index was out of range!");
}
catch(Exception ex)
{
Console.WriteLine("Error occured: " + ex.Message);
}

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