Basics of delegate : C#

Introduction

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

Advertisements

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

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.

Introduction

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.

Introduction

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#

Introduction

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.

Introduction

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

Introduction

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#

Introduction

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

Basics of Web API and REST

ASP.NET Web API is a framework built by Microsoft, which makes it easy to build HTTP services (REST) that reach a broad range of clients, including browsers and mobile devices. With Web API, if the client is requesting the data to be returned as JSON or XML, the Web API deals with the request type and returns the data appropriately based on the media type.

Web API is an ideal platform for building pure REST services where the request and response happens with HTTP protocol. The client can make a GET, PUT, POST, and DELETE request and get the response appropriately.

Before we go deep in Web API, let’s understand the concept of REST. Continue reading