Ned3
Ned3

Reputation: 1

Why does Console.WriteLine() not print a string and an int together

I have a problem with Console.WriteLine() in C#. It just doesn't display string and int variables together.

It just shows the string and not the int, it mentions something about string format that i don't understand how to fix and that didn't happen to me before.

Code:

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

namespace FirstStep
{
    class Program11
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 4;

            Console.WriteLine(" summ up to Digits ");
            Console.WriteLine("Here there are",a+b);
            Console.ReadKey();

        }

    }
}

Upvotes: 0

Views: 1228

Answers (4)

Devanshu
Devanshu

Reputation: 933

With C# 6, you can now use the hybrid solution:

Console.WriteLine($"Hi {a+b} "); 

This is referred to as string interpolation.

Upvotes: 0

Prasad Telkikar
Prasad Telkikar

Reputation: 16079

You need specific format information like placeholder,

Console.WriteLine("Here there are {0}",a+b);

or

You can use string interpolation to print integer with string,

Console.WriteLine($"Here there are {a+b}");

From MSDN:

String interpolation provides a more readable and convenient syntax to create formatted strings than a string composite formatting feature.

Upvotes: 2

vahab-balouchzahi
vahab-balouchzahi

Reputation: 247

it is simple:

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

namespace FirstStep
{
    class Program11
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 4;
            //define c 
            int c=a+b;
            Console.WriteLine(" summ up to Digits ");
            //here append c
            Console.WriteLine("Here there are"+c);
            Console.ReadKey();

        }

    }
}

Upvotes: 0

Rahul
Rahul

Reputation: 77906

You forgot to provide the placeholder for that sum calculation

Console.WriteLine("Here there are {0}",a+b);

Upvotes: 4

Related Questions