C# Fast Collections : Performance Tips


If we want to store a collection of values there are many possibilities in the .net framework.
we have

  • collection classes in System.collections
  • Generic collections in system.collections.generic
  • Regular Typed arrays

so which approach is the fastest… lets find out C# Fast Collections : Performance Tips

Advertisements

c# Interview Questions


What’s the difference between the Debug class and Trace class?
Documentation looks the same.  Use Debug class for debug builds, use Trace class for both debug and release builds.

Can you declare an override method to be static if the original method is not static?
No.  The signature of the virtual method must remain the same.  (Note: Only the keyword virtual is changed to keyword override)

What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack, additional overhead but faster retrieval.  Another difference is that structs cannot inherit.

What’s the difference between an interface and abstract class?
In an interface class, all methods are abstract – there is no implementation.  In an abstract class some methods can be concrete.  In an interface class, no accessibility modifiers are allowed.  An abstract class may have accessibility modifiers.

When do you absolutely have to declare a class as abstract?
1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2.  When at least one of the methods in the class is abstract.

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.

Note: MemberwiseClone() also creates a new copy but its a shallow copy. To perform a deep copy class should be marked as serializable and the object to be copied should be serialized and deserialized.

Explain ACID rule of thumb for transactions.
A transaction must be:
1. Atomic – it is one unit of work and does not dependent on previous and following transactions.
2. Consistent – data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.
3. Isolated – no transaction sees the intermediate results of the current transaction).
4. Durable – the values persist if the data had been committed even if the system crashes right after.

When should you call the garbage collector in .NET?
As a good rule, you should not call the garbage collector.  However, you could call the garbage collector when you are done using a large object (or set of objects) to force the garbage collector to dispose of those very large objects from memory.  However, this is usually not a good practice.

Note:  GC can be implemented using Finalize or extend the idisposable interface  and implement the dispose() function . Finalize method is nothing but a destructor such as ~myClass(){}. GC.Collect()  can be used to force Garbage Collection.

How do you convert a value-type to a reference-type?
Use Boxing.

Boxing vs Unboxing

Boxing is the process of converting a value type to the type object.When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.

int i = 123;
// The following line boxes i. 
object o = i;

o = 123;
i = (int)o;  // unboxing

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed.

Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:

Checking the object instance to make sure that it is a boxed value of the given value type.

Copying the value from the instance into the value-type variable.

The following statements demonstrate both boxing and unboxing operations:

int i = 123;      // a value type 
object o = i;     // boxing 
int j = (int)o;   // unboxing

For all your application development needs, visit www.verbat.com for a fiscally conscious proposal that meets your needs ( So I can keep this blog going as well!!!!)

Alternatively click through the link   if you found this article interesting. (This will help the companies Search engine rankings)