
- 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
Swap two variables in one line using C#
To swap two variables in a single line using the Bitwise XOR Operator.
val1 = val1 ^ val2 ^ (val2 = val1);
Above, we have set the values −
int val1 = 30; int val2 = 60;
The following is the example to swap both the variable in one line using C# −
Example
using System; class Demo { public static void Main(String[] args) { int val1 = 30; int val2 = 60; Console.WriteLine("Values before swap"); Console.WriteLine(val1); Console.WriteLine(val2); val1 = val1 ^ val2 ^ (val2 = val1); Console.WriteLine("Values after swap"); Console.WriteLine(val1); Console.WriteLine(val2); } }
- Related Articles
- Swap two variables in one line in using Java
- Swap two variables in one line in using Python?
- Swap two variables in one line in Java
- Swap two variables in one line in C/C+
- Swap two variables in one line in C/C++, Python, PHP and Java
- How to Swap Two Variables using Python?
- How to swap two variables in JavaScript?
- How to swap variables using Destructuring Assignment in JavaScript?
- How to swap two String variables without third variable.
- How to swap two files in Linux command line?
- How to swap variables with destructuring in JavaScript?
- Swap two Strings without using temp variable in C#
- Next higher number using atmost one swap operation in C++
- Finding the maximum number using at most one swap in JavaScript
- Largest smaller number possible using only one swap operation in C++

Advertisements