Search code examples
c#int

Combine 2 integer's text not add them together


I have two integers, x and y. What I am trying to do, is combine the numbers in both, not add them together. I have tried to do this:

int x = 5;
int y = 10;
sum = x + y;

But that makes the output 15. What I am wondering is if there is any way to combine them, so that the output would be 510.

5 + 10 = 510

That is what I am trying to accomplice.

I know I could do something like this:

int x = 5;
int y = 10;
int sum;
sum = Convert.ToInt32(x.ToString() + y.ToString());

But that seems like a sloppy way to do it. Is there a better way to do this?

Thanks.


Solution

  • A little simplier:

    int x = 5;
    int y = 10;
    int sum;
    sum = Convert.ToInt32("" + x + y);
    

    Notice that you need convertion in any case. Implicit conversion is used here.