Why? appear in IEnumerable <int>?. ToList ()?

I was looking at the code and I came across the following line of code:

List authorIntList = authorIds?.ToList();

In the above example, authorIds is IEnumerable. What is the purpose of ?the line of code above? I'm not sure I have ever seen this template before. What does it do and in what version of .NET was it implemented?

+4
source share
4 answers

" " - .. ? . - # 6.0. , authorIds null, / ToList() . null. , , List AuthorIntList = authorIds != null ? authorIds.ToList() : null;.

+10

"Null Conditional Operator" " null, , null". ,

List authorIntList = authorIds ? authorIDs.ToList() : null; //null coalescing

List authorIntList = null;
if(authorIDs != null) { authorIntList = authorIDs.ToList(); } //traditional if
0

, . # 6.0 .

, - :

var x= a?.b?.c?.d;

x null, a, b, c d null. d, . .

if(a!= null && a.b!= null && a.b.c!= null)
{
    x= a?.b?.c?.d;
}
0

Null correction Conditional statement https://msdn.microsoft.com/en-us/library/dn986595.aspx

I like to say that authorId is not null, then executing ToList () else returns null.

-1
source

Source: https://habr.com/ru/post/1665351/


All Articles