Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if code is written for regular .NET or .NET Core?

Tags:

c#

.net

.net-core

I work with a code base that contains some code in regular .NET and some code in .NET Core. When I open an individual .cs file, I'm not always sure whether the file was meant to be compiled with regular .NET or .NET Core. Obviously, there's a lot of overlap between both frameworks -- and a lot of code can be run unmodified in both frameworks.

So my question is, what are some easy ways to determine whether a .cs file is intended to be compiled for regular .NET or .NET Core?

(I imagine that looking for certain usings that only exist in one framework or the other is probably the biggest telltale sign. If that is indeed the way to determine this, is there a web page which lists which usings are exclusive to regular .NET vs. .NET Core?)

like image 560
Andr Avatar asked Jul 23 '18 19:07

Andr


People also ask

How do I know if I have .NET or .NET Core?

. Net Core does not support desktop application development and it rather focuses on the web, windows mobile, and windows store. . Net Framework is used for the development of both desktop and web applications as well as it supports windows forms and WPF applications.

What is the difference between .NET and .NET Core?

NET framework helps you build web apps, desktop apps, and web services. It works only on the Windows operating system. On the other hand, . NET core is for creating cross-platform cloud apps that run on Windows, Mac, and Linux.


3 Answers

Your best bet is to look at the .csproj file.

Look for either the <TargetFramework> or the <TargetFrameworks> element. It will have entries such as net461. You can cross reference with the chart here:

https://docs.microsoft.com/en-us/dotnet/standard/frameworks

like image 121
RQDQ Avatar answered Sep 19 '22 06:09

RQDQ


Microsoft has a Portability Analyzer that will tell you if your code will run on various platforms and what kind of changes are required, but the only way I know to tell what platform particular code was written for is to check the project properties or makefile.

like image 45
Terry Carmen Avatar answered Sep 20 '22 06:09

Terry Carmen


You could also use an if preprocessor directive such as something like this:

public class MyClass
{
   static void Main()
   {
#if (NETCOREAPP1_0 || NETCOREAPP1_1 || NETCOREAPP2_0 || NETCOREAPP2_1)
        <some code>
#else
        <some code>
#endif
   }
}

I should add that this is a method to use going forward especially with shared code used between NetFramework and Core.

like image 30
liquidanswer Avatar answered Sep 19 '22 06:09

liquidanswer