C
C#14mo ago
LastExceed

❔ null warning while indexing (rather than accessing) null-agnostic type?

first, lets look at a scenario where everything behaves as expected:
string[]? GetList() => throw new NotImplementedException();

var myList = GetList();
string a = myList.ElementAt(0); //null warning while accessing `myList` - as expected
string b = myList[0]; //same thing here - as expected
string c = myList[0]!; //this doesn't change anything, because its not the indexer but the variable that is nullable - as expected
string c = myList![0]; //this successfully suppresses the warning - as expected
string[]? GetList() => throw new NotImplementedException();

var myList = GetList();
string a = myList.ElementAt(0); //null warning while accessing `myList` - as expected
string b = myList[0]; //same thing here - as expected
string c = myList[0]!; //this doesn't change anything, because its not the indexer but the variable that is nullable - as expected
string c = myList![0]; //this successfully suppresses the warning - as expected
but if we move GetList to another project that doesn't use nullables:
#nullable disable //basically any project up until C# 7.3
namespace Foo;

public static class Stuff
{
public static string[] GetList() => throw new NotImplementedException();
}
#nullable disable //basically any project up until C# 7.3
namespace Foo;

public static class Stuff
{
public static string[] GetList() => throw new NotImplementedException();
}
now we get the following behaviour:
using Foo;

var myList = Stuff.GetList();
string a = myList.ElementAt(0); //no warning at all, despite having configured interpretation of null-agnostic types to be pessimistic - weird but whatever
string b = myList[0]; //here we DO get a null warning (dunno why this makes a difference), however its not while accessing the variable (where i would expect one), but while INDEXING it - wtf?
string c = myList[0]!; //this suppresses that warning - ok, but why was it there in the first place?
string d = myList![0]; //this doesn't.
using Foo;

var myList = Stuff.GetList();
string a = myList.ElementAt(0); //no warning at all, despite having configured interpretation of null-agnostic types to be pessimistic - weird but whatever
string b = myList[0]; //here we DO get a null warning (dunno why this makes a difference), however its not while accessing the variable (where i would expect one), but while INDEXING it - wtf?
string c = myList[0]!; //this suppresses that warning - ok, but why was it there in the first place?
string d = myList![0]; //this doesn't.
can someone explain?
29 Replies
ero
ero14mo ago
ElementAt is an extension method on IEnumerable<T>
LastExceed
LastExceed14mo ago
i know, what about it?
ero
ero14mo ago
that's why it doesn't generate a warning
however its not while accessing the variable
you aren't accessing myList in that call you're passing myList to Enumerable.ElementAt
LastExceed
LastExceed14mo ago
then why does it give a warning in the first example ?
ero
ero14mo ago
some var shenanigans i assume
LastExceed
LastExceed14mo ago
thats not very helpful
Thinker
Thinker14mo ago
Hold on what is your actual question? Why disabling nullable... disables nullable? oh nvm, now I see it That is extremely odd
LastExceed
LastExceed14mo ago
coworker and i lost our minds over this yesterday, im on the edge of reporting this as a bug
Thinker
Thinker14mo ago
You could try asking in #roslyn
LastExceed
LastExceed14mo ago
will do
ero
ero14mo ago
ero
ero14mo ago
can't repro
Thinker
Thinker14mo ago
Wasn't it in another project?
LastExceed
LastExceed14mo ago
correct
ero
ero14mo ago
no clue how you gathered that from the information provided lol
LastExceed
LastExceed14mo ago
but if we move GetList to another project that doesn't use nullables:
in the op
ero
ero14mo ago
same diff
LastExceed
LastExceed14mo ago
what?
ero
ero14mo ago
ero
ero14mo ago
ah wait
ero
ero14mo ago
ero
ero14mo ago
forgot the using System.Linq
MODiX
MODiX14mo ago
LastExceed#8727
ok turns out i made 2 mistakes at the same time: 1) i forgot that not only the array itself, but also the items inside the array can be null 2) i tried
var x = myList[0];
var y = myList.ElementAt(0);
var x = myList[0];
var y = myList.ElementAt(0);
and wondered why there's a null warning on the first line but not the second. i forgot that a variable only needs to be null checked once, so the warning will only show once as well. if you comment out the first line the warning will show on the second instead thats embarrasing
Quoted by
<@!173538718886395906> from #roslyn (click here)
React with ❌ to remove this embed.
Thinker
Thinker14mo ago
$close
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
333fred
333fred14mo ago
Also, to be clear, there is no "configuring interpretation of null agnostic types to be pessimistic" That's not a thing that exists
ero
ero14mo ago
I was confused about that too
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts