
- 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
How to pass parameters to a method in C#?
To pass parameters to a method in C#, let us see how to pass parameters by value. In this mechanism, 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.
Here is the example showing how to pass parameters to a method −
Example
using System; namespace Demo { class NumberManipulator { public void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); int a = 50; int b = 150; 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 : 50 Before swap, value of b : 150 After swap, value of a : 50 After swap, value of b : 150
- Related Articles
- How to pass parameters using param array in a C# method?
- How do we pass parameters by reference in a C# method?
- How do we pass parameters by value in a C# method?
- How to pass pointers as parameters to methods in C#?
- How to pass optional parameters to a function in Python?
- How to pass keyword parameters to a function in Python?
- How to pass reference parameters PHP?
- How do we pass parameters by value in a Java method?
- How to pass the parameters in the PowerShell function?
- Method Parameters in C#
- How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
- What are different types of parameters to a method in C#?
- How can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?
- How can I pass parameters in computed properties in VueJS?
- How to pass a jQuery event as a parameter in a method?

Advertisements