Wednesday 3 March 2010

FAQ's - C#


Does C# support multiple-inheritance?
No.

Who is a protected class-level variable available to?
It is available to any sub-class (a class inheriting this class).

Are private class-level variables inherited?
Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.

Describe the accessibility modifier “protected internal”?
It is available to classes that are within the same assembly and derived from the specified base class.

What’s the top .NET class that everything is derived from?
System.Object.

What is the difference between Int.Parse , Convert.ToInt() and Int.TryParse()?
Int32.Parse Method
Converts the string representation of a number to its 32-bit signed integer equivalent.
-When s is a null reference, it will throw ArgumentNullException.
-If s is other than integer value, it will throw FormatException.
-When s represents a number out of range, it will throw OverflowException.

Convert.ToInt32(string)
Converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method.
-When s is a null reference, it will return 0 rather than throw ArgumentNullException.
-If s is other than integer value, it will throw FormatException.
-When s represents a number out of range, it will throw OverflowException.

Int32.TryParse Method
Converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise.
-When s is a null reference, it will return 0.
-If s is other than an integer value, the out variable will have 0.
-When s represents a number out of range, the out variable will have 0.


What does the term immutable mean?
The data value may not be changed.
Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.

What’s the difference between System.String and System.Text.StringBuilder classes?
System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

What’s the advantage of using System.Text.StringBuilder over System.String?
StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.

Can you store multiple data types in System.Array?
No.

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identacle object.

How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

What’s the .NET collection class that allows an element to be accessed using a unique key?
HashTable.

What class is underneath the SortedList class?
A sorted HashTable.

Will the finally block get executed if an exception has not occurred?­
Yes.

What’s the C# syntax to catch any possible exception?
A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.

Can multiple catch blocks be executed for a single try statement?
No.
Once the proper catch block processed, control is transferred to the finally block (if there are any).

Explain the three services model commonly know as a three-tier application?
Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources).

What is the difference between int and Int32?
Int32 is a class of namespace System. Keyword "int" is the alias of the class. So there is no real difference.

What does mutable and immutable means?
Immutable objects (read-only) are objects whose value cannot be modified once it has been created. Methods that appear to modify an immutable object actually return a new object that contains the modification (the previous object remains in memory until garbage collected).
Mutable Objects are objects whose value can be modified.

What is the difference between String class and StringBuilder class? Which one is better?
String Class is immutable and StringBuilder class is mutable. If the object has many operations involved then StringBuilder class is a better option.

What are delegates?
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.

What is difference between delegates and Events?
A delegate is a reference to a method. A delegate is raw in its nature, but an event is hooked up with a control; it acts based on the behaviour of the control. An event is a delegate, but a delegate is not an event. Event = Delegate + Event handling logic.

What is a partial class?
A Partial class is a class that can be split into two or more classes. This means that a class can be physically separated into other parts of the class within the same namespace. All the parts must use the partial keyword. All the other classes should also have the same access modifier. At the compile time, all the partial classes will be treated as a single class. Let us list some advantages of having partial classes.
• Allows a clean separation of business logic layer and the user interface.
• The UI code can be hidden from the developer.
• Makes the debugging easier.

What is a Sealed Class? Where is it used?
A sealed class is a class for which there will be no derived class. The keyword sealed ensures no overriding of members.
In general all the API and framework classes are made to be sealed classes.

What are Generics?
Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types they store or use. Using Generics, the type safety check burden will be on the compiler rather than developer. Hence, the Developer needs to write the code explicitly for checking type safety.
Ex:
public class Generic
{
public T Field;
}
Generic g = new Generic();
g.Field = "Some Value";

Example shows a simple example of Generics. The generic class defined has a field of the type T. The type T is initialized to be string.


http://en.csharp-online.net/CSharp_FAQ:_Are_CSharp_constructors_inherited

http://blogs.crsw.com/mark/articles/252.aspx

http://www.devbistro.com/tech-interview-questions/.NET.jsp

http://blogs.crsw.com/mark/articles/254.aspx

http://www.interviewqsn.com/asp_net-2008-features.html

http://msdotnetsupport.blogspot.com/2007/11/22-new-features-of-visual-studio-2008.html



Sudhakar has given:



http://venkataspinterview.blogspot.in/2008/07/aspnet-interview-questions.html

http://www.aspdotnet-suresh.com/











1 comment: