Elementary Overview
A variable is a named storage location with a type. In C#, the type controls what values the variable can hold and what operations are legal. C# is strongly typed, so the compiler rejects incompatible assignments and expressions before the program runs. That type safety is one of the core ideas behind the language and the .NET runtime.
This lesson continues directly from OSC#.001: .NET SDK and Compiler. The first lesson established the build-and-run loop; this lesson starts filling Program.cs with typed data. It also connects to OSDotNet.001: What Is .NET? and OSDotNet.002: What the Common Language Runtime Does.
What You Should Learn
- How to declare and initialize variables.
- The most common built-in C# types.
- Why
intmaps toSystem.Int32and other C# keywords map to .NET types. - How literals such as
42,3.14,'A', and"hello"get their types. - How
varperforms compile-time type inference. - How
constcreates a compile-time constant. - The beginner-level difference between value types and reference types.
- Why
decimalis usually preferred over binary floating-point for exact base-10 financial values. - How implicit and explicit numeric conversions differ.
Declare and Initialize Variables
A basic declaration places the type first, then the variable name, then an optional initializer. Initializing at declaration time is usually the clearest beginner pattern because the variable starts with a known value.
int miners = 24;
double temperature = 71.5;
decimal electricityCost = 0.0475m;
bool online = true;
char rack = 'A';
string siteName = "North Campus";
Microsoft’s C# documentation lists int, double, decimal, bool, char, and string among the most common built-in types. The C# keywords are language-friendly aliases for .NET types. For example, int and System.Int32 name the same type.
Integer Types
int is the normal starting point for whole numbers. It is a signed 32-bit integer. C# also provides smaller and larger integral types, including byte, short, long, and their unsigned forms. Pick the type that correctly represents the expected range and meaning of the data instead of choosing a smaller type merely to save a few bytes.
byte fanPercent = 85;
short ambientCelsius = -10;
int activeDevices = 120_000;
long totalPackets = 9_000_000_000L;
Underscores inside numeric literals are digit separators for readability. The L suffix marks a long literal when needed. C# also supports hexadecimal with 0x and binary with 0b, which becomes useful later in systems, networking, and hardware-oriented code.
Floating-Point and Decimal Types
double is the default type for a numeric literal with a decimal point. Use float when a 32-bit floating-point type is specifically appropriate and append f to its literals. Use decimal when base-10 decimal precision matters, such as currency and many accounting calculations, and append m.
double efficiency = 13.5;
float gpuLoad = 0.875f;
decimal powerPrice = 0.0475m;
Console.WriteLine(efficiency);
Console.WriteLine(gpuLoad);
Console.WriteLine(powerPrice);
double and float use binary floating-point and cannot exactly represent every decimal fraction. decimal is designed for higher-precision base-10 arithmetic. That does not make one type universally “better”; the correct choice depends on the domain.
bool, char, and string
bool stores true or false. Unlike C, C# does not implicitly treat integers as Boolean values. char represents a single UTF-16 code unit and uses single quotes. string represents text and uses double quotes.
bool breakerClosed = true;
char row = 'B';
string message = "Rack online";
Console.WriteLine(breakerClosed);
Console.WriteLine(row);
Console.WriteLine(message);
Strings are reference types, but they are also immutable: operations that appear to change a string create a new string value. C# supports string interpolation, which lets variables appear directly inside a formatted string literal.
string site = "Tacoma";
int racks = 18;
Console.WriteLine($"{site} has {racks} active racks.");
var Means Type Inference, Not Dynamic Typing
The var keyword tells the compiler to infer a local variable’s type from the initializer. The variable is still strongly and statically typed. Once var count = 10; is compiled, count is an int; it does not become a container that can later hold unrelated types.
var count = 10; // int
var name = "CSharp"; // string
var price = 19.99m; // decimal
// count = "ten"; // compile-time error
var requires an initializer because the compiler needs an expression from which it can infer the type. It is often useful when the type is obvious from the right-hand side. When inference would hide important information, writing the explicit type can improve readability.
const Creates a Constant
A local const must be initialized when declared, and its value cannot later be reassigned. Constants are useful for values that are truly fixed by the program’s design.
const int PortsPerSwitch = 48;
const decimal TargetPowerPrice = 0.05m;
// PortsPerSwitch = 96; // compile-time error
Do not use const merely because a value happens not to change during one run. The keyword means the value is a compile-time constant. Later lessons will distinguish const from readonly fields and immutable object design.
Value Types and Reference Types
At a beginner level, C# types fall into two major categories. A value-type variable contains its value directly, and assignment copies that value. A reference-type variable contains a reference to an object, so copying the variable copies the reference. Built-in numeric types, bool, and char are value types. Classes, arrays, delegates, and string are reference types.
int a = 5;
int b = a;
b = 9;
Console.WriteLine(a); // 5
Console.WriteLine(b); // 9
The exact runtime storage details are more nuanced than the oversimplified rule “value types are on the stack and reference types are on the heap.” For now, focus on copy semantics: value assignments copy the value; reference assignments normally copy a reference to the same object.
Implicit and Explicit Numeric Conversions
C# performs some numeric conversions implicitly when the destination can safely represent the source range. Other conversions require an explicit cast because information could be lost.
int miners = 120;
long total = miners; // implicit conversion
double reading = 98.7;
int whole = (int)reading; // explicit conversion; fractional part is discarded
Console.WriteLine(total);
Console.WriteLine(whole);
A cast is not a promise that the conversion is harmless. It tells the compiler that you intentionally requested the conversion. Always consider range, precision, and overflow when moving between numeric types.
Compile and Run
dotnet new console -n VariableLab
cd VariableLab
dotnet build
dotnet run
Put the examples into Program.cs, build after each change, and read compiler errors carefully. Type errors are not merely obstacles; they are the compiler showing exactly where the program violates the type rules.
Mini Lab
- Create variables for a data-center site name, rack count, electrical price per kWh, ambient temperature, and online/offline state.
- Use
string,int,decimal,double, andboolappropriately. - Create one variable with
varand write a comment identifying the type the compiler infers. - Create one
constvalue. - Print a single summary line using string interpolation.
- Perform one safe implicit numeric conversion and one explicit numeric conversion.
- Deliberately assign a string to an integer variable, build the program, read the compiler diagnostic, then remove the invalid line.
Knowledge Check
- What does a variable’s type control?
- What .NET type does the C# keyword
intrepresent? - What suffix is normally used on a
decimalliteral? - Does
varmake a C# variable dynamically typed? - Why must a
varlocal have an initializer? - What does
constguarantee? - What is the beginner-level difference between assigning a value type and assigning a reference type?
- Why can converting
doubletointrequire an explicit cast?
Answers
- It controls which values the variable may hold and which operations are valid.
System.Int32.morM.- No. The compiler infers a normal static type at compile time.
- The compiler needs the initializer expression to infer the type.
- The declared value cannot be reassigned and is treated as a compile-time constant.
- Assigning a value type copies the value; assigning a reference type normally copies a reference to the same object.
- The conversion can discard the fractional part and may lose information.
Primary References
- Microsoft Learn — Built-in types and literals
- Microsoft Learn — The C# type system
- Microsoft Learn — Built-in types reference
- Microsoft Learn — Implicitly typed local variables
- Microsoft Learn — Declarations and constants
- Microsoft Learn — Numeric conversions
Elementary Review
C# variables are typed storage locations. Start by mastering explicit declarations such as int count = 10;, then add var when type inference improves readability. Learn the difference between integer, floating-point, decimal, Boolean, character, and text data. Treat const as a real compile-time contract, and remember that the compiler’s type errors are one of C#’s most useful safety systems.
BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.

Leave a Reply