Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> noSwapList = new List<int>() { 1, 2 };
- //Swap value types - no modification to list.
- Swap(noSwapList[0], noSwapList[1]);
- Console.WriteLine("No swap occures");
- PrintList(noSwapList);
- //Swap ref types - modification to list.
- Swap(noSwapList, 0, 1);
- Console.WriteLine("Swap occures");
- PrintList(noSwapList);
- }
- private static void PrintList(List<int> noSwapList)
- {
- Console.WriteLine();
- foreach (var item in noSwapList)
- {
- Console.Write(item + " ");
- }
- Console.WriteLine();
- }
- static void Swap(int a, int b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- static void Swap(List<int> list, int firstIndex, int secondIndex)
- {
- int temp = list[firstIndex];
- list[firstIndex] = list[secondIndex];
- list[secondIndex] = temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement