The Insider News
The Insider News is for breaking IT and Software development news. Post your news, your alerts and
your inside scoops. This is an IT news-only forum - all off-topic, non-news posts will be
removed. If you wish to ask a programming question please post it
Get The Daily Insider direct to your mailbox every day. Subscribe
Kent Sharkey wrote:Or is this about swapping the values of two variables without using a third?
For numeric types:
int i = 1;
int j = 2;
i = i + j;
j = i - j;
i = i - j
Also works for a mix of positive and negative numbers.
char y = 'a';
char z = 'b';
y = (char)((int)y+(int)z);
z = (char)((int)y-(int)z);
y = (char)((int)y-(int)z);
You can use the same general approach with characters by casting them to int first. For strings:
string a = "Hello";
string b = "World";
a = a + b;
b = a.Substring(0,a.Length - b.Length);
a = a.Substring(b.Length);
".45 ACP - because shooting twice is just silly" - JSOP, 2010
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 16-Oct-19 15:10pm.
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.