Could someone explain why I have to convert _one to string but the other 3 variables in the following C#...











up vote
0
down vote

favorite












I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);

//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}









share|improve this question







New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
    – Martin Parkin
    Nov 19 at 13:14






  • 1




    Possible duplicate of String Concatenation using '+' operator
    – mjwills
    Nov 19 at 13:16










  • What's the difference if you do not add? What is your problem?
    – SeM
    Nov 19 at 13:20










  • Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
    – HELLHOUND0606
    Nov 19 at 13:46

















up vote
0
down vote

favorite












I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);

//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}









share|improve this question







New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
    – Martin Parkin
    Nov 19 at 13:14






  • 1




    Possible duplicate of String Concatenation using '+' operator
    – mjwills
    Nov 19 at 13:16










  • What's the difference if you do not add? What is your problem?
    – SeM
    Nov 19 at 13:20










  • Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
    – HELLHOUND0606
    Nov 19 at 13:46















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);

//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}









share|improve this question







New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I don't understand why I am getting a cannot convert to string with _one but not the other 3! Yes, I'm new to programming and loosing my mind trying to figure out why stackoverflow is requiring me to enter more words than I needed to ask my question!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace diviTwo
{
class Program
{
static void Main(string args)
{
//Print Test Problems
var n = "n";
var one = "-1 + 4 * 6";
var two = "(35 + 5) % 7";
var three = "14 + -4 * 6 / 11";
var four = "2 + 15 / 16 * 1 - 7 % 2";
Console.WriteLine(one+ n+ two+ n+ three+ n+ four+ n);

//Print Results of Test Problems
var _one = -1 + 4 * 6;
var _two = (35 + 5) % 7;
var _three = 14 + -4 * 6 / 11;
var _four = (2 + 15) / ((16 * 1) - (7 % 2));
Console.WriteLine(Convert.ToString(_one)+ n+ _two+ n+ _three+ n+ _four+ n);
}
}
}






c#






share|improve this question







New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 13:11









HELLHOUND0606

34




34




New contributor




HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






HELLHOUND0606 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
    – Martin Parkin
    Nov 19 at 13:14






  • 1




    Possible duplicate of String Concatenation using '+' operator
    – mjwills
    Nov 19 at 13:16










  • What's the difference if you do not add? What is your problem?
    – SeM
    Nov 19 at 13:20










  • Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
    – HELLHOUND0606
    Nov 19 at 13:46




















  • Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
    – Martin Parkin
    Nov 19 at 13:14






  • 1




    Possible duplicate of String Concatenation using '+' operator
    – mjwills
    Nov 19 at 13:16










  • What's the difference if you do not add? What is your problem?
    – SeM
    Nov 19 at 13:20










  • Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
    – HELLHOUND0606
    Nov 19 at 13:46


















Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
– Martin Parkin
Nov 19 at 13:14




Take another look at your own code: Convert.ToString(_one). You are only converting _one to a string here, then trying to add the numeric variables to it.
– Martin Parkin
Nov 19 at 13:14




1




1




Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16




Possible duplicate of String Concatenation using '+' operator
– mjwills
Nov 19 at 13:16












What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20




What's the difference if you do not add? What is your problem?
– SeM
Nov 19 at 13:20












Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46






Problem is I didn't understand how C# decides what to do in my last WriteLine. @RobinBennett explained that if that first item is a string C# treats the rest of them as strings. I didn't know that and spent sometime trying to figure out why/how to make it work without conversion, gave up and found out how to convert that _one which visual studio seemed to raise the error on.
– HELLHOUND0606
Nov 19 at 13:46














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There's nothing special about _one, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did



Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);


If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375398%2fcould-someone-explain-why-i-have-to-convert-one-to-string-but-the-other-3-varia%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    There's nothing special about _one, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did



    Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);


    If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      There's nothing special about _one, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did



      Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);


      If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        There's nothing special about _one, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did



        Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);


        If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.






        share|improve this answer












        There's nothing special about _one, it's just that if the first item is a string, C# knows that you want to treat everything else as a string and concatenate them. You'd get the same result if you did



        Console.WriteLine("Result=" + _one + n + _two + n + _three + n + _four + n);


        If you just supplied a list of variables that were all integers, C# would add them up and give you the result. However you start with a number and add a string, so C# doesn't know what to do.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 13:19









        Robin Bennett

        1,23312




        1,23312






















            HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.













            HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.












            HELLHOUND0606 is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375398%2fcould-someone-explain-why-i-have-to-convert-one-to-string-but-the-other-3-varia%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python