
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are different types of parameters to a method in C#?
Methods in C# are generally the block of codes or statements in a program which gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides better readability of the code.
There might be certain situations the user wants to execute a method but sometimes that method requires some valuable inputs in order to execute and complete its tasks. These input values are known as Parameters.
Parameters can be passed to a method in the following ways −
Value Parameters
Reference Parameters
Output Parameters
Value Parameters
Value Parameters copies the actual value of an argument into the formal parameter of the function. When a simple variable is passed as the parameter to any method, it is passed as a value. This means that the value contained by the variable that is passed as the parameter is copied to the variables of the method, and if inside the method these values are changed or modified, the change is not reflected in the actual passed variable. Most of the primitive data types such as integer, double, Boolean etc. are passed by value.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ int x = 5, y = 5; Console.WriteLine($"Value before calling the method. x = {x}, y = {y}"); ValueParamter(x, y); Console.WriteLine($"Value after calling the method. x = {x}, y = {y}"); } public static void ValueParamter(int x, int y){ x = 10; y = 10; int z = x + y; Console.WriteLine($"Sum of x and y = {z}"); } } }
Output
The output of the above code is as follows &mius;
Value before calling the method. x = 5, y = 5 Sum of x and y = 20 Value after calling the method. x = 5, y = 5
Reference Parameters
Reference Parameters copies the reference to the memory location of an argument into the formal parameter. Normally, all the objects are passed by reference as parameter to the method. The method operates on the references of the variables passed in the parameters rather than operating on their values. This results in the modification of variables in the calling function when they are modified in the called function. This means that changes made to the parameter affect the argument.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ int x = 5, y = 5; Console.WriteLine($"Value before calling the method. x = {x}, y = {y}"); RefParamter(ref x, ref y); Console.WriteLine($"Value after calling the method. x = {x}, y = {y}"); } public static void RefParamter(ref int x, ref int y){ x = 10; y = 10; int z = x + y; Console.WriteLine($"Sum of x and y = {z}"); } } }
Output
The output of the above code is as follows −
Value before calling the method. x = 5, y = 5 Sum of x and y = 20 Value after calling the method. x = 10, y = 10
Output Parameters
Output Parameters helps in returning more than one value. A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. The variable supplied for the output parameter need not be assigned a value. Output parameters are particularly useful when you need to return values from a method through the parameters without assigning an initial value to the parameter. Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.
Example
using System; namespace MyApplication{ public class Program{ public static void Main(){ int result; OutParamter(out result); Console.WriteLine($"Result: {result}"); } public static void OutParamter(out int result){ int x = 10, y = 10; result = x + y; } } }
Output
The output of the above code is as follows: Result: 20
- Related Articles
- What are different methods of passing parameters in C#?
- What are different types of constants in C++?
- What are different types of constants in C language?
- What are different types of data in C language?
- What are the different data types of arrays in C#?
- What are different types of access modifiers available in C#?
- What are the different types of pointers in C language?
- What are the different types of functions in C Programming?
- What are the different types of keywords in C language?
- What are different types of computers according to their size in C?
- What are different Parameters available in Newman using Postman?
- Method Parameters in C#
- How to pass parameters to a method in C#?
- What are the different types of filters in C# ASP.NET WebAPI?
- What are different types of interrupts?
