WHAT'S NEW?
Showing posts with label dotnet. Show all posts
Showing posts with label dotnet. Show all posts

Ever wondered how our C# code gets compiled and becomes an executable that runs on our machines?

I am sure I wrote more than a million lines of code in C# alone in my career of 7 years till date. I should have written another million lines of code in SQL Server, Android, Node.js, JavaScript and few other NO SQL languages like Mongodb, Redis and Elasticsearch.

Today, i was cleaning up my book shelf and found a book that i wrote sometime in 2012. That was when i was not even 4 years of experienced and the only language that i spoke apart from my mother tongue, Hindi and English was C#. In that i drew a flow chart which explained me the steps that were involved in making my C# code become an executable/ a dll that ran on my machine. I wanted to share it with you all and here it is [ not the hand written one, of course :-) ]


IEnumerable 


  • Is for LINQ to Objects
  • Objects that are in memory on Heap


IQueryable 


  • Is for working on LINQ to SQL
  • Inside it, it using Expressions to frame and execute sql queries.


Func<T>

  • Can return a value. The last value in the definition is the return.
Ex: 

Func<int,int> PrintNumber = x => (x);
            Console.WriteLine(PrintNumber(50));


Action<T>

  • Cannot return any value. It is only to feed and use directly

Ex:
Action PrintEmpty = () => Console.WriteLine("Hi");
            PrintEmpty();

            Action<int> PrintOneNumber = x => Console.WriteLine(x);
            PrintOneNumber(5);

Expression:

  • To be used to help the developer better understand the code.
  • It is slightly different from Func<T> and Action<T>.
  • Expression takes Func<T> as input and is to be used as a variable and not as a method that is the case with Func or Action

Ex:
Expression<Func<int, int, int>> PrintMulti = (x, y) => (x * y);

            Func<int, int, int> Mutli = PrintMulti.Compile();
            var zExp = Mutli(5, 10);
            Console.WriteLine(z);




A simple example for converting the normal linq query to a Parallel Linq query in dotnet.


public void NormalLinqQuery()
        {
            IEnumerable<int> localRange = Enumerable.Range(1, 100000000);
            Stopwatch watch = Stopwatch.StartNew();
            var output = localRange
                .Where(getNum => getNum % 1234567 == 0)
                .Select(rangeNum => rangeNum);
            foreach (var num in output)
                Console.WriteLine(num);
            Console.WriteLine("Normal Query in millisec: {0}",watch.ElapsedMilliseconds);
        }

        public void ParallelLinqQueryExample()
        {
            IEnumerable<int> localRange = Enumerable.Range(1, 100000000);
            Stopwatch watch = Stopwatch.StartNew();
            var output = localRange
                .AsParallel()
                .Where(getNum => getNum % 1234567 == 0)
                .Select(rangeNum => rangeNum);
            foreach (var num in output)
                Console.WriteLine(num);
            Console.WriteLine("Parallel Query in millisec: {0}", watch.ElapsedMilliseconds);
        }
In the last 2 days, I have got my hands dirty on how to use HTML5, CSS3 and JQuery to do a simple, i mean it, very simple TODO-List using the LocalStorage of the browser.

here is what i have achieved after 2+4 hours of effort in the last 2 days. I will upload the code to Github and share the same with you folks.

trust me, JQuery is awesome with HTML5.