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
Course Video In English
Frequently Asked Questions

Still have a question?

Let's talk

Floating-point data types in C# are used to represent numbers with fractional parts. The most commonly used floating-point types are float and double.

The main difference lies in precision. A float uses 4 bytes of memory and provides 6-7 decimal digits of precision, while a double uses 8 bytes and provides 15-16 decimal digits.

Use float when memory usage is critical, and precision is less important, such as in graphics rendering. Opt for double when higher precision is required, like in scientific computations.

Yes, C# provides the decimal type, which is designed for high-precision arithmetic, often used in financial calculations.

Yes, you can represent numbers in scientific notation using the e or E syntax. For example, 1.23e4 equals 12300.

Floating-point calculations may result in rounding errors due to limited precision. These errors can accumulate in iterative computations.

Yes, floating-point data types are fundamental to C# and are supported across all .NET versions.

Yes, you can perform standard arithmetic operations like addition, subtraction, multiplication, and division with float and double.

Yes, you can use methods like ToString or string interpolation to format floating-point numbers. For example:
double number = 123.456;
console.WriteLine(number.ToString(“F2”));
//output: 123.46

Sure! Here are some examples:
float a = 5.5f;
float b = 2.2f;
float result = a+b;//Addition
console.WriteLine(result);
//output: 7.7

Yes, You can learn C# floating-point types with examples through free online courses and tutorials, such as those offered by Iqra Academy.

Yes, free Hindi video tutorials are available at Iqra Academy, covering floating-point types in C# with practical examples.

Best practices include:

  • Use double for higher precision calculations.
  • Avoid floating-point for exact values like currency (use decimal instead).
  • Be cautious of rounding errors in arithmetic operations.

Floating-point types are essential for handling scientific calculations, graphics programming, and any application requiring decimal numbers.

Common issues include:

  • Precision errors in large or small values.
  • Rounding errors during calculations.
    To handle these, you can use the decimal type for higher accuracy.