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

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

Reflection with Example

Introduction

Process of runtime type discovery is called reflection. Using reflection we are able to obtain the metadata information, dynamically. For example we can get list of all types contained within a given assembly, including methods, attributes, fields, custom attributes, properties and many more.

System.Type

Before dive into reflection, first have a look at System.Type class. Because it is a base foundation of reflection api.

Continue reading