Why can't I declare a MemoryStream with a null value (MemoryStream?), When can a function return a MemoryStream?

I have a function that returns MemoryStream?. Therefore, null if an error occurs. Then I found that I could not declare a variableMemoryStream?

public MemoryStream? GetResponseStream() { }
MemoryStream? stream = GetResponseStream();

The type "System.IO.MemoryStream" must be an unimaginable value type in order to use it as the "T" parameter in the generic type or method "System.Nullable"

+3
source share
5 answers

MemoryStream ( class) . ( struct) NULL, ?.

, :

public MemoryStream GetResponseStream() { ... }

:

MemoryStream stream = GetResponseStream();
if (stream == null) { ... }

BTW: , , , GetResponseStream null.

+16

MemoryStream , . Nullable<T> , .

+1

, . A MemoryStream , .

0

No need ?, as reference types can be null.

public MemoryStream GetResponseStream()
{
    return(null);
}
0
source

A modifier with a zero value (?) Is used only for value types. A stream is a type of object that can always be set to null (by its nature, it is already "nullified"). Therefore, you do not need to do what you are trying to do.

0
source

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


All Articles