c# return method code example

Example 1: c# function return

// To create a function with a return value, note the variable
// type in front of the function name and add a return of the
// same type at the end.
static string lastFirst(string firstName, string lastName)
{
	string separator = ", ";
	string result = lastName + separator + firstName;
	return result;
}

Example 2: how to return a value in c#

void Start()
    {
        Debug.Log(Message()/*calling the function with its name*/);
    }

    string Message()/*you can make values to functions with () and {}*/
    {
        string message = "Hello world";
        return message;//this says which value will be returned
    }

Example 3: what is a return statement C#

/*return (C# Reference)
The return statement terminates execution of the method in which it appears and
returns control to the calling method. It can also return an optional value. If
the method is a void type, the return statement can be omitted.*\

//(Basically it means do nothing)

Example 4: how to call a method from a class in c#

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;
         
         if (num1 > num2)
            result = num1;
         else
            result = num2;
         return result;
      }
      
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

Example 5: c# delegate return value invoke

public void TestIndividualInvokesRetVal( )
{
    MultiInvoke MI1 = new MultiInvoke(TestInvoke.Method1);
    MultiInvoke MI2 = new MultiInvoke(TestInvoke.Method2);
    MultiInvoke MI3 = new MultiInvoke(TestInvoke.Method3);

    MultiInvoke All = MI1 + MI2 + MI3;

    int retVal = -1;

    Console.WriteLine("Invoke individually (Obtain each return value):");
    foreach (MultiInvoke individualMI in All.GetInvocationList( ))
    {
        retVal = individualMI( );
        Console.WriteLine("\tOutput: " + retVal);
    }
}

Example 6: c# get the return value of a func

Func<int> function;
int returnValue;

function = () => 0;
returnValue = function();