【Unity】(C#) Use the simplest program to help novices understand the usage of Func<T,TResult>

[Unity] (C#) Use the simplest program to help novices understand the usage of Func<T,TResult>

using System;
using UnityEngine;

public class Test : MonoBehaviour
{
    
    
    Func<int, string> selector = str => (str + 1).ToString();

    // Start is called before the first frame update
    void Start()
    {
    
    
        Debug.Log(selector(1));
    }
}

Insert image description here
How about it? Isn’t it very simple? Here’s another enumeration version.

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

public class Test : MonoBehaviour
{
    
    
    Func<string, string> selector = str => (str + "1");

    // Start is called before the first frame update
    void Start()
    {
    
    
        string[] strings = {
    
     "1", "2", "3", "4", "5", "6", };
        IEnumerable<string> meiju = strings.Select(selector);
        foreach (string s in meiju)
        {
    
    
            Debug.Log(s);
        }
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44099953/article/details/132623403