Basics of delegate : C#


A delegate, which is also known as function pointer in C/C++, is reference type that used to hold the reference of method with specific signature, and invoke method through the delegate instance.
In .Net Delegate types are sealed and immutable type.

Types of Delegates

There are three types of delegates that can be used in C#.

  • Single Delegate
  • Multicast Delegate
  • Generic Delegate

Continue reading

What is the difference between MVP and MVC? : C#

This article will present clear and succinct difference between two architecture presentation patterns i.e. MVP and MVC. Many developers are confused around what is the difference between these patterns.

Here are the key differences between the patterns:

MVP Pattern

  • View is more loosely coupled to the model. The presenter is responsible for binding the model to the view.
  • Easier to unit test because interaction with the view is through an interface
  • Usually view to presenter map one to one. Complex views may have multi presenters.

Continue reading

Why use AsQueryable() instead of List()? : C#

While using data access with the Entity Framework and LINQ, we always return List(), while some of developer prefer AsQueryable().

When we use AsQueryable, it just create a query, which is instruction to get a list. We can make further changes to the query later such as adding new Where clause that sent to the database level.

Returning IQueryable<T> has the advantage, that the execution is postponed until we start enumerating the result and we can compose the query with other queries and still get server side execution.

Continue reading

Local Storage vs Cookies

To reduce load times on our websites, we always cache some item in cookies and local storage. In this article some of differences between “Local Storage vs Cookies” is described.

Both Cookies and local storage serve difference purposes.

Choosing from these two

Cookies are primarily for reading server-side, local storage can only be read client-side.

So choosing from these two location to store data for our app, one question comes that, who needs this data : the client or the server?

If it’s our client (like JavaScript), then we can store all data in local storage.

If it’s our server need data, local storage isn’t so useful because we have to forward the data with each request by somehow (with Ajax or hidden form fields or something).


LocalStorage stays on the client, while cookies are sent with the HTTP header.

Continue reading

How to Change the Return Type in Linq : Select operator

When I was started with Linq, I stuck many times on what the query will return, how to change return type of query? Sometimes our linq code need specific result as requirements.


We often write SQL query for database systems when it need to fetch stored data and we explicitly specify a set of columns to return, like:

The main goal for limiting the number of column is:

  1. Return only those necessary for query in order to improve performance.
  2. And, limit network traffic

The first statement return every column and row of the Employee table; whereas second will return only the three columns explicitly for all row, it save server and network resources.

Similarly select operator in LINQ query expressions provide the same task, and it provides powerful control over how and what data shape is returned from a query expression.

There are different ways a select clause can return results:

  1. As a single result value or element
  2. In an IEnumerable<T> where T is of the same type as the source items
  3. In an IEnumerable<T> where T is any existing type constructed in the select projection
  4. In an IEnumerable<T> where T is an anonymous type created in the select projection
  5. In an IEnumberable<IGrouping>, which is a collection of grouped objects that share a common key

Continue reading

Linq’s Where Clause (operator) : Result Filtering

One of the main task of a Linq query is to filter the results from a larger collection on some criteria. This is perform by Where operator, which tests each element within a source collection and returns only those elements that pass when tested against a given predicate expression.


A predicate is simply an expression that takes an element of the items in source collection and returns true or false.

The extension method for the Where operator is very simple; it iterates the source collection using a foreach loop, testing each element and return passed elements. Here is the actual code in the System.Linq library:

Continue reading

Predicate with Example : C#


When we want to test some criteria on collection object’s element and depending upon result test result (true or false) we perform some operation on that collection or its elements, we should use predicate.

From MSDN:

Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.

Predicate<T> is a functional construct providing a convenient way of basically testing if something is true of a given T object.

Continue reading

LINQ to Objects C# : Introduction

In this post, I discuss about LINQ to object, an amazing features of C# which is introduced in C# 3.0.


Linq is libraries built by Microsoft to make working with data (for example, a collection of in-memory objects, rows from a database table, or elements in an XML file) simpler and more natural.

LINQ has changed the approach to writing data driven applications. Code is become simpler, often faster, and easier to read.

For demonstrate the approach to coding problem with and without using LINQ, I’ll show example that find out vision how coding practice without LINQ changed with using LINQ.

Example is that there is employee list which is to be sorted accoring to their name and grouped sort by their Department name.
Continue reading

String vs string


In this post I discuss what is the difference between String and string in C#? (notice the case). What are the guidelines for the use of each? And what are the differences?

What is String and string?

string is an alias for System.String. So technically, there is no difference. It’s like int vs. System.Int32.

Continue reading

Tips and Tricks of C#


I, C# developer, know the basics of C# like, declaration, condition, loops, operators, generics, anonymous types, lambdas, LINQ. Some of features there are in C# that I don’t know if it was there before I look at a stackoverflow question (Hidden Features of C#). In that question there are many answer that show how to use those features. You must visit that because you are a C# developer and you should know about that.

Continue reading