TryGetValue pattern with C# 8 nullable reference types

If you’re arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern. Returning a swift-style nullable reference type works well, but the try pattern lets you return things … Read more

How does GetValueOrDefault work?

thing isn’t null. Since structs can’t be null, so Nullable<int> can’t be null. The thing is… it is just compiler magic. You think it is null. In fact, the HasValue is just set to false. If you call GetValueOrDefault it checks if HasValue is true or false: public T GetValueOrDefault(T defaultValue) { return HasValue ? … Read more

Why does a `null` Nullable have a hash code?

The point here is that int? i = null; does not create a variable i which is null, but (by performing an implicit cast) a Nullable<int> instance which does not have a value. This means the object/instance is not null (and as Nullable<T> is a struct/value type it actually can’t be null) and therefore has … Read more

C# compiler throws Language Version (LangVersion) reference error “Invalid ‘nullable’ value: ‘Enable’ for C# 7.3”

In my case, I ran into this problem with Visual Studio 2022 when I changed the target framework from .NET Standard 2.1 to .NET Standard 2.0. I solved my problem by removing <Nullable>enable</Nullable> in the .csproj file and restarting Visual Studio. Original .csproj file: <PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <Nullable>enable</Nullable> </PropertyGroup> New .csproj file: <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup>

Can I make an embedded Hibernate entity non-nullable?

Embeddable components (or composite elements, whatever you want to call them) usually contain more than one property and thus are mapped to more than one column. The entire component being null can therefore be treated in different ways; J2EE spec does not dictate one way or another. Hibernate considers component to be NULL if all … Read more

How to specify null prop type in ReactJS?

It is possible to use PropTypes.oneOf([null]).isRequired. It should allow null, and nothing else. You can combine that with any other type: PropTypes.oneOfType([ PropTypes.string.isRequired, PropTypes.oneOf([null]).isRequired, ]).isRequired Edit: I just had this prop type fail for me when given a null prop using prop-types 15.7.2, so I’m not sure this works anymore (if it ever did?). I … Read more