Posts

Showing posts with the label C#

Parallel programming in c# 4.0

In our daily programming routine we use thread mechanism to parallelize your code to distribute work across multiple processors .  To take advantage of the hardware of today and tomorrow Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. Let’s start with a simple concept in parallel programming. Data Parallelism (Task Parallel Library) Data Parallelism describes how to create parallel for and foreach loops. In this article we will look into simple for loop in parallel programming world. In this application I wrote a simple method to calculate the Root of a given number. Let’s look into the executing time of for and Parallel.For loop methods. The below code took me 16 seconds to execute. I am using 2.8-Ghz dual core 64 bit processor with 4 GB of RAM. for (int i = 2; i < 20; i++) { var result = SumRootN(i); ...

Google Authentication with ASP.Net

Image
Code download available at the bottom of the post In this post i would like to share my experience with  Google Authentication with Asp.net membership (Forms Authentication) High level google authentication work flow with ASP.NET membership 1.              Your application requests access and gets an unauthorized request token from Google's authorization server. 2.              Google asks the user to grant you access to the required data. 3.              Your application gets an authorized request token from the authorization server. 4.              You exchange the authorized request token for an access token. 5.              You use the access token to requ...

Difference between Var and IEnumerable

Var VS IEnumerable In previous article I explain the difference between  IEnumerable and IQuerable ,  IEnumerable and IList . In this article I expose the difference between Var and IEnumerable. IEnumerable is an interface that can move forward only over a collection, it can’t move backward and between the items. Var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration. Both have its own importance to query data and data manipulation. Var Type Since  Var  is anonymous types, hence use it whenever you don't know the type of output or it is anonymous. Suppose you are joining two tables and retrieving data from both the tables then the result will be an Anonymous type. var q =( from e in tblEmployee join d in tblDept on e . DeptID equals d . DeptID select new { e . EmpID , e . FirstName , d . Dept...

what is the difference between declarative and imperative programming

A great C# example of declarative vs. imperative programming is LINQ. With  imperative  programming, you tell the compiler what you want to happen, step by step. For example, let's start with this collection, and choose the odd numbers: List <int> collection = new List <int> { 1 , 2 , 3 , 4 , 5 }; With imperative programming, we'd step through this, and decide what we want: List <int> results = new List <int> (); foreach ( var num in collection ) {     if ( num % 2 != 0 )           results . Add ( num ); } Here, we're saying: Create a result collection Step through each number in the collection Check the number, if it's odd, add it to the results With  declarative  programming, on the other hand, you write code that describes what you want, but not necessarily how to get it (declare your desired results, but not the step-by-step): var results = collection . Whe...

Deadlocks in .Net

Deadlocks Before starting to use the thread pool in your applications you should know one additional concept:  deadlocks . A bad implementation of asynchronous functions executed on the pool can make your entire application hang. Imagine a method in your code that needs to connect via socket with a Web server. A possible implementation is opening the connection asynchronously with the  Socket  class' BeginConnect  method and wait for the connection to be established with the  EndConnect  method. The code will be as follows: class ConnectionSocket { public void Connect() { IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName()); IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[0], 80); Socket s = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); IAsyncResult ar = s.BeginConnect(ipEndPoint, null, null); s.EndConnect(ar); } } So far, so good—calling...

Delete item from "Enumerable List"

While performing a delete operation on a "Enumerable List" always use "for" statement instead of "for each". In for statement last from the last item in the list. Example: for(int index=tempList.count;index > 0;i--) {  //Delete logic here................... }