Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Passing Parameters in C#

In this article, we will learn about different ways to pass parameters to a functions in C#.

C# Call By Value

In C#, value-type parameters are that pass a copy of original value to the function rather than reference. It does not modify the original value. A change made in passed value does not alter the actual value. In the following example, we have pass value during function call.

Example:

using System;
namespace CallByValue {
    class Program {
        // User defined function  
        public static void Show(int val) {
            val *= val; // Manipulating value  
            Console.WriteLine("Value inside the show function " + val);
            // No return statement  
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args) {
            int val = 50;
            Console.WriteLine("Value before calling the function " + val);
            Show(val); // Calling Function by passing value            
            Console.WriteLine("Value after calling the function " + val);
        }
    }
}

Output:

Value before calling the function 50

Value inside the show function 2500

Value after calling the function 50


C# Call By Reference

C# provides a ref keyword to pass argument as reference-type. It passes reference of arguments to the function rather than copy of original value. The changes in passed values are permanent and modify the original variable value.

Example:

using System;
namespace CallByReference {
    class Program {
        // User defined function  
        public static void Show(ref int val) {
            val *= val; // Manipulating value  
            Console.WriteLine("Value inside the show function " + val);
            // No return statement  
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args) {
            int val = 50;
            Program program = new Program(); // Creating Object  
            Console.WriteLine("Value before calling the function " + val);
            Show(ref val); // Calling Function by passing reference            
            Console.WriteLine("Value after calling the function " + val);
        }
    }
}

Output:

Value before calling the function 50

Value inside the show function 2500

Value after calling the function 2500


C# Out Parameter

C# provides out keyword to pass arguments as out-type. It is like reference-type, except that it does not require variable to initialize before passing. We must use out keyword to pass argument as out-type. It is useful when we want a function to return multiple values.

Example 1:

using System;
namespace OutParameter {
    class Program {
        // User defined function  
        public static void Show(out int val) // Out parameter  
        {
            int square = 5;
            val = square;
            val *= val; // Manipulating value  
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args) {
            int val = 50;
            Console.WriteLine("Value before passing out variable " + val);
            Show(out val); // Passing out argument  
            Console.WriteLine("Value after recieving the out variable " + val);
        }
    }
}

Output:

Value before passing out variable 50

Value after receiving the out variable 25

The following example demonstrates that how a function can return multiple values.

Example 2:

using System;
namespace OutParameter {
    class Program {
        // User defined function  
        public static void Show(out int a, out int b) // Out parameter  
        {
            int square = 5;
            a = square;
            b = square;
            // Manipulating value  
            a *= a;
            b *= b;
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args) {
            int val1 = 50, val2 = 100;
            Console.WriteLine("Value before passing \n val1 = " + val1 + " \n val2 = " + val2);
            Show(out val1, out val2); // Passing out argument  
            Console.WriteLine("Value after passing \n val1 = " + val1 + " \n val2 = " + val2);
        }
    }
}

Output:

Value before passing

val1 = 50

val2 = 100

Value after passing

val1 = 25

val2 = 25


Task

In the editor below, perform the different tasks as directed.

Create two different functions:

  • func1: Takes an integer as an input (without reference), and changes its value to 2.
  • func2: Takes an integer as an input (with reference), and changes its value to 2.

Then, compare if the both the values after passing them to the functions are equal or not.
If equal, print True, else print False.
</p>

Start solving Passing Parameters in C# on Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test