C# Interview Questions 3

By | May 13, 2014

What does \a character do? On most systems, produces a rather annoying beep.

Can you create enumerated data types in C#? Yes.

What’s different about switch statements in C#? No fall-throughs allowed.

What happens when you encounter a continue statement inside the for loop? The code for the rest of the loop is ignored, the control is transferred back to the beginning of the loop.

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

Will finally block get executed if the exception had not occurred? Yes.

What’s the C# equivalent of C++ catch (…), which was a catch-all statement for 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? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block.

Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.

What’s the difference between // comments, /* */ comments and /// comments? Single-line, multi-line and XML documentation comments.

How do you generate documentation from the C# file commented properly with a command-line compiler? Compile it with a /doc switch.

Can you change the value of a variable while debugging a C# application? Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.

What’s the implicit name of the parameter that gets passed into the class’ set method? Value, and it’s datatype depends on whatever variable we’re changing.

How do you inherit from a class in C#? Place a colon and then the name of the base class. Notice that it’s double colon in C++.

Does C# support multiple inheritance? No, use interfaces instead.

So how do you retrieve the customized properties of a .NET application from XML .config file? Can you automate this process? Initialize an instance of AppSettingsReader class. Call the GetValue method of AppSettingsReader class, passing in the name of the property and the type expected. Assign the result to the appropriate variable. In Visual Studio yes, use Dynamic Properties for automatic .config creation, storage and retrieval.

Why is it not a good idea to insert code into InitializeComponent method when working with Visual Studio? The designer will likely through it away, most of the code inside InitializeComponent is auto-generated.

Where do you add an event handler? It’s the Attributesproperty, the Add function inside that property. e.g. btnSubmit.Attributes.Add(“”onMouseOver””,””someClientCode();””)

What are jagged array? First lets us answer the question that what an array is? The dictionary meaning of array is an orderly arrangement or sequential arrangement of elements. In computer science term: An array is a data structure that contains a number of variables, which are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array. An array has a rank that determines the number of indices associated with each array element. The rank of an array is also referred to as the dimensions of the array. An array with a rank of one is called a single-dimensional array. An array with a rank greater than one is called a multi-dimensional array. Specific sized multidimensional arrays are often referred to as two-dimensional arrays, three-dimensional arrays, and so on. Now let us answer What are jagged arrays?  A jagged array is an array whose elements are arrays. The elements of jagged array can be of different dimensions and sizes. A jagged array is sometimes called as “array-of-arrays”. It is called jagged because each of its rows is of different size so the final or graphical representation is not a square.  When you create a jagged array you declare the number of rows in your array. Each row will hold an array that will be on any length. Before filling the values in the inner arrays you must declare them. Jagged array declaration in C#:  For e.g. : int [] [] myJaggedArray = new int [3][]; Declaration of inner arrays:    myJaggedArray[0] = new int[5] ;   // First inner array will be of length 5.  myJaggedArray[1] = new int[4] ;  // Second inner array will be of length 4.  myJaggedArray[2] = new int[3] ;   // Third inner array will be of length 3. Now to access third element of second row we write:   int value = myJaggedArray[1][2]; Note that while declaring the array the second dimension is not supplied because this you will declare later on in the code. Jagged array are created out of single dimensional arrays so be careful while using them. Don’t confuse it with multi-dimensional arrays because unlike them jagged arrays are not rectangular arrays. For more information on arrays: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfarrayspg.asp 

What is a delegate, why should you use it and how do you call it ? A delegate is a reference type that refers to a Shared method of a type or to an instance method of an object. Delegate is like a function pointer in C and C++.  Pointers are used to store the address of a thing. Delegate lets some other code call your function without needing to know where your function is actually located. All events in .NET actually use delegates in the background to wire up events. Events are really just a modified form of a delegate. It should give you an idea of some different areas in which delegates may be appropriate:

  • They enable callback functionality in multi-tier applications as demonstrated in the examples above. <o:p></o:p>
  • The CacheItemRemoveCallback delegate can be used in ASP.NET to keep cached information up to date. When the cached information is removed for any reason, the associated callback is exercised and could contain a reload of the cached information. <o:p></o:p>
  • Use delegates to facilitate asynchronous processing for methods that do not offer asynchronous behavior.
  • Events use delegates so clients can give the application events to call when the event is fired. Exposing custom events within your applications requires the use of delegates.

How does the XmlSerializer work? XmlSerializer in the .NET Framework is a great tool to convert Xml into runtime objects and vice versa

If you define integer variable and a object variable and a structure then how those will be plotted in memory.Integer , structure – System.ValueType  — Allocated memory on stack , infact integer is primitive type recognized and allocated memory by compiler itself .Infact , System.Int32 definition is as follows :[C#] [Serializable] public struct Int32 : IComparable, IFormattable, IConvertibleSo , it’s a struct by definition , which is the same case with various other value types .Object – Base class , that is by default reference type , so at runtime JIT compiler allocates memory on the “Heap” Data structure . Reference types are defined as class , derived directly or indirectly by System.ReferenceType

Leave a Reply