10

I can't upgrade Net for now. I want to achieve this public SiteOptionModel? Site { get; set; } like bool?. But it says I should upgrade .Net but I am afraid, upgrading .Net will destroy quite a big project. Is there any way to achieve this?

public bool IsVisibleInGrid { get; set; }
public SiteOptionModel? Site { get; set; }
public bool? IsDeleted { get; set; }

LinQ query showing error not having SiteId

from truck in database.Truck
   where truck.CarrierId == carrierId
         && (truck.IsDeleted == null || truck.IsDeleted == false)
   orderby truck.Code
   select new CarrierDetailViewModel.TruckModel2
   {
       Id = truck.TruckId,

       Site = new CarrierDetailViewModel.SiteOptionModel{
            Id = (int)truck.SiteId,
            Name = truck.Site.Name,
            Code = truck.Site.Code
       }
   } ;

Error

Severity Code Description Project File Line Suppression State Error CS8370 Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.

sesamii seed
  • 659
  • 2
  • 8
  • 11
  • It would not destroy anything, it is backward compatible. – Deepak Mishra Apr 22 '20 at 07:04
  • Unless it is a struct... reference types are always nullable. Am I missing something? It can already be null? You don't need it to be postfixed with a ? – Davey van Tilburg Apr 22 '20 at 07:05
  • @DaveyvanTilburg I edited the question you can see the LINQ code because it is not siteID, it throws an error that null object. – sesamii seed Apr 22 '20 at 07:14
  • Is that compile time error? What's the specific error without using nullable reference type? – Deepak Mishra Apr 22 '20 at 07:14
  • @sesamiiseed Are you sure the null ref isnt thrown because truck.Site is being called without including Site frm your context? Can you be more specific in where the null ref is being thrown – Davey van Tilburg Apr 22 '20 at 07:18
  • @DaveyvanTilburg C# 8 introduced [nullable reference types](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#nullable-reference-types), starting from this version all reference types considered as non-nullable – Pavel Anikhouski Apr 22 '20 at 07:51

1 Answers1

10

The compiler is asking you to use a newer version of the language, not a newer version of .NET. Every version of .NET supports nullable references types. References could be null in any version of .NET, you just were not able to annotate that in C# until C# 8.0.

However, your project is configured for C# 7.3, you need to change the language version. Please refer to How to enable Nullable Reference Types feature of C# 8.0 for the whole project.

See also What is the difference between C# and .NET?

Once you have your project configured for C# 8.0 and with Nullable Reference Types enabled… Roslyn, the C# compiler, will understand nullability annotations and provide code analysis based on them.

And you will have to deal with null anyway. Which might mean null checks. Accessing a member of a null reference is still an NullReferenceException, even in C# 8.0. At least the static analysis will help you.

Refer to What is a NullReferenceException, and how do I fix it?.


On the odd chance that you actually need to run newer features on an old runtime (e.g. async/await in .NET 2.0), I might have a solution for you: Theraot.Core. Of which, full disclosure, I'm the author.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • 3
    I just added this to ```.csproj```. ... true 8.0 Red line is gone but I have a green line underneath the question mark – sesamii seed Apr 22 '20 at 07:23
  • @sesamiiseed that is a warning, it should appear in your "List of errors" window. I would expect it to be warning you about the possible `NullReferenceException`. I would also expect for a description of the warning to appear when you hover on it, along side a ligthbulb... Clicking on ligthbulb would provide possible automated fixes (if there are any). – Theraot Apr 22 '20 at 07:29
  • @sesamiiseed the static analysis enabled with C# 8.0 Nullable Reference Types is likely to show warning in other places. You might be interested in watching [Channel9](https://channel9.msdn.com) videos on the topic ([search](https://channel9.msdn.com/Search?term=C%23%208.0%20nullable%20references&lang-en=true)). – Theraot Apr 22 '20 at 07:32
  • @sesamiiseed Your response should be pointed as the answer. Note for those trying to resolve this issue. – lyndon hughey Sep 29 '22 at 15:06
  • From [C# language versioning](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version): This default choice also ensures you don't use a language that requires types or runtime behavior not available in your target framework. Choosing a language version newer than the default _can cause hard to diagnose compile-time and runtime errors_. – Vasiliy Zverev Jun 23 '23 at 04:19