Friday, 29 November 2013

async and await - 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Calling DemoCustomDownloader");

            string text = DemoCustomDownloader("http://www.pluralsight.com").Result;
            Console.WriteLine(text);

            Console.WriteLine("Called DemoCustomDownloader");

            Console.ReadKey();
        }

        private async static Task DemoCustomDownloader(string uri)
        {
            CustomDownloader downloader = new CustomDownloader();
            Task customDownloaderTask = Task.Factory.FromAsync(downloader.BeginDownload, downloader.EndDownload, uri);
           
            return await customDownloaderTask;
        }
    }





    public class CustomDownloader
    {
        private WebClient client = new WebClient();

        public IAsyncResult BeginDownload(AsyncCallback callback, object state)
        {
            Func downloader = this.Download;

            return downloader.BeginInvoke(state.ToString(), callback, null);
        }

        public string EndDownload(IAsyncResult result)
        {
            Func downloader = (result as AsyncResult).AsyncDelegate as Func;

            return downloader.EndInvoke(result);
        }

        private string Download(string uri)
        {
            return this.client.DownloadString(uri);
        }
    }
}

async and await - 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Calling DownloadHeaders");

            DownloadHeaders("http://www.pluralsight.com");

            Console.WriteLine("Called DownloadHeaders");

            Console.ReadKey();
        }

        private async static void DownloadHeaders(string url)
        {
            HttpWebRequest request = WebRequest.Create("http://www.pluralsight.com") as HttpWebRequest;

            request.Method = "HEAD";
            using (var response = await request.GetResponseAsync() as HttpWebResponse)
            {
                PrintHeadersUsingLinq(response.Headers);
            }           
        }

        private static void PrintHeadersUsingLinq(WebHeaderCollection headers)
        {
            var keyValuePairs = from key in headers.AllKeys
                       select string.Format("{0}:{1}", key, headers[key]);

            Console.WriteLine(string.Join(Environment.NewLine, keyValuePairs));
        }
    }
}

Visual Studio 2013

* Version - 12.0

* .Net Framework Version - 4.5.1

* Overrides 4.0, so all dll's under 4.0 folder is replaces.(%WINDIR%\Microsoft.NET\Framework)

* 4.5.1 Reference assemblies are in

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework

* 4.5.1 GAC is usual location - C:\windows\microsoft.net\assembly(up until 3.5 in in c:\windows\assembly)
  here are 3 types of GAC’s now available
    · GAC_32
    · GAC_64
    · GAC_MSIL
  While the 32-bit platforms have GAC_32 and GAC_MSIL, the 64-bit have all the three. GAC_32 contains assemblies that are specific to the 32-bit platform and GAC_64 specific to the 64-bit platform. The GAC_MSIL contains the assemblies that are platform agnostic. While trying to load assemblies from the GAC, the CLR first looks at the GAC specific to the platform. If it is unable to find the same, it then moves to the GAC_MSIL.

Wednesday, 27 November 2013

What do Clustered and Non clustered index actually mean?

Couldn't resist sharing - http://stackoverflow.com/questions/1251636/what-do-clustered-and-non-clustered-index-actually-mean

A clustered index means you are telling the database to store close values actually close to one another on the disk. This has the benefit of rapid scan / retrieval of records falling into some range of clustered index values.
For example, you have two tables, Customer and Order:
Customer
----------
ID
Name
Address

Order
----------
ID
CustomerID
Price
If you wish to quickly retrieve all orders of one particular customer, you may wish to create a clustered index on the "CustomerID" column of the Order table. This way the records with the same CustomerID will be physically stored close to each other on disk (clustered) which speeds up their retrieval.
P.S. The index on CustomerID will obviously be not unique, so you either need to add a second field to "uniquify" the index or let the database handle that for you but that's another story[If the clustered index is not a unique index, SQL Server makes any duplicate keys unique by adding an internally generated value called a uniqueifier].
Regarding multiple indexes. You can have only one clustered index per table because this defines how the data is physically arranged. If you wish an analogy, imagine a big room with many tables in it. You can either put these tables to form several rows or pull them all together to form a big conference table, but not both ways at the same time. A table can have other indexes, they will then point to the entries in the clustered index which in its turn will finally say where to find the actual data.

Clustered Index
  • Only one per table
  • Faster to read than non clustered as data is physically stored in index order
Non Clustered Index
  • Can be used many times per table
  • Quicker for insert and update operations than a clustered index

Monday, 18 November 2013

Strong typed and Weakly typed too

As a C# programmer, used to think C# is strongly typed, but after the introduction of dynamic keyword, we can't really claim so anymore. Between read a great article from Eric, below is brief

1. C# is is both a strong and weakly typed language
2. What is strongly typed - type safe, memory safe and statically typed.
          > type safe - it won't allow a static type to store in to a variable of another incompatible type. Static type disallows dynamic here.
          > memory safe - prevents accidental access to bad memory.
          > statically typed - all typed are determined at compile time.
3. Weak typed, because 'unsafe', 'dynamic' and cast operators will provides us mechanism to overrule above said safety systems.