Collection was modified; enumeration operation might not execute

“Collection was modified; enumeration operation might not execute”  this common error developer faces every time he is updating(add/delete/sort) the collection which is being enumerated.

The question is why this happens, it understandable that Deleting and Sorting might change the way index might have sorted into but Adding should work fine – if there is no sort defined.

All this is related to the way MS has implemented enumeration for its classes.

Take List<T> as an example.

In List, there is an internal variable by name “version” which is incremented on any kind of operation done on list, this includes Add, Delete, Sort. Now when enumerator object is requested from List, internally list instantiate a struct (Enumerator) which implements IEnumerator.

Initialization of struct enumerator requires list as its mandatory parameter and locally notes down current “version” of list You can think struct enumerator as Linked-list implementation using arrays.

“MoveNext()” API call, It first checks the version noted by enumerator and list matches, secondly gives the reference of next element in the array/list.

Whenever mismatch happens exception is thrown.

All above is said about MS implementation. If you create custom collection class and override GetEnumerator function you won’t receive the error.


Reteriving current directory in batch file.

Today I ran into a strange scenario, there was a need to create a batch file which will create a service.

Now the problem is service installation path was unknown to me and for any service to install it needs to its executable’s absolute path.

After doing some hit & trial and googling, came across the solution:

ECHO “Creating some service”


sc create someService binPath= %CURRENT_DIR%”\someService.exe” start= demand obj=  password=

Hope  this helps!!

Finding subset from a given set

Your job is find out which of find which records are not present in child set but are present in parent set.

Suppose you have 2 set of records. Parent set containing 2000 odd records and child set containing say 700 records.

To accomplish this I’ll suggest to take help from excel.

Step 1: Load parent set in a worksheet. Name as “Parent”.

Step 2: Load child set in another worksheet. Name as “Child”

Step 3: Use VLOOKUP in conjuction with ISNA function

  • VLOOKUP will do the filtering job
  • ISNA is take the result of VLOOKUP. In case the interested record is not in the list then this function returns error as #NA. ISNA function returns TRUE/FALSE to the reuslt if VLOOKUP.
  • The formula is: =IF(ISNA(VLOOKUP(A2,Child!A$2:B$15,1,FALSE))=TRUE,”Y”,”N”)

Step 4: Create a extra colum in parent worksheet name “IsMatched?” and use above said formula to the rest of job.

Record with “Y” suggest these records present in child set

Recrod with “N” suggest these records are not in child set


Hope this helps!!

Implicit vs explicit interface implementation

Whenever we need to implement an interface in C#, two options pops up (click Ctrl+’.’).

  • Implicit interface implementation 
  • Explicit interface implementation

What shall be selected? before making a decision, lets understand what is the difference between those two.

Solving the diamond problem
The diamond problem is related to object oriented languages that allow multiple inheritance. The problem raises when there is base class A, classes B and C derived from A and class D derived from B and C. B and C, both have a method “Foo”, that has been overridden differently. If this method is called in D, then via which class is it inherited: B or C?

As we know C# does not support multiple inheritance of classes, but it does support multiple inheritance of interfaces. Therefore, the same ambiguity may arise unless explicit interface implementation is used. Going explicitly, we can have different implementations to the same method. The problem is solved…

interface IRegularAccess
    void ApproveLoan();

interface IAdministratorAccess
    void ApproveLoan();

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
    void IRegularAccess.ApproveLoan()
        // Code for regular access approve loan.

    void IAdministratorAccess.ApproveLoan()
        // Code for administrator access approve loan.

A good example of such a usage is a bank, where one class implements 2 interfaces to the data-base proxy: IRegularAccess and IAdministratorAccess.It is clear that the ApproveLoan method shall not be implemented in the same manner for both cases.
Private and Public issues:

In implicit implementation, the public keyword is attached to the method (otherwise, compilation error). It is the opposite with explicit implementation (where the private keyword is used). This is how the implicit implementation looks with our example:

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
     public void ApproveLoan()
         // Insert your code.

The result is that the explicit implementation keeps the interface members out of the class (and out of the Intellisense too). Again, it is much easier to demonstrate this issue in code:

// Does not compile because of explicit implementation.
BankDatabaseProxy bankDB = new BankDatabaseProxy();

// This is good.
IAdministratorAccess adminAccessToBankDB = new BankDatabaseProxy();

No Virtual for Explicit:
Explicit method implementation can not be virtual, whereas an implicit method can. Child classes won’t be able to override an explicit method.

No Abstract for Explicit:
Explicit method implementation can not be abstract, whereas an implicit method can. Class that use the explicit implementation can not be abstract!

Build and Deploy a .NET COM Assembly

This article shows how to build a .NET COM assembly following the recommended best practices, and shows how to deploy it with a Visual Studio Setup and Deployment Project. Article also shows you two types of client for the COM object (early binding and late binding) and discuss the advantages and disadvantages of each.


Is managed DLL binary compatible?

This is in continuation of the error discussed in my earlier blog .

While working with this error I accidently clicked on my main application without rebuilding and it worked although I had done changes to my referenced DLL.

Does it mean compiled DLL are binary compatible?

Lets see how it works,

Below image show my code
  • Referenced DLL (you can see I’ve commented the overloaded API ) 

  • My test application code  

Now I build my exe and read the generated IL code in C# using reflector
Below are the images:

  • When overloaded API is commented 


  • When overloaded API is uncommented and my application is rebuilt. 


These images reveal that compiler intelligently creates the syntax (in IL) according to the best available function.

Pretty interesting stuff!!

Learning: creating overloads of function with params type parameter

This is about mistake which happened couple of days ago. 
We had a function having declaration like:
protected string function1(string parameter1, params string[] parameterArray)
//some code

Due to some requirement to overload this API  we added below function 

protected string function1(string parameter1, string parameter2, params string[] parameterArray)
//some code

Now if we compile these two functions the project will be compiled with no errors and also the calling function will be compiled successfully.
But for the cases where code was calling function1 using below syntax may give some undesired results.

function1(“some string“, “some string1“, “some string2“, “some string3“);

Reason:  Now the call is getting diverted to best available match which is our newly added API.

We should be extra carful about creating overloading funcitons specially when current function is having any of the params type parameter.
For above discussed case best way is to create a new API as chaning old API may end up in breaking some calling codes.