
- 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 pointers as parameters to methods in C#?
To pass pointers as parameters to methods, refer the below steps −
Firstly, crate a function swap with unsafe modifier.
public unsafe void swap(int* p, int *q) { int temp = *p; *p = *q; *q = temp; }
Now under static void main, add the value for the first and second variable, set pointers for both of them.
Display the values of the variables and then call the swap() method shown above. The method swaps the values and displays the result −
public unsafe static void Main() { Program p = new Program(); int var1 = 10; int var2 = 20; int* x = &var1; int* y = &var2; Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2); p.swap(x, y); Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2); Console.ReadKey(); }
- Related Articles
- Why C treats array parameters as pointers?
- How to pass parameters to a method in C#?
- How to pass reference parameters PHP?
- How to pass parameters using param array in a C# method?
- How to pass optional parameters to a function in Python?
- How to pass keyword parameters to a function in Python?
- How to pass the parameters in the PowerShell function?
- How to pass Arrays to Methods in Java?
- How do we pass parameters by reference in a C# method?
- How do we pass parameters by value in a C# method?
- How to compare pointers in C/C++?
- How can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?
- How to pass a 2D array as a parameter in C?
- How to pass entire structure as an argument to function in C language?
- How to pass an entire structure as an argument to function in C?

Advertisements