SkillPilotAI

Variables and data types

A variable is a named box with a fixed type that decides what data it can hold. Picking the right type (decimal for money, long for big counts, string for phone numbers) prevents real bugs like 0.1 + 0.2 not being 0.3.

4.1 Problem: money in a double

C#
// UPI cashback: 10 paise + 20 paisedouble wallet = 0.1 + 0.2; Console.WriteLine(wallet);Console.WriteLine(wallet == 0.3);
Output
$ dotnet run Bug.cs0.30000000000000004False
Why 0.1 + 0.2 != 0.3 in a double
  1. 1.double stores numbers in binary (base 2)

  2. 2.0.1 and 0.2 cannot be represented exactly in base 2

  3. 3.Each value carries a tiny error

  4. 4.Adding them shows the error: 0.30000000000000004

  5. 5.wallet == 0.3 -> False

Money = decimal (base 10, exact)

4.2 Fix: decimal + m suffix

  • decimal stores numbers in base 10, so 0.1 is exact: use it for money.
  • The m suffix marks a literal as decimal; without it 0.1 is a double and assigning it to a decimal does not compile.
  • double is fine for science, measurements and games, where a tiny error does not matter.
C#
// UPI cashback: 10 paise + 20 paisedecimal wallet = 0.1m + 0.2m; Console.WriteLine(wallet);Console.WriteLine(wallet == 0.3m);
Output
$ dotnet run Fix.cs0.3True

4.3 Basic data types

  • Pattern on every line: type, name, value.
  • Digit separators (9_000_000_000) make big numbers readable.
C#
int quantity = 3;long views = 9_000_000_000;double distanceKm = 12.5;decimal price = 1899.50m;bool isPaid = true;char grade = 'A';string city = "Pune"; Console.WriteLine(quantity);Console.WriteLine(views);Console.WriteLine(distanceKm);Console.WriteLine(price);Console.WriteLine(isPaid);Console.WriteLine(grade);Console.WriteLine(city);
Output
$ dotnet run Types.cs3900000000012.51899.50TrueAPune
TypeHoldsExample use
intWhole numbers up to about 2 billionQuantity, age
longVery large whole numbersViews, IDs
doubleBinary floating point (approximate)Distance, measurements
decimal (m)Base-10, exact; keeps trailing zeros (1899.50)Money, prices
booltrue / falseisPaid
charOne character, single quotes'A'
stringText, double quotes"Pune"

4.4 var: the compiler picks the type

  • With var you skip the type; the compiler infers it from the right side.
  • GetType shows the real .NET names: Int32, Decimal, String.
  • The type is fixed at compile time: assigning text to count later fails with CS0029.
  • var is only a writing shortcut, not a "any type" variable like JavaScript.
C#
var count = 10;var price = 499.99m;var name = "Asha"; Console.WriteLine(count.GetType());Console.WriteLine(price.GetType());Console.WriteLine(name.GetType()); count = "ten";
Output
# without the last line$ dotnet run Var.csSystem.Int32System.DecimalSystem.String # with count = "ten"$ dotnet run Var.csVar.cs(9,9): error CS0029: Cannot implicitly  convert type 'string' to 'int'The build failed. Fix the build errors and run again.

4.5 const vs readonly

C#
const decimal GstRate = 0.18m;decimal price = 1000m; Console.WriteLine(price * GstRate);Console.WriteLine(AppInfo.StartedAt); static class AppInfo{    public static readonly DateTime StartedAt        = DateTime.Now;}
Output
$ dotnet run Const.cs180.0011/6/2026 11:36:15 PM # changed GstRate and StartedAt$ dotnet run ConstError.cserror CS0131: The left-hand side of an  assignment must be a variable, property or indexererror CS0198: A static readonly field cannot be  assigned to (except in a static constructor  or a variable initializer)
Pointconstreadonly
Value fixedAt compile timeOnce at runtime (declaration or constructor)
Allowed typesBuilt-in types only (numbers, bool, string)Any type (e.g. DateTime)
Static?Automatically staticInstance or static
ExampleGstRate = 0.18mStartedAt = DateTime.Now

4.6 Default values and unassigned locals

  • Fields with no value get a default: int and decimal 0, bool False, string null.
  • Local variables get no default: using one before assigning is error CS0165.
C#
Console.WriteLine(Defaults.Count);Console.WriteLine(Defaults.Price);Console.WriteLine(Defaults.IsActive);Console.WriteLine(Defaults.Name is null); static class Defaults{    public static int Count;    public static decimal Price;    public static bool IsActive;    public static string? Name;}
Output
$ dotnet run Defaults.cs00FalseTrue # int count; Console.WriteLine(count);$ dotnet run Unassigned.csUnassigned.cs(2,19): error CS0165:  Use of unassigned local variable 'count'The build failed. Fix the build errors and run again.

4.7 null and string? (first look)

  • null means the variable has no value at all.
  • string: null not allowed; string?: may be null.
  • Putting null into a plain string gives warning CS8600: an early sign of a future crash.
C#
string name = "Ravi";string? middleName = null; Console.WriteLine(name.Length);Console.WriteLine(middleName is null); string nickname = null;
Output
$ dotnet run Nullable.csNullable.cs(7,19): warning CS8600: Converting null  literal or possible null value to non-nullable type.4True

4.8 Common mistakes

  • int Mobile = 9876543210 fails (CS0266): it is bigger than int, and you never do maths on a phone number: use string.
  • Names like x tell nothing: use mobileNumber, orderAmount, MinAge.
  • Money in a double: use decimal with the m suffix.
  • Naming: locals in camelCase, constants in PascalCase.
C#
string mobileNumber = "9876543210";const int MinAge = 18;decimal orderAmount = 99.99m;

4.9 Interview tips

  • const vs readonly: const is fixed at compile time (built-in types only, automatically static); readonly is set once at runtime and can be any type.
  • Changing a public const means projects using it must be rebuilt (the value is copied into them).
  • var is statically typed: fixed at compile time and just as strict; dynamic exists but is almost never needed.
  • Why decimal for money: double is binary and cannot store 0.1 exactly.