Values in C#

Our first program will demonstrate various value types in C#, including strings, integers, floats, and booleans. Here’s the full source code:

using System;

class Values
{
    static void Main()
    {
        // Strings, which can be concatenated with +
        Console.WriteLine("C#" + "lang");

        // Integers and floats
        Console.WriteLine("1+1 = " + (1 + 1));
        Console.WriteLine("7.0/3.0 = " + (7.0 / 3.0));

        // Booleans, with boolean operators as you'd expect
        Console.WriteLine(true && false);
        Console.WriteLine(true || false);
        Console.WriteLine(!true);
    }
}

To run the program, save the code in a file named Values.cs and use the C# compiler (csc) to compile it, then execute the resulting executable.

$ csc Values.cs
$ ./Values.exe
C#lang
1+1 = 2
7.0/3.0 = 2.3333333333333335
False
True
False

C# has various value types including strings, integers, floats, booleans, and more. This example demonstrates some basic operations with these types:

  1. Strings can be concatenated using the + operator.
  2. Integer and floating-point arithmetic work as expected.
  3. Boolean operations follow standard logical rules.

C# provides type inference for local variables, but in this example, we’ve used explicit Console.WriteLine statements for clarity. In practice, you might use var for local variable declarations where the type is obvious from the context.

This program gives you a glimpse of some basic value types and operations in C#. As you progress, you’ll encounter more complex types and operations that build on these fundamentals.