
- 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 methods of passing parameters in C#?
When a method with parameters is called, you need to pass the parameters to the method using any of the following three methods -
Reference Parameters
This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.
Value Parameters
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
In Value parameters, when a method is called, a new storage location is created for each value parameter. The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument.
Let us see an example for Value Parameters in C# −
Example
using System; namespace CalculatorApplication { class NumberManipulator { public void swap(int x, int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put temp into y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* local variable definition */ int a = 7; int b = 10; Console.WriteLine("Before swap, value of a : {0}", a); Console.WriteLine("Before swap, value of b : {0}", b); /* calling a function to swap the values */ n.swap(a, b); Console.WriteLine("After swap, value of a : {0}", a); Console.WriteLine("After swap, value of b : {0}", b); Console.ReadLine(); } } }
Output
Before swap, value of a : 7 Before swap, value of b : 10 After swap, value of a : 7 After swap, value of b : 10
- Related Articles
- What are different methods of weeding?
- What are different Parameters available in Newman using Postman?
- What are different types of parameters to a method in C#?
- What are different hashing methods in DBMS?
- What are different Navigator methods available?
- What are different methods of parallelism in Parallel Computer Architecture?
- What are different data conversion methods in Python?
- What are the different cookies methods in Selenium?
- What are the different modes of parameters used by MySQL stored procedure?
- Passing parameters to callback functions JavaScript
- What are the different methods of creating a css expression?
- What are different methods to implement rename buffers?
- Passing multiple parameters in SAP BO Webi report
- What are default-parameters for function parameters in JavaScript?
- Passing arrays to methods in C#?
