Combining types in output statements in C#

This tutorial shows you how you can combine types in a Console.WriteLine() output statement. For example, you can combine a string with an integer or float value. Watch the video below and scroll down for the sample code.

Sample code

using System;

namespace MyCSharpProject
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      string message = "The result is ";
      float num1 = 6f; // Assign value to num1 variable
      float num2 = 4f; // Assign value to num2 variable

      float result = num1 + num2 + 10f; // Add numbers

      result = num1 - num2; // Subtract
      result = num1 * num2; // Multiply
      result = num1 / num2; // Divide
      result = num1 % num2; // Mod

      int num3 = 30;
      num3--;

      Console.WriteLine(message + result); // String and float value combined in output statement
      Console.WriteLine(num3);
    }
  }
}

Next tutorial: Converting variable types in C#