Sunday, 22 May 2016

Overloading

Overloading

          Just as a reminder, overloading is what happens when you have two methods with the same name but different signatures. At compile time, the compiler works out which one it's going to call, based on the compile time types of the arguments and the target of the method call. 
Category wise operators

           Now, things can get a little bit confusing sometimes when it comes to resolving overloads... especially as things can change between versions. This article will point out some of the things you might run into... but I'm not going to claim it's an authoritative guide to how overloading is performed. For that, read the specification - but be aware that you may get lost in a fairly complex topic. Overloading interacts with things like type inference and implicit conversions (including lambda expressions, anonymous methods and method groups, all of which can become tricky). All specification references are from the C# 4 specification.

This article is also not going to go into the design choices of when it's appropriate and when it's not. I'll give a little advice about when it might be potentially very confusing to use overloading, but anything beyond that will have to wait for another time. I will say that in general I believe overloading should be used for convenience, usually with all overloads ending up calling one "master" method. That's not always the case, but I believe it's the most common scenario which is appropriate.


In each example, I'll give a short program which will declare some methods and call one - and then I'll explain what gets called in which version of C#, and why. As I'm not trying to focus on the design decisions but merely the mechanical choices the C# compiler makes, I haven't tried to make the examples do anything realistic, or even given them realistic names: the overloaded method is always Foo, and it will always just print its own signature. Of course the action taken is irrelevant, but it makes it easier to grab the code and experiment with it if you want to.

Simple cases

Using System;
Class Test
{
    Static void Foo(int x)
    {
        Console.WriteLine("Foo(int x)");
    }
    Static void Foo(string y)
    {
        Console.WriteLine("Foo(string y)");
    }
    Static void Main()
    {
        Foo ("text");
    }
}
using System;
class Test
{
    static void Foo(int x)
    {
        Console.WriteLine("Foo(int x)");
    }
 static void Foo(double y)
    {
        Console.WriteLine("Foo(double y)");
    }
     static void Main()
    {
        Foo (10);
    }
}


operator Overloading Example

This time, Foo(int x) will be printed. Both methods are applicable - if we removed the method taking an `int`, the method taking a `double` would be called instead. The compiler decides which one to pick based on the better function member rules (section 7.5.3.2) which look at (amongst other things) what conversions are involved in going from each argument to the corresponding parameter type (int for the first method, double for the second). There are more rules (section 7.5.3.3) to say which conversion is better than the other - in this case, a conversion from an expression of type int to int is better than a conversion from int to double, so the first method "wins".

Multiple parameters

When there are multiple parameters involved, for one method to "beat" another one it has to be at least as good for each parameter, and better for at least one parameter. This is done on a method-by-method comparison: a method doesn't have to be better than all other methods for any single parameter. For example:
Difference between overloading and overriding

using System;

class Test
{
    static void Foo(int x, int y)
    {
        Console.WriteLine("Foo(int x, int y)");
    }

    static void Foo(int x, double y)
    {
        Console.WriteLine("Foo(int x, double y)");
    }

    static void Foo(double x, int y)
    {
        Console.WriteLine("Foo(double x, int y)");
    }
   
    static void Main()
    {
        Foo(5, 10);
    }

}


Tuesday, 17 May 2016

.Net Framework Evolution

.Net Framework Evolution


Microsoft .NET Framework is an evolving platform and each year, it gets better and better. If you look at the following image, you will see in the past 15 years how .NET has evolved.



The first release, .NET Framework 1.0, was introduced in 2002 and since then, as you can see, it has evolved. Every new version of .NET has introduced many new technologies and has improvements in existing technologies.
.NET Framework is still evolving…
When .NET Framework was first introduced, it supported limited applications. It started with managed and unmanaged applications but the managed area was mainly focused around Win Forms and ASP.NET. Today, if you look at the architecture of .NET Framework in the following image, you will see there is much more added to the framework, including support of JavaScript and other libraries.


Image courtesy: Microsoft


.NET application types
When .NET Framework was first introduced, it supported limited applications. Today, you can build almost any kind of app using .NET framework. Here are some of the application types we can build using .NET framework:
  • Windows client applications (WPF, Windows Forms)
  • Windows Store Apps
  • Universal Windows Apps using UWP for Windows Phone, Windows 10, HoloLens, Surface, XBox other Microsoft devices
  • Web applications using ASP.NET and/or HTML 5/JavaScript/CSS3
  • Android Mobile apps
  • Apple iOS apps
  • Cross-platform responsive web applications using HTML 5/JavaScript and CSS
  • Type Script to build modern object based JavaScript applications
  • Enterprise applications
  • WCF and Web API services
  • SharePoint, Office, SQL Server and cloud enabled apps
  • Azure Micro services and Azure enabled apps
  • Components and libraries
  • 2D and 3D games
  • And many more
As you can see from the above list, the possibilities are endless.
Languages and .NET
C#, C++, VB.NET, and F# seem to be the most supported and used languages to build .NET applications, but there are close to 50 languages that are supported by the .NET framework. Some of these languages were developed by Microsoft and some were developed by other companies. The .NET Framework supported languages including Ada, APL, AsmL, BETA, Boo, C, C++, C#, Caml, CAT, COBOL, CULE, Eiffel, FORTRAN, Haskell, IL/MSIL, Java, JavaScript, Lexico, LISP, LOGO, Lua, Mercury, Mixal Assembly Language, Modula-2, Mondrian, Oberon, Memerle, Pan, Perl, Pascal, PHP, Processing, Prolog, Python, R, Ruby, RPG, Scala, Scheme, Smalltalk, SML, Synergy, Tcl/Tk, Visual Basic, and XAML.
When we talk about .NET, it’s unfair not to talk about C# and Roslyn. Roslyn is the .NET Compiler Platform that provides open-source C# and Visual Basic compilers with rich code analysis APIs.
Microsoft has just announced C# 7 with a few new features. Check out What Is The Future Of C#. Here is a good article that talks about What C# Can Do For You and that also means .NET framework.
Similar to .NET, C# is now open source and evolving. The following table shows the improvements made in each newer version of the language.



As you can see, unlike other languages, C# is a continuous work. Now that C# is open source, the community is growing strong and more features are being finalized for future versions.
Universal Windows Platform
Universal Windows Platform (UWP) is a single platform to build all kind of applications for Windows 10 including Devices, IoT, Desktop, Holographic, Xbox, Console, and Surface Hub.


Image courtesy: Microsoft
.NET is the core technology to build UWP apps. And UWP is just getting started. As a part of .NET, you can use C#, VB.NET, F#, or VC++ to build UWP applications. Check out C# Corner’s Universal Windows Platform category to learn more about UWP.
.NET Core is Open Source
In 2014, Microsoft open sourced .NET and has slowly open sourced most of .NET components including compilers, languages, libraries, and tools. Most of the .NET open sourced components are available in Github now.
https://dotnet.github.io/
The new .NET is named .NET Core, which is open source, and now developers are embracing what Microsoft is doing with its .NET framework. Companies likeXamarin and many others are joining hands with Microsoft.
Conclusion

The .NET Framework, which is now open source, is an evolving platform. Not only is .NET open source, but it is managed by a well-managed body named the .NET Foundation. The .NET Foundation’s team comes from a strong open source background and has vast experience in building and managing several large, long-term, and successful projects. As the industry’s need grows or changes, .NET framework will continue to evolve based on the needs.

Web API OData


Microsoft just released version 5.9 of Web API OData, announced by Sam Xu via a blog post. The release is available to download on NuGet.

This package contains everything you need to create OData v4.0 endpoints using ASP.NET Web API and to support OData query syntax for your web APIs.

Saturday, 14 May 2016