c# – Differences between methods, procedure, function, what each one is for and how they are declared in C #

Question:

How are methods, classes, procedures, and functions different in C # and how are they declared?

Answer:

  • Procedure : It is a set of instructions that fulfill a task
  • Function : Like a procedure but returns a value
  • Class : Object-oriented programming concept, it is a way of encapsulating functionality, it contains fields and methods.
  • Method : It can be a procedure or a function, the difference is that it belongs to a class.

.

public class ConsoleTest
{
    public void Saludo()
    {
        Console.WriteLine("¡Hola!");
    }

    public int Suma(int a, int b)
    {
        return a + b;
    }
}

In this example,

  • ConsoleTest is a class
  • Saludo is a method and a procedure. As it does not return a value it is type void
  • Suma is a method and a function
Scroll to Top