Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework. .net
Deferred Execution Vs Eager Loading
Lets start with an Example :int[] listOfNumbers = new int[] { 5, 9, 8, 1, 7, 3, 6, 4, 2, 0 }; int i = 0; var query = from num in listOfNumbers select ++i; // Note, the local variable 'i' is not incremented until each element is evaluated (as a side-effect): foreach (var x in query) { Console.WriteLine("x = {0}, i = {1}", x, i); } Now without deferred execution we would expect that after the variable “query” is declared, “i” would have the value 10 and the output would be: “x = {1-10}, i = 10” However ......

Posted On Tuesday, December 3, 2013 2:41 PM

LINQ performace considerations
Outside of LINQ statements such as .Any(predicate) and .First(predicate) short circuiting execution when a matching element is found, the library does not inherently do any underlying optimizations due to the limits of what is available to it. Since LINQ only works with IEnumerables, the only thing that can be done is to loop through the elements in the collection, therefore, no index access, saving of local properties such as Length, etc. is possible. It is important to keep in mind that each time ......

Posted On Thursday, November 21, 2013 4:30 PM

Cyclomatic Complexity of Methods
Cyclomatic Complexity is a widely used software metric that is used to compute the number of decisions being made by a piece of code. In its simplest form, the complexity number is equal to: 1 + {the number of expressions in the method}. Different tools interpret the definition of “expressions” differently, thus the slightly varying metrics based on the tool being used, but in general the statement used for making decisions are counted.In general, the following are treated as expressions in C#: if, ......

Posted On Thursday, November 21, 2013 12:41 PM

Unit tests for HttpClient using HttpMessageHandler
You can unit test a class that uses HttpClient by giving that HttpClient a mock HttpMessageHandler. This way, you can capture the request and prevent it from actually going over the wire. Here is an example using Moq. HttpClient depends on HttpMessageHandler’s SendAsync() method, so give SendAsync() a stub implementation and use Moq’s Callback() to capture arguments. var handler = new Mock<HttpMessageHandler&... handler.Protected() .Setup<Task<HttpRespo... ......

Posted On Wednesday, November 20, 2013 4:32 PM

Properties vs. Methods
One of the decisions a developer needs to make while designing a class is when to use a property and when to use a method. Methods typically represent an action or an operation whereas, properties represent pieces of data associated with a class / instance. Properties are typically used to allow accessibility (getting and/or setting) to private class fields. Cases where the use of a property is recommended: Ø When the member represents a logical attribute of the type. E.g. Message is a property of ......

Posted On Wednesday, November 20, 2013 4:31 PM

Usage of Arrays
Although both arrays and collections are used in a similar fashion, there are some performance considerations when choosing one over the other. Below are some design guidelines: 1- You should not return an internal instance of an array. This allows calling code to change the array. The following example demonstrates how the array delimiters can be altered by the calling code. public sealed class DocParser { private DocParser(){} private static char[] delimiters = {',', '-', ' '} public char[] Delimiters ......

Posted On Wednesday, November 20, 2013 4:29 PM

Unit Testing Non-Public Methods
Before we proceed any further, I should say this, Testing through this public API should be your first choice.Alternative for Abstract Base Classes One common case is when an abstract base class defines functionality in some protected methods, and there are a series of inheriting classes using those methods without overriding them. In this case, rather than choosing one inheriting member at random to test the methods, you can simply create a “Fake” class locally in your test project that inherits ......

Posted On Wednesday, November 20, 2013 4:03 PM

Assembly Initialize
AssemblyInitialize is very similar to the ClassInitialize and TestInitialize attributes, but rather than executing before classes/methods are run it executes before the any method marked with TestMethod, ClassInitialize, or TestInitialize inside this assembly. Only one method in an assembly can be annotated with this attribute. Code that belongs in the method decorated with AssemblyInitialize is any code that sets up shared resources that are used by tests across the assembly. Tests should not change ......

Posted On Monday, November 18, 2013 7:13 PM

Use of Byte/Short
In terms of performance some developers jump to the conclusion that Byte and Short would be generally more efficient since they require less room on the stack, but actually this is a common misnomer. Int32 (int) is actually more efficient than both Int16 (short) and Byte (byte) in most situations due to the fact that modern processors are optimized for use of 32 and 64 bit values. When a short (16 bit) or byte (8 bit) is read, the processor must read the entire 32 bits anyway and then apply a mask ......

Posted On Monday, November 18, 2013 7:11 PM

Deferred Execution with LINQ
One of the best features of LINQ is the fact that it is lazy. Lazy or Deferred Execution allows you to specify all the conditions of your LINQ statement (Where, OrderBy, Select, etc), without actually executing those functions on the given collection until the results are needed. An example of where you can see this lazy execution in action is when using the Visual Studio Debugger. Let’s say you have an IEnumerable<int> that you want to execute a LINQ statement on and inspect the resulting ......

Posted On Monday, November 18, 2013 6:56 PM

Copyright © Abhishek Anand | Powered by: GeeksWithBlogs.net