top of page
Search
galedisnant1987

Reminder: Handling Nullable Types in LINQ - Avoiding NullReferenceException



In this post, we have seen that APIs that do not yet use nullable reference types may lead to incorrect code analysis, thus compromising null safety. ReSharper and Rider recognize such APIs and uses JetBrains.Annotations, if available, to ensure null safety.




Reminder: Handling Nullable Types in LINQ



Though this question has been already answered but I would like to give my suggestion to use Nullable types if you are using .Net 2.0 because for the same problem Nullable types were introduced in .Net 2.0 and provides the true safety when working with the database where a columns type is nullable but C# types do not support to have null values in them because they are value types.


This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.2Reference parametersThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.3Output parametersThis method helps in returning more than one value.C# - NullablesC# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.


The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.


Recently nullable reference types have become trendy. Meanwhile, the good old nullable value types are still here and actively used. How well do you remember the nuances of working with them? Let's jog your memory or test your knowledge by reading this article. Examples of C# and IL code, references to the CLI specification, and CoreCLR code are provided. Let's start with an interesting case.


Note. If you are interested in nullable reference types, you can read several articles by my colleagues: "Nullable Reference types in C# 8.0 and static analysis", "Nullable Reference will not protect you, and here is the proof".


Here we will look at the basics of nullable value types in general: what they are, what they are compiled into in IL, etc. The answer to the question from the case at the very beginning of the article is discussed in the next section.


Edit: I see I was misunderstood. I understand and agree with the advantages of avoiding null pointers. I'm not talking about arbitrary pointers that accept null. I'm only asking why not use compile-time metadata, like C# 8's nullable reference types and TypeScript with strict null checks, where default pointers can't be null and there's a special syntax (mostly ?) to indicate a pointer that can accept null.


In the case of C# the language, the type system is sufficiently complex to support an Optional type allowing value types to gain a nullable state, but there isn't a trivial way to remove nullability from the object references.


Some systems prefer to have very simple primitive types, and rely on a powerful type composition system to create the desired behaviours. In these languages a C# nullable Reference might look like def cs_reference(T) => NULL T. The Optional pattern makes more sense though in these languages: def Option(T) => T[0..1]. Its an array/list/sequence of 0 or 1 element.


Dictionaries are just an example, the problem arise anywhere you have a data-structure with multiple levels of optional elements. Nullable types prevent polymorphism: code can't manipulate data of an unknown type generically, it has to treat nullable and non-nullable types differently.


Those types of static checks are a relatively recent invention. They were invented in response to options, as a way to pull in the benefits of options without the memory overhead and with a more familiar syntax. So a big part of the reason why more languages don't use statically-checked nullables is because options were invented first.


This is incorrect. Nullables have exactly the same overhead as option types in Rust, and overall the overhead can go either way depending on the language design. You can have option types with no overhead over nullables, and you can have nullables with overhead over option types.


In Rust, Option is represented by a nullable pointer. If anything, Option is more efficient than some languages with null because Option lets you represent optionals as stack based value types, whereas in languages with null these optionals need to be heap-allocated so that null can be used.


Whether Nullables or Options are better is a hotly debated issue among programming language enthusiasts. There is nothing objectively superior about Option types, just certain use cases it happens to excel at. Likewise for nullables; it's situational.


Semantically, Options and nullable types are pretty similar. Option and T? work pretty much the same. There are some differences like explicit Some and methods that operate on options. But you indicate that's not what you are interested in. Rather, you seem more interested in the implementation detail of using null pointers rather then some sort of enum.


Another way of looking at this, most types in Rust are value types not reference types. Thus nullable versions of those types couldn't be implemented as null pointers. They'd have to implemented as a value with a flag, which is how enums are implemented.


Furthermore, this is a relatively obvious optimization. I suspect that all languages with optional types that care about performance will ensure it gets implemented as a null pointer when that is suitable. So, there is no performance reason to shy away from Optional types in favor of nullables.


Although one important improvement to LINQ queries has the possibility of breaking many applications, there were a number of changes made to improve LINQ queries overall. The driver was to fix problems related to evaluating part of queries on the client. The original implementation was dependent on re-linq, which provides higher-level abstractions of LINQ expression trees. But this was preventing the EF team from making the types of changes required to fix the evaluation problems. So re-linq was removed and everything broken by this removal needed to be rewired. Let's take a look at the client evaluation problem that started this important transition.


C# 8 brings us some new super cool features and EF Core 3.0 is there for some of them. For example, support for asynchronous streaming, thanks to await foreach and the standard IAsyncEnumerable interface.Nullable and Non-Nullable Reference Types, also new to C#8, are supported by EF Core 3.0 as well. This C#8 feature allows you to explicitly signal permission (or forbid) to allow nulls into reference types like string. EF Core recognizes nullable reference types. This works for mappings and queries but not yet for scaffolding from an existing database


The type is recognized by EF Core. A relevant column is created for the database table. And queries handle the nullable reference type as well, such as in the following query. Being new to nullable reference types, I expected to use p.Middle.HasValue() in the query but nullable reference types don't have the same behaviors as nullable value types like int?.


In C# with nullable reference types the null-coalescing operator ?? already implements the desired functionality. (If you're using another language or an older version of C#, you can instead use Maybe.)


I added the [Required] attribute to the ReservationDto class' Email property. Since this code base also uses nullable reference types, it was necessary to also annotate the property with the [NotNull] attribute:


This code snippet demonstrates how to parse, not validate, an incoming Data Transfer Object (DTO). This code base uses C#'s nullable reference types feature to distinguish between null and non-null objects. Other languages (and earlier versions of C#) can instead use the Maybe monad. Nothing in this article or the book hinges on the nullable reference types feature.


Instead of basing the design on nullable reference types or the Maybe monad, you can instead base parsing on applicative validation. In order to explain that, I'd first need to explain functors, applicative functors, and applicative validation. It might also prove helpful to the reader to explain Church encodings, bifunctors, and semigroups. That's quite a rabbit hole to fall into, and I felt that it would be such a big digression from the themes of the book that I decided not to go there.


So what is the right thing to do here? The solution is to write your own extension method version of TryParse the way it would have been written had there been nullable value types available in the first place: 2ff7e9595c


0 views0 comments

Recent Posts

See All

USB Joystick Center 2018 APK

APK do USB Joystick Center 2018: como usar qualquer joystick ou gamepad no seu dispositivo Android Introdução Se você é um fã de jogos em...

Baixe Cluedupp Geogames

CluedUpp Geogames: uma maneira divertida e imersiva de explorar sua cidade Você está procurando um dia diferente? Você quer se divertir,...

Comments


bottom of page