C# Fundamentals: Variables, Data Types, and Core Concepts

Variables

Variables store data in memory for use in your programs.

Declaration syntax:

dataType variableName;
variableName = value;

The assignment operator = assigns the value on the right to the variable on the left. It does not represent equality in the mathematical sense.

Shorthand declaration:

dataType variableName = value;

Data Types

// Integer: stores whole numbers only
int age = 25;

// Double: stores integers and decimals (15-16 digit precision)
double price = 19.99;


// Decimal: used for monetary calculations (requires 'm' suffix)
decimal balance = 1000.50m;

// String: stores multiple characters, enclosed in double quotes
string greeting = "Hello World";


// Char: stores a single character, enclosed in single quotes
char grade = 'A';

IDE Underline Indicators

Red underline: Syntax error detected in your code.

Green underline: No syntax error, but the IDE flags a potential runtime issue. This is a warnning—not a guaranteed failure.

Variable Usage

Always declare a variable before assigning a value and using it in your program.

Naming Conventions

Variables must follow these rules:

// Valid: starts with a letter, underscore, or @ symbol
// Can include letters, numbers, and underscores after the first character

string firstName;     // OK
int _count;          // OK
string @namespace;    // OK (using @ allows reserved keywords)

Important constraints:

  • Variable names cannot match C# reserved keywords
  • C# is case-sensitive (Name and name are different)
  • Duplicate variable names in the same scope are not allowed

Standard Naming Styles

Camel Case: First letter lowercase, subsequent word capitals. Used for variables and fields.

string customerName;
int totalAmount;
double interestRate;

Pascal Case: First letter of every word capitalized. Used for classes and methods.

string CustomerName;
int TotalAmount;

Assignment Operators

The = operator assigns the right-side value to the left-side variable. An expression using = is called an assignment expression.

int count = 10;

The Plus Operator

The + operator serves two purposes:

  • Concatenation: joining strings together
  • Addition: performing arithmetic sum

Format Specifiers

Format specifiers allow you to insert values into placeholders.

string name = "Alice";
int age = 30;
Console.WriteLine("Name: {0}, Age: {1}", name, age);

Rules:

  • Each placeholder {index} must have a corresponding argument
  • Excess arguments are ignored
  • Missing arguments throw an exception
  • Arguments are output in placeholder order

Exceptions

An exception occurs when code is syntactically correct but fails during execution due to unexpected conditions, causing the program to terminate abnormally.

Escape Sequences

An escape sequence combines a backslash \ with a special character to produce a specific effect.

\n    // New line
\t    // Tab character
\"    // Double quote
\b    // Backspace (ineffective at string boundaries)
\r\n   // Windows line break (use instead of \n on Windows)
\\    // Backslash

The @ Symbol

The @ symbol has two functions:

  • Disables escape sequence processing in strings
  • Preserves whitespace formatting exactly as written

Arithmetic Operators

+   // Addition
-   // Subtraction
*   // Multiplication
/   // Division
%   // Modulo (remainder)

An arithmetic expression combines operands with arithmetic operators.

Type Conversion

Arithmetic and assignment operations require compatible types between operands and results. When types differ, conversion rules apply.

Implicit Conversion

Automatically performed when both conditions are met:

  • Source and target types are compatible
  • Target type has larger capacity than source type
int smallValue = 100;
double largeValue = smallValue;  // implicit conversion

Explicit Conversion

Required when converting a larger type to a smaller compatible type.

double largeValue = 99.9;
int smallValue = (int)largeValue;  // explicit conversion, truncates decimal

Always verify that explicit conversions won't cause data loss before using them.

Tags: C# Variables Data Types Type Conversion Operators

Posted on Sat, 30 May 2026 22:01:32 +0000 by nepton