Noob question about mathematical addition vs. "string addition" in C#

Joined
Mar 6, 2022
Messages
1
Reaction score
0
Hi all!

I have just started out to learn programming for the first time and am an absolute noob.

I ran into a question from a simple assignment in Vystavel's C# Programming for absolute beginners, regarding different ways of putting numbers together.

I'm in the standard VS Console App C# (.NET Framework) environment:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

ex. 1:
Console.WriteLine("a) " + 1 + 1);
results in a) 11. I understand why.

ex. 2:
Console.WriteLine("b) " + (1 + 1));
results in b) 2. Again, I understand why.

But what if I want the result of 1 + 1 to be the number 11 without prefacing it with text like in example 1?
So, having the 1's string together to 11 instead of adding up to 2?

(if I understood correctly, doing ("1" + "1") will result in text, as will ("11"), instead of a number)
 
Joined
Sep 4, 2022
Messages
127
Reaction score
16
hello ,

by Math , 1+1 in integer Type will always result in 2.

to reach 11 as integer, It's 10+1 = 11. ( decimal base )


when you use (1+1) you're giving priority to Math operation, for the total 2.

Console.writeline() return string as output, and cast to string all the parameters in it.

I'm not sure I understand what you're requesting,
is it about binaries ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top