nullable-reference-types

Nullable reference types unexpected CS8629 Nullable value type may be null with temporary variables

纵饮孤独 提交于 2021-02-18 22:45:43
问题 In a C# 8 project, I am using nullable reference types and am getting an unexpected (or at least, unexpected to me) CS8629 warning, bool singleContent = x.DataInt != null; bool multiContent = x.DataNvarchar != null; if (singleContent && multiContent) { throw new ArgumentException("Expected data to either associate a single content node or " + "multiple content nodes, but both are associated."); } if (singleContent) { var copy = x.DataInt.Value; // CS8629 here newPropertyData.DataNvarchar = $

Specify NotNull If Method Returns At All

寵の児 提交于 2021-02-04 15:43:05
问题 I'm using the new nullable reference types from C# 8 and I was wondering if it is possible to indicate that a parameter passed in is not null if the method returns at all. I've found [NotNullIf] and [DoesNotReturnIf] but these appear to trigger off a method's return value and a specific bool parameter respectively. Here is what I currently have: public static bool RaiseIfNull([NotNullWhen(true)] object? thing) => RaiseIf(() => thing is null); public static bool RaiseIf(Func<bool> predicate) {

Function with return type of 'string' returns a nullable string (ie 'string?')

与世无争的帅哥 提交于 2021-01-28 05:20:31
问题 For an Azure Functions project I am using C# 8 with enabled nullable reference types. Since with AzureFunctions AppSettings are exposed via environment variables, which can be a string or null (i.e. they return string? ) I tried to encapsulate the logic of getting environment variables - and throwing an error if they're not set - into a separate method GetEnvVariable . As you can see, GetEnvVariable is implemented in three short lines. If the environment variable is not set (i.e it is null )

What is the default value of Non-Nullable reference types in C# 8?

拜拜、爱过 提交于 2020-12-30 07:28:23
问题 If I enable nullable reference types, what will be the value of the following string if I declare it like so? string text; 回答1: The value will be null . Bear in mind that the new system for nullable reference types will only warn you about problems, it will not give you an error , and that means that the code will still compile, with warnings. If you declare this class: public class Test { private string text; } You'll get this warning for your class: CS8618: Non-nullable field 'text' is

In C# 8, why does type inference on new expressions result in nullable references? [duplicate]

雨燕双飞 提交于 2020-12-25 01:31:48
问题 This question already has answers here : Why does Visual Studio Type a Newly Minted Array as Nullable? (3 answers) Closed 6 months ago . If I have the C# 8 code: class Foo {} And later: #nullable enable var bar = new Foo(); Then the type of bar is Foo? . This seems clearly incorrect, as a new expression can't return null . Why would bar be a nullable reference? I even looked up the Nullable Reference Type Specification, and found the following: Expressions that are never null The null state

How to solve “The issue with T?”/nullable constraint on type parameter?

久未见 提交于 2020-12-05 11:10:50
问题 I'm designing an interface in C# 8.0 with nullable enabled, targeting .Net Standard 2.0 (using the Nullable package) and 2.1. I am now facing The issue with T? . In my example, I am building an interface for a cache which stores Stream s/byte data, identified by a string key, i.e. the file system could by a trivial implementation. Every entry is additionally identified by a version, which should be generic. This version could for example be another string key (like an etag), an int or a date