![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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); } }
Advertisements