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:

Model binding with ASP.NET 4.5 and Visual Studio 2012

Note:I have written a whole series of Visual Studio 2012 features and this post will also be part of same series. You can get the whole list of blogs/articles from the Visual studio 2012 feature series posts there. Following is a link for that.Visual Studio 2012 feature series
In earlier version of the asp.net we have to bind controls with data source control like SQL Data source, Entity Data Source, Linq Data Source if we want to bind our server controls declaratively. Some developers prefer to write whole data access logic and then bind the data source with databind method. Model binding is something similar to asp.net mvc binding.

Model Binding in ASP.NET 4.5 and Visual Studio 2012:


With ASP.NET 4.5 and Visual studio 2012 we have a new method of binding data called ‘ Model binding’. It’s a code focus approach of data binding. Now all the controls will have ItemType property which will implement strongly type binding. The server controls will then call appropriate methods at the proper time in page life cycle and bind the data.

So what we are waiting for ? let’s take one example. First we need to create a model. For the this I have created a model class called ‘Customer’. Following is code for that.
Share:
Thursday, July 12, 2012

Free Visual Studio 2012 eBook from Telerik

Note:I have been writing lots of things about Visual Studio 2012 and this post is same in the series. You can find complete list of post from the following link.Visual Studio 2012 feature series- What’s new in Visual Studio 2012.
Visual studio 2012 is there in RC stage and I am exploring that into the great extent. Visual Studio 2012 is great editor there is no doubt. It is much more mature and comes with lots of features. Telerik – A well know in third party controls released a free eBook on Visual Studio 2012 and ASP.NET 4.5. You can find lots of time saving feature documented in this eBook.

Visual Studio 2012 feature list - What's new in Visual Studio 2012

Following is a quick list of features they have explored in Visual Studio 2012.
  1. JavaScript Intellisense
  2. CSS Intellisense
  3. ASP.NET and ASP.NET Web API
  4. Strongly typed data binding
  5. Page Inspector(new Debugging Tool)
  6. HTML5 and CSS3 is available out of box
  7. Request validation prevents yellow screen of death
  8. WAI Aria makes the web usable by all
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