38

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

1
  • 9
    Usually .cs files are in a project, and the project will tell you what framework it should be built with. I would worry about looking at .cs files in isolation like that... Commented Jul 23, 2018 at 19:12

3 Answers 3

35

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://learn.microsoft.com/en-us/dotnet/standard/frameworks

9

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.

1
  • I should add that this is a method to use going forward especially with shared code used between NetFramework and Core Commented Jul 23, 2018 at 20:21
8

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.