C# Difference  Between Method & Function in C#C#

Difference Between Method & Function in C#

Method: A method is a ready-to-use function. Methods are always associated with a class. Methods can access and modify the data. Below are examples of the class and methods. A method can’t be  defined without the class

For example:

Class

Method

Math

Max

Math

Min

Array

GetLength

Array

Equals

string

Concat

string

Equals

DateTime

Today

DateTime

Now

Example of Method :

Scenario: create a program to find a max no between these 2 no(40,60)

using System;
class Program
{
    static void Main()
    {
        int result = Math.Max(40, 60);
        Console.WriteLine(result);
    }
}

Explanation:

using System;
class Program
{
    static void Main()

This line defines the Main method, which is the entry point of the program. When you run the program, this method is executed first.

int result= Math.Max(40, 60);

This line declares an integer variable named result and sets it to the larger of the two numbers 40 and 60 using the Math.Max method. Here, Math.Max is a method that finds the maximum value between two numbers.

Console.WriteLine(result);

This line prints the value of MaximumValue to the console. In this case, it prints 60 because that’s the larger of the two numbers.

Function: A function is a block of reusable code that performs a specific task. Functions can take arguments as input and return values as output.

Example of Function :

Scenario: create a program to find a max number between these 2 no(10,20) without using Math.Max method and use custom  function.

using System;
class Program
{
    public static void Maximum()
    {
        int marksofenglish  = 10;
        int marksofscience = 20;
        int result  = marksofenglish  > marksofscience? marksofenglish  : marksofscience;
        Console.WriteLine(result);
    }
    static void Main()
    {
        Maximum();
    }
}

Explanation:

using System;
class Program
{
    public static void Maximum()

This defines a method named Maximum. The public keyword means this method can be called from outside the class. The static keyword means this function belongs to the class itself, not to any specific object of the class.

int marksofenglish  = 10;

This declares an integer variable named marksofenglish and sets its value to 10.

int marksofscience = 20;

This declares another integer variable named marksofscience  and sets its value to 20.

int result  = marksofenglish  > marksofscience? marksofenglish  : marksofscience;

This line uses a conditional (ternary) operator to find the larger of the two numbers. It checks if marksofenglish is greater than marksofscience. If true, result is set to marksofenglish. If false, the result is set to marksofscience. In this case, marksofscience is larger, so the result will be 20.

Console.WriteLine(result);

This line prints the value of the result to the console. It will print ” 20″.

static void Main()

This defines the Main method, which is the entry point of the program. When you run the program, this method is executed first

Maximum();

This line calls the Maximum function, which then executes the code inside that function.

In the above 2 examples, we have shown you what is the difference between Method and Function i.e. to find out the maximum number  we can either use method or function

Class

There are two types of class

1st Standard class like math, string, and Datetime, etc. as described in the above method table

2nd Custom class as you can see in the below code  class operation is a custom class

using System;
class Operation
{
   public static void Main()
    {   
// Your code
    }
}

We can only define a standard class with a method for example Math.Max, string.Concat, Array.Equals and DateTime.Today

Course Video