Reputation: 1
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
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
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}");
String interpolation provides a more readable and convenient syntax to create formatted strings than a string composite formatting feature.
Upvotes: 2
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
Reputation: 77906
You forgot to provide the placeholder for that sum calculation
Console.WriteLine("Here there are {0}",a+b);
Upvotes: 4