В C# действительно ли возможно бросить Список <Ребенок> для Списка <Родитель>?

[NSFileManager fileExistsAtPath:isDirectory:]

Returns a Boolean value that indicates whether a specified file exists.

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

Parameters
path
The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

isDirectory
Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn’t exist, the return value is undefined. Pass NULL if you do not need this information.

Return Value
YES if there is a file or directory at path, otherwise NO. If path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file or directory at the link destination.
50
задан Matthew 18 April 2018 в 11:32
поделиться

2 ответа

Прямое приведение типов запрещено, потому что нет способа сделать его типизированным. Если у вас есть список жирафов и вы поместите его в список животных, вы можете добавить тигра в список жирафов! Компилятор не остановит вас, потому что, конечно, тигр может войти в список животных. Единственное место, где компилятор может вас остановить, - это небезопасное преобразование.

В C # 4 мы будем поддерживать ковариацию и контравариантность интерфейсов SAFE и типов делегатов, параметризованных ссылочными типами. Подробности см. Здесь:

http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

61
ответ дан 7 November 2019 в 10:36
поделиться

Использование LINQ:

List<Parent> parentList = childList.Cast<Parent>().ToList();

Документация для Cast <> ()

74
ответ дан 7 November 2019 в 10:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: