Interview Tips Interview Tips, Interview Questions and Answers

20Nov/100

Working with Dictionary Collections

Another very useful generic collection is the Dictionary, which works with key/value pairs. There is a non-generic collection, called a Hashtable that does the same thing, except that it operates on type object. However, as explained earlier in this lesson, you want to avoid the non-generic collections and use thier generic counterparts instead. The scenario I'll use for this example is that you have a list of Customers that you need to work with. It would be natural to keep track of these Customers via their CustomerID. The Dictionary example will work with instances of the following Customer class:

public class Customer
{
public Customer(int id, string name)
{
ID = id;
Name = name;
}

private int m_id;

public int ID
{
get { return m_id; }
set { m_id = value; }
}

private string m_name;

public string Name
{
get { return m_name; }
set { m_name = value; }
}
}

The Customer class above has a constructor to make it easier to initialize. It also exposes it's state via public properties. It isn't very sophisticated at this point, but that's okay because its only purpose is to help you learn how to use a Dictionary collection. The following example populates a Dictionary collection with Customer objects and then shows you how to extract entries from the Dictionary:

Dictionary customers = new Dictionary();

Customer cust1 = new Customer(1, "Cust 1");
Customer cust2 = new Customer(2, "Cust 2");
Customer cust3 = new Customer(3, "Cust 3");

customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust2);
customers.Add(cust3.ID, cust3);

foreach (KeyValuePair custKeyVal in customers)
{
Console.WriteLine(
"Customer ID: {0}, Name: {1}",
custKeyVal.Key,
custKeyVal.Value.Name);
}

11Nov/100

Applying Generics

Because of the native support for generics in the IL and the CLR, most CLR-compliant language can take advantage of generic types. For example, here is some Visual Basic .NET code that uses the generic stack of Code block 2:

Dim stack As Stack(Of Integer)
stack = new Stack(Of Integer)
stack.Push(3)
Dim number As Integer
number = stack.Pop()

You can use generics in classes and in structs. Here is a useful generic point struct:

public struct Point
{

public T X;

public T Y;
}

You can use the generic point for integer coordinates, for example:

Point point;
point.X = 1;
point.Y = 2;

Or for charting coordinates that require floating point precision:

Point point;
point.X = 1.2;
point.Y = 3.4;

Besides the basic generics syntax presented so far, C# 2.0 has some generics-specific syntax. For example, consider the Pop() method of Code block 2. Suppose instead of throwing an exception when the stack is empty, you would like to return the default value of the type stored in the stack. If you were using an Object-based stack, you would simply return null, but a generic stack could be used with value types as well. To address this issue, you can use the default() operator, which returns the default value of a type.

Here is how you can use default in the implementation of the Pop() method:

public T Pop()
{
m_StackPointer--;
if(m_StackPointer >= 0)
{
return m_Items[m_StackPointer];
}
else
{
m_StackPointer = 0;
return default(T);
}
}

7Aug/100

What is the difference between overloading and overriding.

difference between overloading and overriding.

Overriding - Methods have the same signature as the parent class method.
Overloading - Methods have different parameters list or type or the return type.

Advantages and disadvantages of using multithreading

Advantages:

Simultaneous access to multiple applications
Reduced number of required servers
Improved performance and concurrency
Simplified coding of remote procedure calls and conversations

Disadvantages:

Code writing, debugging, managing concurrency, testing, porting existing code is difficult in multithreading and multicontexting.

Programmers need to remove static variables and replace any code that is not thread-safe to introduce threading into a previously non threaded application.

24Feb/100

Interview questions for C# developers

  1. If I return out of a try/finally in C#, does the code in the finally-clause run? - Yes. The code in the finally always runs. If you return out of the try block, or even if you do a “goto” out of the try, the finally block always runs:

    Both “In Try block” and “In Finally block” will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it’s a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there’s an extra store/load of the value of the expression (since it has to be computed within the try block).

  2. Is it possible to inline assembly or IL in C# code? - No.

  3. Is it possible to have a static indexer in C#? - No. Static indexers are not allowed in C#.

  4. I was trying to use an “out int” parameter in one of my functions. How should I declare the variable that I am passing to it? - You should declare the variable as an int, but when you pass it in you must specify it as ‘out’, like the following: int i; foo(out i); where foo is declared as follows: [return-type] foo(out int o) { }

  5. How do you directly call a native function exported from a DLL? -

  6. This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.

  7. How do I simulate optional parameters to COM calls? - You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters.

  8. How do you specify a custom attribute for the entire assembly (rather than for a class)? - Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

    using System;[assembly : MyAttributeClass] class X {}

    Note that in an IDE-created project, by convention, these attributes are placed in AssemblyInfo.cs.