3c5dd572ae2677cf8911d000536edbd4
38.Cast

LINQ Cast 简介

将 IEnumerable 的元素强制转换为指定的类型。

LINQ Cast 代码示例

/****************************************************************************
 * http://sikiedu.com liangxie
 ****************************************************************************/

using System.Collections;
using System.Linq;
using UnityEngine;

namespace UniRxLesson
{
    public class LINQCastExample : MonoBehaviour
    {
        private void Start()
        {
            var fruits = new ArrayList {"mango", "apple", "lemon"};

            var fruitNames =
                fruits.Cast<string>();

            // 等同于
            // var fruitNames = fruits.Select(fruit => fruit.ToString);

            foreach (var fruit in fruitNames)
            {
                Debug.Log(fruit);
            }
        }
    }
}

输出代码为:

mango
apple
lemon

UniRx Cast 示意图

top Created with Sketch.