bhylu.com

Advertisements

C# Variables and Data Types

Choose integer values for places and use var only when the initializer makes its type clear.

How it works

An int stores a whole value in a defined finite range. A long offers a wider range, not unlimited arithmetic. var infers a local variable’s type from its initializer at compile time; the variable does not become dynamically typed. Keep units in names where the type alone cannot distinguish minutes, places and identifiers. A valid C# expression can still implement the wrong real-world calculation if it combines incompatible units. Initialize a local before reading it and validate values arriving from outside the program.

Run it locally

Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.

dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-build

Use the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.

Download the complete C# source as Program.cs.

Example

C# 14 · Program.cs

using System;

internal static class Program
{
    private static void Main(string[] args)
    {
        int bookedPlaces = 7;
        var extraPlaces = 3;
        long sessionId = 42001;
        Console.WriteLine($"Session {sessionId}: {bookedPlaces + extraPlaces} booked");
        Console.WriteLine($"int maximum: {int.MaxValue}");
    }
}

Expected result

Session 42001: 10 booked
int maximum: 2147483647

The literal three infers int for extraPlaces. The identifier is deliberately long even though this fixture is small. The fixed sum is safe, but a reusable booking operation should validate against a capacity before addition. C# has built-in numeric sizes; do not transfer assumptions about C’s implementation-dependent int width into this language.

Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.

Advertisements