WHAT'S NEW?
Showing posts with label C#. Show all posts
Showing posts with label C#. 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 :-) ]


Visual Studio from Microsoft is mostly used as a paid one except for small application and demos that are done by people like us. For quick code snippets and tutorials, Visual Studio Express has been released a along with VS2010. From then on, Microsoft has been having an eye on the trends and developments in the market.
They have released plugins via their NuGet package manager for Node.js, CofeeScript and many others in the last few months.
After Satya Nadella became its CEO, Microsoft has predominantly been embracing open source communities. Their MVP now includes open source contributions as well.
They have recently released VS-Code editor that fits rightly for a right looking IDE for Node.js. Its light wait and installs quickly. It is a perfect one to develop ASP.NET with Node.js.
  

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);



Was trying few things in random and came across this.
How to call a non-static method inside a static method which belong to the same class. Here is the sample.
question framed myself.



I came across one question too many times on multiple websites and from multiple persons -
"What is REST and RESTful service?" or "Please tell me how to define RESTful service?" or "What is the significance of RESTful service?"

So, I have decided to write this post and believe this will clear the air about the basic understanding of RESTful service.

Here we go:

  • REST stands for Representational State Transfer.
  • RESTful service says that any service should be treated as a RESOURCE and it should be available and be accessible using simple HTTP binding.
  • It says that any operations that we perform on database are resource operations. So, RESTful service should also be able operate in the same manner.
  • Thus, all the CRUD operations, called the VERBS- that is actions should be performed using the REST.
  • So they are – GET(), POST(), PUT(), DELETE().



What next? Think about ODATA services? .... sure, will take this up too for the benefit of all of us here, in the next post. Happy Coding!!! 

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);
        }
Hi guys,
Wish you and your dear one's a wonderful new year- 2014 ahead.

Continuing our abilities in trying to bring the best of us using our code, we are in the 3rd episode of this. This time around, we will discuss how we can make our code get rid of the red lines.
Red lines? Yes, the hard coded text in C# is in general in red color and so we call it the Red-Color-Phobia.

Red-Color-Phobia.

Many developers have this tendency of hard coding few values, during the process of assigning things, comparing things or while retrieving things.

Example 1:
Say, there is a requirement for an online shopping portal where in, based on the current location of the active customer, a surprise discount percentage has to be offered during the Thanks Giving time.

The developer/ programmer has written this to achieve the requirement.

Var cutomerSurpriseDiscountPercentage=0;
Var customerLocation = string.Empty;
If(customerLocation == “New York”)
{
       cutomerSurpriseDiscountPercentage = 10;
}


How can we better this code?

Define an ENUM that holds the names of the locations to which the surprise discount s available

enum ValidSurpriseDiscountLocations{
NewYork = “New York”,
Detroit,
NewJersy = “New Jersy”
}

Thus, our code is little modified for a better quality


Var cutomerSurpriseDiscountPercentage=0;
Var customerLocation = string.Empty;
If(customerLocation == ValidSurpriseDiscountLocations. NewYork)
{
       cutomerSurpriseDiscountPercentage = 10;
}


See you again.

In our previous post episode -1, we spoke on how we can make our variable names can talk and all of them put together can tell their story on their own.

But if we see, in our previous code snippet, we can improve it much more than what it is now. Lets go get it here...
var customersBilledThisMonth;

//fill the list here


for(int currentCustomerNode=0; currentCustomerNode<customersBilledThisMonth.count;
                  currentCustomerNode++)
{
        if(customersBilledThisMonth[currentCustomerNode].billedAmount>750)
       {
             Console.WriteLine(customersBilledThisMonth[currentCustomerNode].customerName);
        }

}
We can see a number in the code, right? Hey, what is this? Have a look at it a little closely...

Yeah!! I get it. It says if the value billedAmount is greater than 750, we enter the IF-condition. 
Okay, now let us make our code better. Let it talk to us on its own.

var customersBilledThisMonth;

//fill the list here

var currentMonthTotalBillAmount = 750;

for(int currentCustomerNode=0; currentCustomerNode<customersBilledThisMonth.count;            currentCustomerNode++)
{
        if(customersBilledThisMonth[currentCustomerNode].billedAmount> currentMonthTotalBillAmount)
       {
             Console.WriteLine(customersBilledThisMonth[currentCustomerNode].customerName);
        }

}





That's it? Not really.
Lets make it little more readable and story telling thing. What if the client want this value to be changed to $900 next month? Open the project, change the code, build and deploy again? NO. So what we do is make it configurable.

Add the below code in config.cs
<appSettings>
<add key ="currentBillingAmount" value = "750"/>
</appSettings>
change the value assigned to the variable as below
var currentMonthTotalBillAmount = ConfigurationSettings.AppSettings("currentBillingAmount");

Now, add the string that's hard coded into an enum file or a constants file, so that if the name spelled at different areas stay the same and there are no typos.

Observed the change? Now its more easy to understand what the code is doing?

There are many more ways than just this. This is one way of making our code do the right thing than just deliver the mere functionality.

 Yes, its telling its story. Will talk more in the next episode.