Thursday, September 6, 2012

Action delegates in C#

In last few posts about I have written lots of things about delegates and this post is also part of that series. In this post we are going to learn about Action delegates in C#.  Following is a list of post related to delegates.

  1. Delegates in C#.
  2. Multicast Delegates in C#.
  3. Func Delegates in C#.

Action Delegates in c#:

As per MSDN action delegates used to pass a method as parameter without explicitly declaring custom delegates. Action Delegates are used to encapsulate method that does not have return value. C# 4.0 Action delegates have following different variants like following. It can take up to 16 parameters.

  • Action – It will be no parameter and does not return any value.
  • Action(T)
  • Action(T1,T2)
  • Action(T1,T2,T3)
  • Action(T1,T2,T3,T4)
  • Action(T1,T2,T3,T4,T5)
  • Action(T1,T2,T3,T4,T5,T6)
  • Action(T1,T2,T3,T4,T5,T6,T7)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15)
  • Action(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16)
Share:
Wednesday, September 5, 2012

Incremental search in Visual Studio

Visual studio is a Great IDE and there are still lots of feature that not known to developers. Incremental search is one of them. This is a amazing feature to find code in particular document and its available from Visual Studio 2010 and carried over in Visual Studio 2012. Incremental search allows developers to search in document without blocking UI and allow to search as they type.

Interesting!! right.. So let’s open visual studio and see how it works. Once you open Visual Studio and press Ctrl + I and type something it will find the string without blocking your visual studio UI. Just like following.

Incremental Search in Visual Studio
Share:
Saturday, August 25, 2012

Func Delegate in C#

We already know about delegates in C# and I have previously posted about basics of delegates in C#. Following are posts about basic of delegates I have written.

Delegates in C#
Multicast Delegates in C#

In this post we are going to learn about Func Delegates in C#. As per MSDN following is a definition.

“Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.”

Func can handle multiple arguments. The Func delegates is parameterized type. It takes any valid C# type as parameter and you have can multiple parameters as well you have to specify the return type as last parameters.

Followings are some examples of parameters.
Func<int T,out TResult>
Func<int T,int T, out Tresult>

Now let’s take a string concatenation example for that. I am going to create two func delegate which will going to concate two strings and three string. Following is a code for that.

using System;
using System.Collections.Generic;

namespace FuncExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string, string> concatTwo = (x, y) => string.Format("{0} {1}",x,y);
            Func<string, string, string, string> concatThree = (x, y, z) => string.Format("{0} {1} {2}", x, y,z);

            Console.WriteLine(concatTwo("Hello", "Jalpesh"));
            Console.WriteLine(concatThree("Hello","Jalpesh","Vadgama"));
            Console.ReadLine();
        }
    }
}

Share:
Friday, August 24, 2012

What is Inversion of control and why we need it?

Most of programmer need inversion of control pattern in today’s complex real time application world. So I have decided to write a blog post about it. This blog post will explain what is Inversion of control and why we need it. We are going to take a real world example so it would be better to understand.

The problem- Why we need inversion of control?


Before giving definition of Inversion of control let’s take a simple real word example to see why we need inversion of control. Please have look on the following code.

public class class1
{
    private class2 _class2;

    public class1()
    {
       _class2=new class2();
    }

}
public class class2
{
     //Some implementation of class2
}

I have two classes “Class1” and “Class2”.  If you see the code in that I have created a instance of class2 class in the class1 class constructor. So the “class1” class is dependent on “class2”. I think that is the biggest issue in real world scenario as if we change the “class2” class then we might need to change the “class1” class also. Here there is one type of dependency between this two classes that is called Tight Coupling. Tight coupling will have lots of problem in real world applications as things are tends to be change in future so we have to change all the tight couple classes that are dependent of each other. To avoid this kind of issue we need Inversion of control.
Share:
Friday, August 17, 2012

IUnlockJoy I build amazing Windows Phone Apps event –11th August rocks!!

Last Saturday ,11th August Microsoft and Ahemdabad .NET user group has organized the IUnlockJoy I build amazing Windows Phone Apps  event. It was first in india as IUnlockJoy campaign was just launched by Microsoft before 3 days. It was superb and well organized thanks to Dhola Mahesh, Prabjhot Baxi,Kaushal Bhavasar and Pranjal Nigam.



It was a great event with almost 100+ people during holiday. Event was started by a keynote speaker Mahesh Dhola. He introduced IUnlockJoy- I build amazing Windows Phone Apps campaign and some of benefits of it.


VIS_8584

After his session Prabjhot singh Baxi has taken a session about how you can get best out of windows phone. He has describe all the process from opening a developer account to publish an app to market place. The session was full of information and I really enjoyed it.
Share:
Saturday, July 28, 2012

Multiple file upload with asp.net mvc and HTML5

Few days I have written a blog post about Multiple file upload with asp.net 4.5 and Visual studio 2012. It was greatly appreciated by the community and also been part of www.asp.net community daily spot light.  On that post one of my reader Ciwan Kurd has requested the asp.net mvc version of that post. So in this post I will explain how we can do multiple file upload with HTML5.

For this post I am going to use asp.net mvc 3 with HTML5 template and visual studio 2012 but you can use same techniques in any version of asp.net mvc.  First things we needs to do create a HTML form for the uploading file in asp.net mvc view so following is a code for that.

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    <label for="file">Upload Image:</label>
    <input type="file" name="files" value="" multiple="multiple"/>
    <input type="submit" value="Upload Image" />
}

In above code that I have use @HTML.BeginForm to create a HTML form with multipart as this attribute is required to upload any kind of files on the server. I have mapped that form to upload action result. Also I have used the file input control with HTML5 multiple attribute which allow us to upload multiple files on the server. Now its time to write code in asp.net mvc controller. Following is a code for that.
Share:
Tuesday, July 17, 2012

Why we should write blogs and do community work

I am doing blogging since last 5 years and I have seen lots of people asking the same things. Why do you write blog? do you have any side income for this blog? Why you are so active in community ? I am answering this questions almost every day. So I decided to write a blog post about it. Following are the reason why I am writing blogs and doing community work?

Help:

I like to help people and that is one of the main reason behind the all blogs and community work. Also helping other is a great way to learn new things. Because other person may be facing a problem which you never encountered during your professional life or project you are doing!!. At the end of you can feel proud that you help somebody.

Learning:

I have to learn lots of things everyday to write my blogs. Some one said Teaching some one is best way to learn new things. It apply here also when you write blog or answer question on forum.

Earn Award and Get famous:

This is one of the advantages of writing blogs and doing community things. People will know that you are having such knowledge in respected fields. I have got Microsoft Most Valuable Professional award 3 times due to my blogging and community work. But remember one thing your first intension of doing communication work is to help people. All other things will come as other benefits.

Communication skills:

Once you communicate with lots of people and write blog it will definitely increase your communication skills. If you are writing some blogs then its going to increase your writing skills also.
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews