Understanding .NET Core, .NET Standard and .NET Framework

Introduction

There have been major changes in the .NET world with the introduction of .NET Core and .NET standard. As a .NET developer we were never ever so confused before but with .NET core and .NET standard i have seen many developers confused with these frameworks and when to use where specially when in the job market there seems to be sudden jump for .NET core as one of the required skills. So in this article i will clarify about .NET Core, .NET standard and .NET framework. We will also create a small application to use all these things and see how they work together.

What is .NET Framework?
I hope you know the answer. .NET framework has been in the industry for around 15 years and has grown and became very popular in the dev community but these framework was completely focused on Windows environment. So you need Windows to do the development and Windows to run your application. It was totally Windows thing.

What is .NET Core?
Microsoft has done very right thing to introduce .NET core. Now .NET core is not exactly .NET framework but has many features of it. In .NET Core you can actually develop and run your application in any supported platforms. Like your code can now run on Linux. So .NET core is platform independent. You develop your application in a similar way as you were doing it with .NET framework but now once you publish your code you can deploy it on Linux and run it. That is actually very good where many organisation who wanted to leverage the performance of Linux but couldn’t do it with .NET framework applications.

What is .NET Standard now?
If you have done lot of coding in .NET space then you would be well aware of the fact that there are many Class projects we create to separate the logic and reuse them. For eg If your big and complex applications uses logging then you would have a separate Logger class library project which you would reference and use it in different parts of your application. .NET standard does the same. It is meant only to create class library projects but these projects can be used by .NET core as well as .NET Framework. So your common libraries can be used with .NET Framework Win forms application (as of now .NET core does not support Winforms project) and .NET Core console application. Below diagram will give you the complete picture

.NET Core .NET Standard .NET Framework
.NET Core .NET Standard .NET Framework

So now lets start the sample application type. We will create two main projects one in .NET framework and other in .NET Core. These projects will reference a class library written in .NET Standard. With this small sample application you will get an idea of how to use different .NET versions.

I have used VS 2017 latest version for this article. I would suggest you to grab the Community Edition of Visual studio 2017 from Microsoft. Community Edition is free. That’s great.

1. .NET Framework application

Create a new Project in Visual Studio. See the screenshot below and you will notice it is a console app targeting .NET framework. So this application will only work on Windows

.NET Framework Project
.NET Framework Project

2. .NET Core Application

Once above step done is done. Add new project in your solution and this will be a .NET Core application. See below screenshot

.NET Core Project
.NET Core Project

3. .NET Standard Project.

Now we will add a new project in the solution and this will be in .NET Standard. In below screenshot you will see that in .NET standard you can only create Class Library projects

.NET Standard Project
.NET Standard Project

4. Now our Solution explorer would look like this

Solution Explorer
Solution Explorer

5. Adding Reference

Now we will add the reference of the TutorialLib .NET Standard Class library project in Tutorial (.NET Framework) and TutorialCore (.NET Core) projects. Right click on these projects and Click on Add Reference and select TutotualLib project.

6. Coding

Now we will take input from the user and find the length of the input string. Its a very very basic application but the intent of this article is give you an understanding of .NET Core and .NET standard before going into the deep dive. This is a test drive

We will add a new method GetLength in .NET Standard Class1 which will return the length of the input string

using System;

namespace TutorialLib
{
public class Class1
{
public int GetLength(string input)
{
return input.Length;
}
}
}

Our .NET Framework and .NET Core Program.cs file will look like this

using System;
using TutorialLib;
namespace TutorialCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is in .NET Framework");
            Console.WriteLine("Enter your string");
            var input = Console.ReadLine();
            var cls = new Class1();
            var length = cls.GetLength(input);
            Console.WriteLine($"Length of Input string is {length}");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

        }
    }
}
using System;
using TutorialLib;
namespace TutorialCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is in .NET Core");
            Console.WriteLine("Enter your string");
            var input = Console.ReadLine();
            var cls = new Class1();
            var length = cls.GetLength(input);
            Console.WriteLine($"Length of Input string is {length}");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

        }
    }
}

Now in Visual Studio you can run either of the main application see the output.

I hope this article has given you an understanding of .NET Framework, .NET Core and .NET standard and how these work together.

In the next article (not yet published) i will create a complex application and architecture using different .NET versions and run them in Windows as a Service and in Linux as a Daemon. I will use similar kind of layered architecture to demonstrate how to run .NET Core application under Linux and creating Common library projects in .NET standard which will be used by .NET Core and .NET Framework applications.

Happy reading.

See you soon.

Any suggestions please drop a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *