Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework. October 2013 Entries
Dispose Vs Finalize
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects, etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. The problem with finalize is that garbage collection has to make two rounds in order to remove objects which have finalize methods since objects have to be placed in finalization queue in first round and later in next round of ......

Posted On Tuesday, October 22, 2013 12:50 PM

ref Vs out
The ref keyword tells the compiler that the object will be initialized before entering into the function, while out keyword tells the compiler that the object will be initialized inside the function.ref is two-ways, out is out-only ......

Posted On Tuesday, October 22, 2013 12:35 PM

Short-circuited operators
//Both are evaluatedif (_firstCondition =="" & _secondCondition == ""){ //Some code } // The second condition is evaluated only if the first is trueif (_firstCondition =="" && _secondCondition == ""){ //Some code } ......

Posted On Tuesday, October 22, 2013 12:32 PM

Collection Optimizations
Array Vs. ListDevelopers use arrays when their collection has a fixed length, and lists when they do notIt is always more efficient to use arrays over lists whenever possible.Underneath the hood most collections use arrays in their implementation, but it is still better to default to using an array. If the case arises in development that requires a variable size collection, List would be a good choice.List Vs. ArrayListUnlike List<T>, ArrayList is not a generic collection which means it stores ......

Posted On Monday, October 21, 2013 5:10 PM

String Optimizations
STRING ITERATIONC# allows the characters of a string to be iterated by using a “foreach” or a simple index access within a traditional “for”In this specific case, it is much more efficient to use a traditional “for” loop for iterating over a stringOne main reason for this is due to the fact that the JIT compiler can optimize “for” loops to remove bounds checking and other operations (it cannot do this with “foreach”).Using StringBuilder ClassC# Strings are immutable objects underneath the hood, so ......

Posted On Monday, October 21, 2013 3:38 PM

Performance Gains
Using the “static” keyword Static classes can only contain static members and cannot be instantiated. So using the static keyword will make your code a bit faster since no object creation is involved.Static methods can be accessed without an instance of the class being created.Static fields are shared across all instances of that class.In-short, they are all loaded once into memory and do not require instance resolution at run-time, which reduces the amount of instructions needed to call the method.Static ......

Posted On Monday, October 21, 2013 2:52 PM

Efficiency vs. Readability
Maintainable SolutionEfficient Solution
void swap(ref int x, ref int y)
  int temp = x;
  x= y;
  y = temp;
void swap(ref int x, ref int y)
  x = x ^ y; //Bit-wise XOR
  y = y ^ x; //Bit-wise XOR
  x = x ^ y; //Bit-wise XOR

Posted On Monday, October 21, 2013 2:02 PM

Unit Testing Abstract Class and Protected Methods
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 Unit Testing Protected Methods You are responsible for unit testing your Protected methods just like the Public ones. The suggested guidance for how to do so is to create a fake class in your unit test that inherits from the class you are testing. Then in this new class, create public methods that expose the protected members of the base class. You can now use this fake child class in your tests.Unit Testing Abstract Classes ......

Posted On Monday, October 21, 2013 1:47 PM

Copyright © Abhishek Anand | Powered by: