how to call a function in c# code example

Example 1: How to make a function in C#

public void SayHello(string name) 
{
    Console.WriteLine("Hello")  
}


Console.WriteLine("What is your name?")
string name = Console.ReadLine() ; 

SayHello(Adam)

Example 2: 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 3: c# funtion

public int AddNumbers(int number1, int number2){    int result = number1 + number2;    if(result > 10)    {    return result;    }    return 0;}

Example 4: call class c#

//Declaring an object of type MyClass.
MyClass mc = new MyClass();

//Declaring another object of the same type, assigning it the value of the first object.
MyClass mc2 = mc;

Example 5: 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();
      }
   }
}