Where Extension Method V/s FindAll


Upon checking on MSDN, the only advantage of FindAll I found is backwards compatibility until .NET 2.0 on the other hand, Where seemed to be introduced in .NET 3.5 … Technically, Where should outperform FindAll for large number of records as despite the observations that on small set, these two would likely perform comparably. The reason is their mechanisms.

FindAll copies the matching elements to a new list, whereas Where just returns a lazily evaluated sequence – no copying is required. That said in the case of “FindAll”, the new List created to contain the results will have to dynamically grow to contain additional results. Memory usage of FindAll will also start to grow exponentially as the number of matching results increases where as … “Where” should have constant minimal memory usage.

Ref : http://stackoverflow.com/a/2260227, http://stackoverflow.com/a/1531756

LINQ


Below are some code snippets that I have prepared initially, if you do not find one that you are looking for, just drop me an E-mail or just a comment below would suffice.

Example Description Download
Data table to LINQ combine the ease of the database access layer with LINQ queries zip
filter asp:GridView using LINQ Depending on what the user has selected from the drop down, records of a gridview is updated zip
Get cells of a selected row get the text of all cells using LINQ and perform queries for Update/Delete/Insert in the datbase zip

 

XML / XSD Validation


I know, I should probably have a separate page for XML and C#, but friends trust me there are plenty out there and I’d very much not like to simply copy and paste it all, rather to share URLs. The resources that helped me most were [drum-roll] MSDN & W3Schools.com (http://xmlmaster.org is also a strong competitor though – IMO )

References :

Alright, so w/o further due, assuming you’ve some understanding of XSD, ( of-course ) XML, let’s see how can we Continue reading “XML / XSD Validation”

Logger & Singleton Pattern


Problem :

How do you make an instance of an object globally available and guarantee that only one instance of the class is created?

Per MSDN, to ensure that only a single instance of a class can exist, you must control the instantiation process. This implies that you need to prevent other objects from creating an instance of the class by using the instantiation mechanism inherent in the programming language (for example, by using the new operator). The other part of controlling the instantiation is providing a central mechanism by which all objects can obtain a reference to the single instance. (MSDN)

Solution:

Singleton provides a global, single instance by:

  • Making the class create a single instance of itself.
  • Allowing other objects to access this instance through a class method that returns a reference to the instance. A class method is globally accessible.
  • Declaring the class constructor as private so that no other object can create a new instance. ( and this should also make it thread-safe ! )

Let’s take a quick look at the example below, Continue reading “Logger & Singleton Pattern”

Implicit – Explicit Conversion Operators


The explicit keyword declares a user-defined type conversion operator that must be invoked with a cast. Where as the implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.

I would use a self explanatory example to describe Explicit. Continue reading “Implicit – Explicit Conversion Operators”

Quickly convert Comma Separated Values to Dictionary


If you are getting some sort of CSV let’s say some sort of configurations or data object or whatever the case may be, my strategy is : “If you can not deserialize ’em, structure ’em !”

And here is one of the possible scenario :

TIP :

If you’re dealing with more complex scenario, you might wanna consider using Tuple or Jagged Tuple ! ( What is it ? )

Continue reading “Quickly convert Comma Separated Values to Dictionary”

Remove Duplicates from stack (?)


Well first off, stack is not for that ! I mean what is the point of having list / collection / etc vs stack as different data structures ? But if still if you want to do so you can Continue reading “Remove Duplicates from stack (?)”

Quickly Clean-up Blank lines from a multi-line String


Scenario : At the time of consuming data from another application or program, one of the frequent issue is data in and data out ! I was working for similar scenario the other day where a flat was generated and then another program had lexical parser and would generate tokens etc. Long story short, in a typical workflow we had blank lines and they were causing issue. Very simple issue … once this stupid thing is identified and verified being the actual culprit !

Let’s take a look :

Continue reading “Quickly Clean-up Blank lines from a multi-line String”