Interview Tips Interview Tips, Interview Questions and Answers

29Sep/110

Volatile Fields

Software built on the .NET framework is subject to many optimisations. Some optimisation is performed when compiling your program or library in Visual Studio or using the command-line compiler. Other optimisations are applied when executing the compiled intermediate language (IL) code. These vary according to the type of processor used to run the program. In many situations these optimisations lead to faster code or smaller programs without any noticeable side effects.

One optimisation that can have side effects relates to publicly visible fields in classes or structures. When you request the value of such a field the program normally performs a non-volatile read. This type of read can be optimised to improve the performance of the program. For example, the processor may choose to read the value from memory earlier than expected, and potentially in a different order than specified, in preparation for its later use. This may move the value to the processor's cache memory, where it can be accessed more quickly than from the main memory, or to its registers for yet faster performance. In single-threaded code these changes are unnoticeable.

When you are creating a multithreaded application or one that uses parallel programming code, non-volatile reads can present a problem. To illustrate this, consider the following program:

17Nov/100

What Can Generics Do For Me?

In .NET v1.0 there were collections, such as the ArrayList for working with groups of objects. An ArrayList is much like an array, except it could automatically grow and offered many convenience methods that arrays don't have. The problem with ArrayList and all the other .NET v1.0 collections is that they operate on type object. Since all objects derive from the object type, you can assign anything to an ArrayList. The problem with this is that you incur performance overhead converting value type objects to and from the object type and a single ArrayList could accidentally hold different types, which would cause a hard to find errors at runtime because you wrote code to work with one type. Generic collections fix these problems.

A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This eliminates type mismatches at runtime. Another benefit of type safety is that performance is better with value type objects because they don't incur overhead of being converted to and from type object. With generic collections, you have the best of all worlds because they are strongly typed, like arrays, and you have the additional functionality, like ArrayList and other non-generic collections, without the problems.

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);
}
}

7Nov/100

C# Topic: Generics Overview

Generics are the most powerful feature of C# 2.0. Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code. In concept, generics are similar to C++ templates, but are drastically different in implementation and capabilities. This article discusses the problem space generics address, how they are implemented, the benefits of the programming model, and unique innovations, such as constrains, generic methods and delegates, and generic inheritance. You will also see how generics are utilized in other areas of the .NET Framework such as reflection, arrays, collections, serialization, and remoting, and how to improve on the basic offering.

Generics allow you to define type-safe classes without compromising type safety, performance, or productivity. You implement the server only once as a generic server, while at the same time you can declare and use it with any type. To do that, use the < and > brackets, enclosing a generic type parameter. For example, here is how you define and use a generic stack:

public class Stack
{
T[] m_Items;
public void Push(T item)
{...}
public T Pop()
{...}
}
Stack stack = new Stack();
stack.Push(1);
stack.Push(2);
int number = stack.Pop();

27Sep/100

is vs as in C#

This code

bool b = x is Foo;

could be considered as a syntactic sugar for

bool b = (x as Foo) != null;

in which case is is a syntactic sugar for as. Similarly,

Foo f = x as Foo;

could be considered to be a syntactic sugar for

var temp = x;
Foo f = (temp is Foo) ? (Foo)temp : (Foo)null;

in which case as is a syntactic sugar for is. Clearly we cannot have both of these be sugars because then we have an infinite regress!

The specification is clear on this point; as (in the non-dynamic case) is defined as a syntactic sugar for is.

However, in practice the CLR provides us instruction isinst, which ironically acts like as. Therefore we have an instruction which implements the semantics of as pretty well, from which we can build an implementation of is. In short, de jure is is is, and as is as is is, but de facto is is as and as is isinst.

13Aug/100

Debug Vs Trace in VSTS

Debug Vs Trace.

Both these objects are found in the System.Diagnostics namespace.
Both are used for diagnose problems without interrupting application execution.
Debug statement can only be used in debug mode while trace statement can be used both in debug and released mode.
Debug statements can't be compiled into a release version.

Define trace class.

The trace class in the code is used to diagnose problem.
You can use trace messages to your project to monitor events in the released version of the application.
The trace class is found in the System.Diagnostics namespace.

20May/100

.NET interview: What is the difference between a Thread and Process?

A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread. Prior to the introduction of multiple threads of execution, applications were all designed to run on a single thread of execution.

When a thread begins to execute, it continues until it is killed or until it is interrupted by a thread with higher priority (by a user action or the kernel’s thread scheduler). Each thread can run separate sections of code, or multiple threads can execute the same section of code. Threads executing the same block of code maintain separate stacks. Each thread in a process shares that process’s global variables and resources.

13Mar/100

C# interview questions and answers

1. What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in cases where a large amount of manipulation is done in the text. Strings are immutable, so it is increasingly being operated, a new instance is created.

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

No.

3. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The first three performs a deep copy of the array, the second three is shallow.

4. How can you sort the elements of the array in descending order?

By calling Sort() and then Reverse() methods.

5. 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 Process.Exception. You can also omit the parameter information type in this case and write catch {}.

6. Why is it a bad idea to throw your own exceptions?

Well, if that point is known that an error has occurred, why not write the code of law to resolve this error, instead of passing a new Exception object to the catch block? Start your own exclusions means that some design flaws in the project.

7. What’s a delegate?

A delegate object encapsulates a reference to a system. In C++ they were referred to as function pointers.

8. How’s the DLL Hell problem solved in .NET?

Assembly versions allows the application to specify not only the library it needs to function (which was available under Win32), but also the version of the Assembly.

9. What are the ways to deploy an assembly?

An MSI installer, a CAB archive, and XCOPY command.

10. What’s a satellite assembly?

When writing a multilingual application in multi-cultural. NET, and need to deliver the core application separately from the localized modules, the localized assemblies that change the core application are called satellite assemblies.

11. What’s the difference between // comments, /* */ comments and /// comments?

Single-line, multi-line and XML documentation comments.

12. How do you generate documentation from the C# file commented properly with a command-line compiler?

Compile it with a /doc switch.

13. What’s the difference between <c> and <code> XML documentation tag?

Single line code example and multiple-line code example.