Skip to main content
added 83 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a functionmethod. You can store a reference to a method in such a variable, change which method is stored in it, and execute the currently stored method at a later time, passing whatever data you have available at that time as arguments.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an string, an int and a float as parameters and returns an int. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. These two families of delegate templates cover almost all use-cases. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an string, an int and a float as parameters and returns an int. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. These two families of delegate templates cover almost all use-cases. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a method. You can store a reference to a method in such a variable, change which method is stored in it, and execute the currently stored method at a later time, passing whatever data you have available at that time as arguments.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an string, an int and a float as parameters and returns an int. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. These two families of delegate templates cover almost all use-cases. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());
deleted 4 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an intstring, an int and a float and another int as parameters and returns a stringan int. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. These two families of delegate templates cover almost all use-cases. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an int, a float and another int as parameters and returns a string. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an string, an int and a float as parameters and returns an int. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. These two families of delegate templates cover almost all use-cases. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());
added 115 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics forthat cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an int, a float and another int as parameters and returns a string. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics for the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an int, a float and another int as parameters and returns a string. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());

What you are looking for is a C# feature called delegates.

A delegate is a variable that holds a reference to a function. You can store a reference to a method in such a variable, change which method is stored in it, and execute the method at a later time.

The C# standard already implements some generics that cover the most common use-cases. For example the delegate Func<TResult> that can be used to hold a call to any function that takes no arguments but returns one. Or the delegate Action that represents a method that does not return anything. Both Action and Func also have variants with multiple additional parameters. For example a Func<string, int, float, int> can hold a function that takes an int, a float and another int as parameters and returns a string. Or a Action<GameObject, Transform> represents a method that has no return value but takes a GameObject and a Transform. So making your own delegate types is usually optional and only required if you want some extra type safety.

For example, if you want to have a script that is supposed to contain "canned calls" to multiple functions that return integers and can then return the sum of the return values when all those functions get executed could look like this:

class FunctionStack() {
    private List<Func<int>> functions = new List<Func<int>>();

    void AddFunction(Func<int> function)
    {
        functions.Add(f);
    }

    int GetResult()
    {
        int ret = 0;
        foreach(Func<int> function in functions) {
            ret += function();
        }
        return ret;
    }
}

Feeding that class with functions can be done via methods or via lambdas:

FunctionStack instance = new FunctionStack();

// add a lambda expression that returns 4
instance.AddFunction(() => 4); 
// add a lambda function that returns 7 + 3
instance.AddFunction(() => { return 7 + 3; });
// add a call to an existing function. 
// Note the missing () behind the function name.
instance.AddFunction(someObject.SomeMethodThatReturnsAnInteger);
// if you want to call an existing function with arguments, then you need to
// wrap those in a lambda:
instance.AddFunction(() => { return Mathf.FloorToInt(transform.position.x); });

// note that none of the functions above get executed until you do this:
Debug.Log(instance.GetResult());
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading