Monday, 22 February 2016

LINQ Operator in C#






LINQ stands for Language Integrated Query and is Microsoft’s advanced data access technology that is used to query collections of data.


The collections can be of two types:


1.  local collections such as collections belonging to the System.Collections namespace  and the              System.Collections.Generic spaces

e.g.       List<T>,Dictionary, HashSet etc


2.  Other collections are remote collections such as an XML document or a database table

Syntax of LINQ
There are two syntaxes of LINQ. These are the following ones.

·         Lamda (Method) Syntax
Example
var longNames = Names.Where( n => n.length > 10);


·         Query (Comprehension) Syntax
Example
var longNames = from n in Names where n.length > 10;

  

Monday, 1 February 2016

How to return NULL value from generic method in C#

Following  my Example code for describe the issue,

following i have written the generic method to Search thru the list by using given search value
if value in list return the list and otherwise return NULL Value.















According to the code it seems ok.but it get following error

Cannot convert null to type parameter 'T' because it could be a value type

For avoid that issue following option

1- Method

Return default(T) which means you'll return null if T is a reference type (or a nullable value type), 0 for int, '\0' for char etc

2- Method
Restrict T to be a reference type with the where T : class constraint and then return null as normal


Single Responsibility Principle (SRP)