Bits & Bytes about .net technology framework.
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 ......
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 ......
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, ......
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... ......
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 ......
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 ......
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 ......
The goal of both the Single and SingleOrDefault methods is the same: get one element in the collection that matches the provided predicate. The difference between the two lies in their behavior when no matching element is found. The Single method will throw an error saying something like “Sequence contains no elements”, while the SingleOrDefault() method will return the default value of the collection’s type. For reference types and nullable, it will return null and for value types it will return ......
As a developer, you have probably run into the situation where you want to return more than 1 value from a method. Even though this can often be a flag for bad function design, there are certain cases where returning 2+ values makes sense. In order to accomplish this functionality in other programming languages like Java, you often would have to create a Data Transfer Object which can become a pain and feel unnecessary. Fortunately C# provides a couple different ways to have “multiple return values”. ......
As most of you know LINQ provides a set of great extension methods for common functions on .NET Collections. In most cases LINQ is simply the “prettier”, abstracted out implementations of the lower level .NET language features like loops and conditionals. Even though LINQ often increases readability, it does come with a little more overhead. This additional overhead is negligible in most cases, but when performance is a primary concern in your function every millisecond counts. In these scenarios, ......
Full c# Archive