C
C#2y 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
ero2y ago
ElementAt is an extension method on IEnumerable<T>
LastExceed
LastExceedOP2y ago
i know, what about it?
ero
ero2y 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
LastExceedOP2y ago
then why does it give a warning in the first example ?
ero
ero2y ago
some var shenanigans i assume
LastExceed
LastExceedOP2y ago
thats not very helpful
Thinker
Thinker2y ago
Hold on what is your actual question? Why disabling nullable... disables nullable? oh nvm, now I see it That is extremely odd
LastExceed
LastExceedOP2y ago
coworker and i lost our minds over this yesterday, im on the edge of reporting this as a bug
Thinker
Thinker2y ago
You could try asking in #roslyn
LastExceed
LastExceedOP2y ago
will do
ero
ero2y ago
ero
ero2y ago
can't repro
Thinker
Thinker2y ago
Wasn't it in another project?
LastExceed
LastExceedOP2y ago
correct
ero
ero2y ago
no clue how you gathered that from the information provided lol
LastExceed
LastExceedOP2y ago
but if we move GetList to another project that doesn't use nullables:
in the op
ero
ero2y ago
same diff
LastExceed
LastExceedOP2y ago
what?
ero
ero2y ago
ero
ero2y ago
ah wait
ero
ero2y ago
ero
ero2y ago
forgot the using System.Linq
MODiX
MODiX2y 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
Thinker2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
333fred
333fred2y 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
ero2y ago
I was confused about that too
Accord
Accord2y 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