I'm playing around with lambdas and trying to pass arguments to them. This works fine: Action<string> logString = (strVal) => { Debug.Log(strVal); }; However, Action<T> throws the error ' The type or namespace name `T' could not be found.' I've seen Action<T> used in several examples online. What is it that I'm not getting, something relating to the use of generics? The Action type? I can just carry on using the <string> type for now, but any help understanding what I'm missing would be much appreciated.
I think in order for T to work you have to create a function like: Code (CSharp): public void Log<T>(T value) { Action<T> log = (arg) => {Debug.Log(arg);}; log(value); } Correct me if I'm mistaken. EDIT: Fixed typo.
The T has to come from somewhere - either from the containing method, or from the containing class. So you could use an Action<T> like this: Code (CSharp): void PerformActionOn<T>(T obj) { Action<T> action = x => Debug.Log(x); action(obj); } And then call it like this: Code (CSharp): void Example() { int intVar = 5; string stringVar = "five"; PerformActionOn(intVar); PerformActionOn(stringVar); } A much more usefull example is a method that takes the action, and applies it to something. Take the classic map function, which "maps" a function over a collection (ie. applies it to every element in the collection). You can implement that through using an Action, like this: Code (CSharp): void Map<T>(T[] array, Action<T> function) { for (int i = 0; i < array.Length; i++) { function(array[i]); } } and you could use it like this: Code (CSharp): void MoveAllInArrayUp(Transform[] array) { Action<Transform> moveAction = x => x.position = x.position + Vector3.up; Map(moveAction, array); } Note that you have to type out the type of the T parameter when you declare the Action. Also note that using the letter T is completely arbitrary - it's short for "Type", but you could just as well have used "Y" or "D" or even full words like "Lollipop" or "dizIzZparta". You can read more about Action<T> here. I'd suggest looking into Func<TResult> here and Func<T, TResult> here. In short, where Action is a method that takes an argument of a type and returns nothing, the Func<TResult> method takes no arguments and returns and object of the type TResult, while Func<T, TResult> takes an argument of type T and returns and object of the type TResult. Hope that helps!
Cheers guys, very helpful. My mistake was thinking T was more readily available than it is, and I could just throw it in like id in Objective-C or * in ActionScript. Cheers again.
T means you can put whatever you want within the < > brackets. You could write, string, int, or anything else. So if you look at the List class for example, you'll see the name of the class is List<T> which means that when you do want to use List, you have to pass in a type to it like such List<string>, List<int> but it could really be anything.