+ 2

Swap

How do i swap the values of two integer variables?

14th Nov 2020, 8:03 AM
Aleka
Aleka - avatar
14 Respostas
+ 9
int a=0, b=1; 1) int temp=a; a=b; b=temp; 2)a ^= b; b ^= a; a ^= b; 3) (a, b)=(b, a); UPD https://www.sololearn.com/Profile/4528074/?ref=app method 4)a+=b; b=a-b; a-=b;
14th Nov 2020, 8:04 AM
Orchiiik
Orchiiik - avatar
+ 6
Brian Tuple Deconstruction is one of several new language features that came out with C# 7. You might find this detailed review of tuple deconstruction interesting: https://github.com/dotnet/roslyn/blob/master/docs/features/deconstruction.md To get a sense of the other awesome features that came out with C# 7, take a look at this link: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7 Oh heck... it's worth checking these out as well: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8 - Continues expanding support for pattern matching in a static typed language. https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9 - Top Level Statements is a standout feature that removes the verbosity of specifying the namespace, class, and static main method. C# is such an expressive language, it's difficult for me to find as much joy coding in any other static typed language. Kotlin may be the closest to C# for me in this regards. Sorry for the tangent. 🙏
14th Nov 2020, 9:09 AM
David Carroll
David Carroll - avatar
+ 6
Brian We're still stuck on C# 7 for our applications as. I want to use features of C# 8 so badly. Hopefully, we'll get the upgrades in with an upcoming tech debt sprint? I'm still unpacking the awesome features of C# 9 and just discovered support for covariant return types. Mind is blown! 🤯😂 Seriously though... overloading methods based on different return types have huge potential for making code even more concise. https://daveabrock.com/2020/07/14/c-sharp-9-target-typing-covariants#:~:text=With%20return%20type%20covariance%2C%20you,returns%20a%20more%20specific%20type.&text=Now%2C%20you%20can%20return%20the%20more%20specific%20type%20in%20C%23%209.
14th Nov 2020, 9:30 AM
David Carroll
David Carroll - avatar
14th Nov 2020, 8:31 AM
Orchiiik
Orchiiik - avatar
+ 3
David Carroll thank you, much! My C# knowledge is definitely a couple revisions old. My last workplace was reluctant to upgrade.
14th Nov 2020, 9:18 AM
Brian
Brian - avatar
+ 3
I've seen people use method #2 and #4 also seen people say it doesn't always work (forgot the why). So beginner context, take method #1, or #3 which looks pretty neat (so long as the framework was updated). Hats off David Carroll for the updates links, fresh from the oven 🙏
14th Nov 2020, 10:17 AM
Ipang
+ 2
𝔸𝕞𝕒𝕟𝕓𝕖𝕜 𝕆𝕣𝕪𝕟𝕓𝕒𝕪 I see clearly that I am wrong. I did try to prove it to myself before posting, and I got a syntax error. Thank you for showing me something new! So, add my suggestion as number 4.
14th Nov 2020, 8:42 AM
Brian
Brian - avatar
+ 2
Brian Right, I remember slightly someone mentioned that. So how about negative numbers support in #2 and #4? are they all good for it? I still think #1 is the clearest and simple method, especially for beginners. Though we've all seen people asking how to do swaps without interim storage, many a time here #scratchinghead#
14th Nov 2020, 9:14 PM
Ipang
+ 2
Big Thanks Brian 🙏
16th Nov 2020, 5:31 AM
Ipang
+ 1
Thanks 🤗
14th Nov 2020, 8:07 AM
Aleka
Aleka - avatar
+ 1
𝔸𝕞𝕒𝕟𝕓𝕖𝕜 𝕆𝕣𝕪𝕟𝕓𝕒𝕪 option 3) is valid in Python, but not in C#. May I suggest a replacement for 3? 3) a += b; b = a - b; a -= b; EDIT: Oops, 3) IS valid in C# after all!
14th Nov 2020, 8:21 AM
Brian
Brian - avatar
+ 1
Brian, ok :)
14th Nov 2020, 8:49 AM
Orchiiik
Orchiiik - avatar
+ 1
Ipang #2 would not work for floating point numbers, unless they were cast to an equivalent-sized integer type. And #4 would be unreliable for floating point numbers due to floating point math issues, magnitude truncation, et al. Also, #4 may cause overflow in any data type if the sum of a+b is too large.
14th Nov 2020, 4:05 PM
Brian
Brian - avatar
+ 1
Ipang negative integers should work just fine with all the methods, including #2 and #4 (while heeding the warning about a+b overflow). I fully agree that #1 is the most universal and recommended solution. It is good for all data types, value ranges, and not limited to any particular language.
15th Nov 2020, 11:32 PM
Brian
Brian - avatar