Types, Variables, and Constants
Declare strongly typed values and constants in C#.
Introduction
C# checks types at compile time while var asks the compiler to infer a type from the assigned value.
const creates a compile-time constant. var remains statically typed; price is still a decimal and cannot later hold unrelated data.
Runnable C# example
Program.cs
using System;
class Program
{
static void Main()
{
const decimal TaxRate = 0.10m;
var price = 25.00m;
int quantity = 4;
decimal total = price * quantity * (1 + TaxRate);
Console.WriteLine($"Total: {total:C}");
}
}
Total: ¤110.00Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. Easy
- Simple Interest Find the total amount after simple interest. Easy