General Questions
1.Does C# support multiple-inheritance?
No. But you can use Interfaces.
2.Where is a protected class-level variable available?
It is available to any sub-class derived from base class
3.Are private class-level variables inherited?
Yes, but they are not accessible.
4.Describe the accessibility modifier “protected internal”.
It is available to classes that are within the same assembly and derived from the specified base class.
6.Which class is at the top of .NET class hierarchy?
System.Object.
7.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.
8.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.
9.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.
10.Can you store multiple data types in System.Array?
No.
11.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.
12.How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
13.What’s the .NET collection class that allows an element to be accessed using a unique key?
HashTable.
14.What class is underneath the SortedList class?
A sorted HashTable.
15.Will the finally block get executed if an exception has not occurred?
Yes.
16.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 {}.
17.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 .
18.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).
Benefits of exceptions
Exceptions provide the ability to handle or clean up code in a localized place. Also, they allow clean-up code to execute in case of error. The application becomes easier to write and maintain when clean-up code is in a centralized place.
Also—and maybe more importantly—, exceptions can be used to find bugs within the code; because, the CLR walks up the stack of the thread to get the call history. With this, it is possible to find the location of the failure within the code. Further, it is possible to add additional information within the exception, so that a developer can describe the kind of error more precisely.
The biggest problem with exceptions is performance because of some information overhead. So, they should be used only where preemptive programming is inappropriate. Unfortunately, most developers tend to use exceptions improperly—i.e. catch (Exception e); which will be discussed later—or too seldom, so debugging proves harder.
Custom Exceptions in .NET
The .NET Framework provides a rich set of system-defined exception types that can be thrown and caught by the C# developer. However, the list of available exceptions does not cover every eventuality and often it is more appropriate to define custom exceptions for specific error scenarios. This can be achieved by deriving a new exception class from any of the existing types, usually the ApplicationException class.
In the final section of this article we will define a new exception class and demonstrate how it may be thrown and handled. This will be a simple exception with no custom methods or properties. These can be added to the derived class using standard object-oriented programming techniques. However, this is beyond the scope of the C# Fundamentals tutorial and will instead be described in a future object-oriented programming tutorial.
What is Exception.InnerException in .NET
When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.
Use the InnerException property to obtain the set of exceptions that led to the current exception.
You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.
Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine what causes the initial error.
The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.
Re-Throwing Exceptions
When an exception is caught in a catch block, the exception is considered to have been processed and code execution continues as normal. In some situations it is useful to catch the exception but still have it thrown to be caught again. For example, a method may catch all exceptions so that they can be logged and then re-throw the exception to be handled appropriately by the calling function.
When an exception is thrown explicitly using the syntax previously discussed, a new exception object is constructed. This exception object contains only the basic information set by the programmer; information such as the StackTrace property of the original exception is lost. To re-throw the exception and retain this additional information, the throw command is used without specifying an exception object. This is achieved using the following syntax:
The following example code demonstrates how this syntax can be used. For simplicity, the methods called are not defined so this code cannot be directly executed.
What is a good exception?
1. throwing an exception when an invalid parameter is passed to a method. The ArgumentException or one of its derived exception classes should be thrown in this situation.
2. throwing an exception when a call is made to a method that cannot operate because other information must be initialised or other methods must be called beforehand. An InvalidOperationException could be thrown in this case.